+ Fixed all insert bugs

~ found a weird one in PreparedStatement

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2019-04-24 21:59:23 +02:00
parent c9ae2db7e1
commit d91e4ad5f1
5 changed files with 47 additions and 48 deletions

View File

@ -13,10 +13,7 @@ import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.time.Year;
import java.util.AbstractMap;
import java.util.ArrayList;
@ -161,13 +158,7 @@ public class Database {
Author author = null;
ResultSet rs = entry.getKey();
try {
while(rs.next()) {
System.out.println(rs.getString("name"));
}
rs.beforeFirst();
if(!rs.next()) {
System.out.println("test");
}else
if(rs.next())
author = new Author(rs.getLong("id"), rs.getString("name"));
entry.getValue().close();
} catch (SQLException e) {
@ -190,10 +181,6 @@ public class Database {
ResultSet rs = entry.getKey();
try {
while(rs.next()) {
System.out.println(rs.getLong("id"));
}
rs.beforeFirst();
} catch (SQLException e) {
e.printStackTrace();
@ -220,7 +207,6 @@ public class Database {
ResultSet rs = entry.getKey();
Category category = null;
try{
System.out.println(rs.getStatement());
while(rs.next()) {
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image"));
}
@ -330,7 +316,7 @@ public class Database {
assert author != null;
authors.add(author);
}
book = new Book(rs.getLong("id"), authors, publisher, category, rs.getString("title"),
book = new Book(rs.getLong("id"), rs.getLong("isbn"), authors, publisher, category, rs.getString("title"),
Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"),
rs.getString("image"));
entry.getValue().close();
@ -426,7 +412,6 @@ public class Database {
for (Author author : book.getAuthors()) {
if (author.getId() == 0) {
Author dbAuthor = getAuthor(author.getName());
System.out.println(dbAuthor);
if (dbAuthor == null) {
insertCount += insert(author);
dbAuthor = getAuthor(author.getName());
@ -463,21 +448,33 @@ public class Database {
assert category != null;
try {
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(title, description, "
+ "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, book.getTitle());
statement.setString(2, book.getDescription());
statement.setInt(3, book.getPrice());
statement.setInt(4, book.getYear().getValue());
statement.setLong(5, publisher.getId());
statement.setLong(6, category.getId());
statement.setString(7, book.getImage());
PreparedStatement statement = connection.prepareStatement("INSERT INTO " + book + "(isbn, title, description, " +
"price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
statement.setLong(1, book.getIsbn());
statement.setString(2, book.getTitle());
statement.setString(3, book.getDescription());
statement.setInt(4, book.getPrice());
statement.setInt(5, book.getYear().getValue());
statement.setLong(6, publisher.getId());
statement.setLong(7, category.getId());
statement.setString(8, book.getImage());
insertCount += statement.executeUpdate();
ResultSet resultSet = statement.getGeneratedKeys();
long lastId = -1;
while(resultSet.next()) {
lastId = resultSet.getLong("id");
}
for (Author author : authors) {
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
statement.setLong(1, author.getId());
statement.setLong(2, book.getIsbn());
//FIXME: Prepared Statement is broken, parameters are null, but they are not
String query = "INSERT INTO " + authorBook + " (author, book) VALUES (?, ?);";
String workingQuery = "INSERT INTO " + authorBook + " (author, book) VALUES (" + author.getId() + ", "+ lastId + ")";
PreparedStatement authorStatement = connection.prepareStatement(workingQuery);
//statement.setLong(1, author.getId());
//statement.setLong(2, lastId);
System.out.println(authorStatement);
System.out.println(author.getId());
System.out.println(lastId);
insertCount += authorStatement.executeUpdate();
}

View File

@ -21,7 +21,8 @@ import java.util.List;
@RequiredArgsConstructor
public class Book {
private long isbn;
private long id;
@NonNull private long isbn;
@NonNull private List<Author> authors;
@NonNull private Publisher publisher;
@NonNull private Category category;

View File

@ -16,10 +16,9 @@ public class AddBook extends HttpServlet {
PrintWriter out = resp.getWriter();
Utillity.insertFile(out, "template_head.html");
de.hsel.itech.db.Database db = de.hsel.itech.db.Database.getInstance();
//List<Book> books = db.getBooks();
out.println("<form class=\"m-container\" action=\"booklist\" method=post>");
Utillity.addInput(out, "ISBN", "isbn");
Utillity.addInput(out,"Titel", "title");
Utillity.addInput(out,"Author", "author");
Utillity.addInput(out,"Preis", "price", "0");

View File

@ -11,35 +11,37 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.time.Year;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class BookList extends HttpServlet {
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
//get database object
Database db = Database.getInstance();
//print parameter (debug)
Enumeration<String> names = req.getParameterNames();
while(names.hasMoreElements()){
//print parameters (debug)
/*Enumeration<String> names = req.getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
System.out.println(name + "=" + req.getParameter(name));
}
}*/
//remove book by id
String param = req.getParameter("removeid");
if(param != null){
if (param != null && Long.parseLong(param) != 0) {
long isbn = Long.parseLong(param);
db.deleteBook(isbn);
resp.sendRedirect("booklist");
}
if(req.getParameter("title") != null){
if (req.getParameter("title") != null) {
long isbn = Long.parseLong(req.getParameter("isbn"));
String title = req.getParameter("title");
String authorName = req.getParameter("author");
int price = Integer.parseInt(req.getParameter("price"));
@ -52,16 +54,16 @@ public class BookList extends HttpServlet {
ArrayList<Author> authors = new ArrayList<>();
String[] authorNames = authorName.split(",");
for(String name : authorNames) {
for (String name : authorNames) {
authors.add(new Author(name));
}
Category category = new Category();
category.setName(categoryName);
Book book = new Book(
Book book = new Book(isbn,
authors,
new Publisher(publisher),
category,
title, Year.of(year),price,description, image
title, Year.of(year), price, description, image
);
db.insert(book);
@ -77,7 +79,7 @@ public class BookList extends HttpServlet {
List<Book> books = db.getBooks();
if(books != null) {
if (books != null) {
//book entries
out.println("<div class=\"m-container\">");
@ -107,7 +109,7 @@ public class BookList extends HttpServlet {
out.println(" <div class=\"m-col-l-4\">");
out.println(" <div class=\"m-button m-danger\">");
out.println("<a href=\"/itech/booklist?removeid=" + book.getIsbn() + "\">");
out.println("<a href=\"/itech/booklist?removeid=" + book.getId() + "\">");
out.println("Entfernen");
out.println("</a>");
@ -119,7 +121,7 @@ public class BookList extends HttpServlet {
}
//add book button
Utillity.addButton(out,"Neues Buch Hinzufügen", "addbook");
Utillity.addButton(out, "Neues Buch Hinzufügen", "addbook");
//footer template
Utillity.insertFile(out, "template_footer.html");
@ -127,7 +129,7 @@ public class BookList extends HttpServlet {
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {
doGet(req,resp);
doGet(req, resp);
}
}

View File

@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS test(id bigint primary key auto_increment, hello varchar(25));
INSERT IGNORE INTO test(hello) values ('Welt');
CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, title varchar(50), description varchar(500), price int, year year, publisher bigint, category bigint, image varchar(11) unique);
CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, isbn bigint unique, title varchar(50), description varchar(500), price int, year year, publisher bigint, category bigint, image varchar(11) unique);
CREATE TABLE IF NOT EXISTS publisher(id bigint primary key auto_increment, name varchar(50) unique);
CREATE TABLE IF NOT EXISTS author(id bigint primary key auto_increment, name varchar(50) unique);
CREATE TABLE IF NOT EXISTS author_book(author bigint, book bigint, primary key(author, book), foreign key (author) REFERENCES author(id), foreign key (book) references book(id));