From c57fe0491634f22791de04e315ece0876f77812f Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 23 Apr 2019 15:32:52 +0200 Subject: [PATCH] edit book servlet now interacts with database --- src/main/java/de/hsel/itech/db/Database.java | 48 ++++++++++ .../java/de/hsel/itech/servlet/AddBook.java | 6 +- .../java/de/hsel/itech/servlet/BookList.java | 94 +++++++++++-------- 3 files changed, 104 insertions(+), 44 deletions(-) diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 35af2eb..2faa88e 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -146,6 +146,28 @@ public class Database { return author; } + @Nullable + public Author getAuthor(String name) { + Author author = null; + + Map.Entry, Connection> set = getResultSets(this.author, "name", name); + + if(set == null){ + return null; + } + + if (set.getKey().size() > 0) { + try { + author = new Author(set.getKey().get(0).getLong("id"), set.getKey().get(0).getString("name")); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + + return author; + } + /** * gets category by id. * @@ -562,6 +584,32 @@ public class Database { return null; } + /** + * gets specific entries from table. + * + * @param table table name + * @param column column to match + * @return {@link java.util.Map.Entry} + */ + @Nullable + private Map.Entry, Connection> getResultSets(@NotNull String table, @NotNull String column, @NotNull String value) { + Connection connection = getConnection(); + try { + assert connection != null; + PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?"); + statement.setString(1, table); + statement.setString(2, column); + statement.setString(3, value); + + System.out.println(statement); + + return getAllEntries(connection, statement); + } catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + /** * gets all specified columns from table. * diff --git a/src/main/java/de/hsel/itech/servlet/AddBook.java b/src/main/java/de/hsel/itech/servlet/AddBook.java index 77477a3..3a5a4de 100644 --- a/src/main/java/de/hsel/itech/servlet/AddBook.java +++ b/src/main/java/de/hsel/itech/servlet/AddBook.java @@ -1,13 +1,10 @@ package de.hsel.itech.servlet; -import de.hsel.itech.db.pojo.Book; - import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; -import java.util.ArrayList; public class AddBook extends HttpServlet { @@ -20,7 +17,7 @@ public class AddBook extends HttpServlet { Utillity.insertFile(out, "template_head.html"); de.hsel.itech.db.Database db = de.hsel.itech.db.Database.getInstance(); - ArrayList books = db.getBooks(); + //List books = db.getBooks(); out.println("
"); Utillity.addInput(out,"Titel", "title"); @@ -29,6 +26,7 @@ public class AddBook extends HttpServlet { Utillity.addInput(out,"Jahr", "year", "2019"); Utillity.addInput(out,"Beschreibung", "description"); Utillity.addInput(out,"Verlag", "publisher"); + Utillity.addInput(out,"Kategorie", "category"); Utillity.addInput(out,"Bild", "image", "DxAzOKSiPoE"); Utillity.addTypeButton(out, "Hinzufügen", "submit"); out.println("
"); diff --git a/src/main/java/de/hsel/itech/servlet/BookList.java b/src/main/java/de/hsel/itech/servlet/BookList.java index 99a29ec..0ff4a15 100644 --- a/src/main/java/de/hsel/itech/servlet/BookList.java +++ b/src/main/java/de/hsel/itech/servlet/BookList.java @@ -13,7 +13,7 @@ import java.io.PrintWriter; import java.time.Year; import java.util.ArrayList; import java.util.Enumeration; -import java.util.Random; +import java.util.List; public class BookList extends HttpServlet { @@ -34,13 +34,13 @@ public class BookList extends HttpServlet { String param = req.getParameter("removeid"); if(param != null){ long isbn = Long.parseLong(param); - db.removeBook(isbn); + db.deleteBook(isbn); resp.sendRedirect("booklist"); } if(req.getParameter("title") != null){ String title = req.getParameter("title"); - String author = req.getParameter("author"); + String authorName = req.getParameter("author"); int price = Integer.parseInt(req.getParameter("price")); int year = Integer.parseInt(req.getParameter("year")); String category = req.getParameter("category"); @@ -48,14 +48,24 @@ public class BookList extends HttpServlet { String publisher = req.getParameter("publisher"); String image = req.getParameter("image"); + + ArrayList authors = new ArrayList(); + Author author = null; + + author = db.getAuthor(authorName); + + if(author == null){ + author = new Author(authorName); + db.insert(author); + } + authors.add(author); Book book = new Book( - new Random().nextInt(), - new Author(0,author), - new Publisher(0,publisher), - new Category(0,category, "DxAzOKSiPoE"), + authors, + new Publisher(publisher), + new Category(category, "DxAzOKSiPoE"), title, Year.of(year),price,description, image ); - db.addBook(book); + db.insert(book); resp.sendRedirect("booklist"); } @@ -67,44 +77,48 @@ public class BookList extends HttpServlet { Utillity.insertFile(out, "template_head.html"); - ArrayList books = db.getBooks(); + List books = db.getBooks(); - //book entries - out.println("
"); - for (Book book : books) { + if(books != null) { - out.println("
"); + //book entries + out.println("
"); + for (Book book : books) { - out.println("
"); - out.println(" \"Buchcover\""); - out.println("
"); + out.println("
"); - out.println("
"); - out.println("

" + book.getTitle() + "

"); - out.println("
"); - out.println("

" + book.getAuthor().getName() + "

"); - out.println("
"); - out.print("

" + book.getPrice() / 100 + ","); - if(book.getPrice() % 100 < 10){ - out.print("0"); + out.println("
"); + out.println(" \"Buchcover\""); + out.println("
"); + + out.println("
"); + out.println("

" + book.getTitle() + "

"); + out.println("
"); + for (int i = 0; i < book.getAuthors().size(); i++) { + out.println("

" + book.getAuthors().get(0).getName() + "

"); + } + out.println("
"); + out.print("

" + book.getPrice() / 100 + ","); + if (book.getPrice() % 100 < 10) { + out.print("0"); + } + out.println(book.getPrice() % 100 + " €

"); + out.println("
"); + out.println("
"); + + out.println("
"); + out.println("
"); + + out.println(""); + out.println("Entfernen"); + out.println(""); + + out.println("
"); + out.println("
"); + out.println("

"); } - out.println(book.getPrice() % 100 +" €"); - out.println("
"); - out.println("
"); - - out.println("
"); - out.println("
"); - - out.println(""); - out.println("Entfernen"); - out.println(""); - - out.println("
"); - out.println("
"); - out.println("
"); + out.println("
"); } - out.println("
"); - //add book button Utillity.addButton(out,"Neues Buch Hinzufügen", "addbook");