edit book servlet now interacts with database
This commit is contained in:
parent
15dddd5d30
commit
c57fe04916
|
@ -146,6 +146,28 @@ public class Database {
|
||||||
return author;
|
return author;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Author getAuthor(String name) {
|
||||||
|
Author author = null;
|
||||||
|
|
||||||
|
Map.Entry<List<ResultSet>, 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.
|
* gets category by id.
|
||||||
*
|
*
|
||||||
|
@ -562,6 +584,32 @@ public class Database {
|
||||||
return null;
|
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<List<ResultSet>, 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.
|
* gets all specified columns from table.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
package de.hsel.itech.servlet;
|
package de.hsel.itech.servlet;
|
||||||
|
|
||||||
import de.hsel.itech.db.pojo.Book;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class AddBook extends HttpServlet {
|
public class AddBook extends HttpServlet {
|
||||||
|
|
||||||
|
@ -20,7 +17,7 @@ public class AddBook extends HttpServlet {
|
||||||
|
|
||||||
Utillity.insertFile(out, "template_head.html");
|
Utillity.insertFile(out, "template_head.html");
|
||||||
de.hsel.itech.db.Database db = de.hsel.itech.db.Database.getInstance();
|
de.hsel.itech.db.Database db = de.hsel.itech.db.Database.getInstance();
|
||||||
ArrayList<Book> books = db.getBooks();
|
//List<Book> books = db.getBooks();
|
||||||
|
|
||||||
out.println("<form class=\"m-container\" action=\"booklist\" method=post>");
|
out.println("<form class=\"m-container\" action=\"booklist\" method=post>");
|
||||||
Utillity.addInput(out,"Titel", "title");
|
Utillity.addInput(out,"Titel", "title");
|
||||||
|
@ -29,6 +26,7 @@ public class AddBook extends HttpServlet {
|
||||||
Utillity.addInput(out,"Jahr", "year", "2019");
|
Utillity.addInput(out,"Jahr", "year", "2019");
|
||||||
Utillity.addInput(out,"Beschreibung", "description");
|
Utillity.addInput(out,"Beschreibung", "description");
|
||||||
Utillity.addInput(out,"Verlag", "publisher");
|
Utillity.addInput(out,"Verlag", "publisher");
|
||||||
|
Utillity.addInput(out,"Kategorie", "category");
|
||||||
Utillity.addInput(out,"Bild", "image", "DxAzOKSiPoE");
|
Utillity.addInput(out,"Bild", "image", "DxAzOKSiPoE");
|
||||||
Utillity.addTypeButton(out, "Hinzufügen", "submit");
|
Utillity.addTypeButton(out, "Hinzufügen", "submit");
|
||||||
out.println("</form>");
|
out.println("</form>");
|
||||||
|
|
|
@ -13,7 +13,7 @@ import java.io.PrintWriter;
|
||||||
import java.time.Year;
|
import java.time.Year;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Random;
|
import java.util.List;
|
||||||
|
|
||||||
public class BookList extends HttpServlet {
|
public class BookList extends HttpServlet {
|
||||||
|
|
||||||
|
@ -34,13 +34,13 @@ public class BookList extends HttpServlet {
|
||||||
String param = req.getParameter("removeid");
|
String param = req.getParameter("removeid");
|
||||||
if(param != null){
|
if(param != null){
|
||||||
long isbn = Long.parseLong(param);
|
long isbn = Long.parseLong(param);
|
||||||
db.removeBook(isbn);
|
db.deleteBook(isbn);
|
||||||
resp.sendRedirect("booklist");
|
resp.sendRedirect("booklist");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(req.getParameter("title") != null){
|
if(req.getParameter("title") != null){
|
||||||
String title = req.getParameter("title");
|
String title = req.getParameter("title");
|
||||||
String author = req.getParameter("author");
|
String authorName = req.getParameter("author");
|
||||||
int price = Integer.parseInt(req.getParameter("price"));
|
int price = Integer.parseInt(req.getParameter("price"));
|
||||||
int year = Integer.parseInt(req.getParameter("year"));
|
int year = Integer.parseInt(req.getParameter("year"));
|
||||||
String category = req.getParameter("category");
|
String category = req.getParameter("category");
|
||||||
|
@ -48,14 +48,24 @@ public class BookList extends HttpServlet {
|
||||||
String publisher = req.getParameter("publisher");
|
String publisher = req.getParameter("publisher");
|
||||||
String image = req.getParameter("image");
|
String image = req.getParameter("image");
|
||||||
|
|
||||||
|
|
||||||
|
ArrayList<Author> authors = new ArrayList<Author>();
|
||||||
|
Author author = null;
|
||||||
|
|
||||||
|
author = db.getAuthor(authorName);
|
||||||
|
|
||||||
|
if(author == null){
|
||||||
|
author = new Author(authorName);
|
||||||
|
db.insert(author);
|
||||||
|
}
|
||||||
|
authors.add(author);
|
||||||
Book book = new Book(
|
Book book = new Book(
|
||||||
new Random().nextInt(),
|
authors,
|
||||||
new Author(0,author),
|
new Publisher(publisher),
|
||||||
new Publisher(0,publisher),
|
new Category(category, "DxAzOKSiPoE"),
|
||||||
new Category(0,category, "DxAzOKSiPoE"),
|
|
||||||
title, Year.of(year),price,description, image
|
title, Year.of(year),price,description, image
|
||||||
);
|
);
|
||||||
db.addBook(book);
|
db.insert(book);
|
||||||
|
|
||||||
resp.sendRedirect("booklist");
|
resp.sendRedirect("booklist");
|
||||||
}
|
}
|
||||||
|
@ -67,7 +77,9 @@ public class BookList extends HttpServlet {
|
||||||
Utillity.insertFile(out, "template_head.html");
|
Utillity.insertFile(out, "template_head.html");
|
||||||
|
|
||||||
|
|
||||||
ArrayList<Book> books = db.getBooks();
|
List<Book> books = db.getBooks();
|
||||||
|
|
||||||
|
if(books != null) {
|
||||||
|
|
||||||
//book entries
|
//book entries
|
||||||
out.println("<div class=\"m-container\">");
|
out.println("<div class=\"m-container\">");
|
||||||
|
@ -82,13 +94,15 @@ public class BookList extends HttpServlet {
|
||||||
out.println(" <div class=\"m-col-l-4\">");
|
out.println(" <div class=\"m-col-l-4\">");
|
||||||
out.println("<h1>" + book.getTitle() + "</h1>");
|
out.println("<h1>" + book.getTitle() + "</h1>");
|
||||||
out.println(" <br>");
|
out.println(" <br>");
|
||||||
out.println(" <h2>" + book.getAuthor().getName() + "</h2>");
|
for (int i = 0; i < book.getAuthors().size(); i++) {
|
||||||
|
out.println(" <h2>" + book.getAuthors().get(0).getName() + "</h2>");
|
||||||
|
}
|
||||||
out.println(" <br>");
|
out.println(" <br>");
|
||||||
out.print(" <h3>" + book.getPrice() / 100 + ",");
|
out.print(" <h3>" + book.getPrice() / 100 + ",");
|
||||||
if(book.getPrice() % 100 < 10){
|
if (book.getPrice() % 100 < 10) {
|
||||||
out.print("0");
|
out.print("0");
|
||||||
}
|
}
|
||||||
out.println(book.getPrice() % 100 +" €</h3>");
|
out.println(book.getPrice() % 100 + " €</h3>");
|
||||||
out.println(" <br>");
|
out.println(" <br>");
|
||||||
out.println(" </div>");
|
out.println(" </div>");
|
||||||
|
|
||||||
|
@ -104,7 +118,7 @@ public class BookList extends HttpServlet {
|
||||||
out.println(" </div>");
|
out.println(" </div>");
|
||||||
}
|
}
|
||||||
out.println("</div>");
|
out.println("</div>");
|
||||||
|
}
|
||||||
|
|
||||||
//add book button
|
//add book button
|
||||||
Utillity.addButton(out,"Neues Buch Hinzufügen", "addbook");
|
Utillity.addButton(out,"Neues Buch Hinzufügen", "addbook");
|
||||||
|
|
Loading…
Reference in New Issue