+ Fixed all insert bugs
~ found a weird one in PreparedStatement Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
c9ae2db7e1
commit
d91e4ad5f1
|
@ -13,10 +13,7 @@ import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.time.Year;
|
import java.time.Year;
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -161,13 +158,7 @@ public class Database {
|
||||||
Author author = null;
|
Author author = null;
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
try {
|
try {
|
||||||
while(rs.next()) {
|
if(rs.next())
|
||||||
System.out.println(rs.getString("name"));
|
|
||||||
}
|
|
||||||
rs.beforeFirst();
|
|
||||||
if(!rs.next()) {
|
|
||||||
System.out.println("test");
|
|
||||||
}else
|
|
||||||
author = new Author(rs.getLong("id"), rs.getString("name"));
|
author = new Author(rs.getLong("id"), rs.getString("name"));
|
||||||
entry.getValue().close();
|
entry.getValue().close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
@ -190,10 +181,6 @@ public class Database {
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while(rs.next()) {
|
|
||||||
System.out.println(rs.getLong("id"));
|
|
||||||
}
|
|
||||||
|
|
||||||
rs.beforeFirst();
|
rs.beforeFirst();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -220,7 +207,6 @@ public class Database {
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
Category category = null;
|
Category category = null;
|
||||||
try{
|
try{
|
||||||
System.out.println(rs.getStatement());
|
|
||||||
while(rs.next()) {
|
while(rs.next()) {
|
||||||
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image"));
|
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image"));
|
||||||
}
|
}
|
||||||
|
@ -330,7 +316,7 @@ public class Database {
|
||||||
assert author != null;
|
assert author != null;
|
||||||
authors.add(author);
|
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"),
|
Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"),
|
||||||
rs.getString("image"));
|
rs.getString("image"));
|
||||||
entry.getValue().close();
|
entry.getValue().close();
|
||||||
|
@ -426,7 +412,6 @@ public class Database {
|
||||||
for (Author author : book.getAuthors()) {
|
for (Author author : book.getAuthors()) {
|
||||||
if (author.getId() == 0) {
|
if (author.getId() == 0) {
|
||||||
Author dbAuthor = getAuthor(author.getName());
|
Author dbAuthor = getAuthor(author.getName());
|
||||||
System.out.println(dbAuthor);
|
|
||||||
if (dbAuthor == null) {
|
if (dbAuthor == null) {
|
||||||
insertCount += insert(author);
|
insertCount += insert(author);
|
||||||
dbAuthor = getAuthor(author.getName());
|
dbAuthor = getAuthor(author.getName());
|
||||||
|
@ -463,21 +448,33 @@ public class Database {
|
||||||
assert category != null;
|
assert category != null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(title, description, "
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO " + book + "(isbn, title, description, " +
|
||||||
+ "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
"price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
|
||||||
statement.setString(1, book.getTitle());
|
statement.setLong(1, book.getIsbn());
|
||||||
statement.setString(2, book.getDescription());
|
statement.setString(2, book.getTitle());
|
||||||
statement.setInt(3, book.getPrice());
|
statement.setString(3, book.getDescription());
|
||||||
statement.setInt(4, book.getYear().getValue());
|
statement.setInt(4, book.getPrice());
|
||||||
statement.setLong(5, publisher.getId());
|
statement.setInt(5, book.getYear().getValue());
|
||||||
statement.setLong(6, category.getId());
|
statement.setLong(6, publisher.getId());
|
||||||
statement.setString(7, book.getImage());
|
statement.setLong(7, category.getId());
|
||||||
|
statement.setString(8, book.getImage());
|
||||||
insertCount += statement.executeUpdate();
|
insertCount += statement.executeUpdate();
|
||||||
|
ResultSet resultSet = statement.getGeneratedKeys();
|
||||||
|
long lastId = -1;
|
||||||
|
while(resultSet.next()) {
|
||||||
|
lastId = resultSet.getLong("id");
|
||||||
|
}
|
||||||
|
|
||||||
for (Author author : authors) {
|
for (Author author : authors) {
|
||||||
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
|
//FIXME: Prepared Statement is broken, parameters are null, but they are not
|
||||||
statement.setLong(1, author.getId());
|
String query = "INSERT INTO " + authorBook + " (author, book) VALUES (?, ?);";
|
||||||
statement.setLong(2, book.getIsbn());
|
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();
|
insertCount += authorStatement.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,8 @@ import java.util.List;
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class Book {
|
public class Book {
|
||||||
|
|
||||||
private long isbn;
|
private long id;
|
||||||
|
@NonNull private long isbn;
|
||||||
@NonNull private List<Author> authors;
|
@NonNull private List<Author> authors;
|
||||||
@NonNull private Publisher publisher;
|
@NonNull private Publisher publisher;
|
||||||
@NonNull private Category category;
|
@NonNull private Category category;
|
||||||
|
|
|
@ -16,10 +16,9 @@ public class AddBook extends HttpServlet {
|
||||||
PrintWriter out = resp.getWriter();
|
PrintWriter out = resp.getWriter();
|
||||||
|
|
||||||
Utillity.insertFile(out, "template_head.html");
|
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>");
|
out.println("<form class=\"m-container\" action=\"booklist\" method=post>");
|
||||||
|
Utillity.addInput(out, "ISBN", "isbn");
|
||||||
Utillity.addInput(out,"Titel", "title");
|
Utillity.addInput(out,"Titel", "title");
|
||||||
Utillity.addInput(out,"Author", "author");
|
Utillity.addInput(out,"Author", "author");
|
||||||
Utillity.addInput(out,"Preis", "price", "0");
|
Utillity.addInput(out,"Preis", "price", "0");
|
||||||
|
|
|
@ -11,35 +11,37 @@ 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.nio.charset.StandardCharsets;
|
||||||
import java.time.Year;
|
import java.time.Year;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class BookList extends HttpServlet {
|
public class BookList extends HttpServlet {
|
||||||
|
|
||||||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
|
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||||
|
|
||||||
//get database object
|
//get database object
|
||||||
Database db = Database.getInstance();
|
Database db = Database.getInstance();
|
||||||
|
|
||||||
//print parameter (debug)
|
//print parameters (debug)
|
||||||
Enumeration<String> names = req.getParameterNames();
|
/*Enumeration<String> names = req.getParameterNames();
|
||||||
while (names.hasMoreElements()) {
|
while (names.hasMoreElements()) {
|
||||||
String name = names.nextElement();
|
String name = names.nextElement();
|
||||||
System.out.println(name + "=" + req.getParameter(name));
|
System.out.println(name + "=" + req.getParameter(name));
|
||||||
}
|
}*/
|
||||||
|
|
||||||
//remove book by id
|
//remove book by id
|
||||||
String param = req.getParameter("removeid");
|
String param = req.getParameter("removeid");
|
||||||
if(param != null){
|
if (param != null && Long.parseLong(param) != 0) {
|
||||||
long isbn = Long.parseLong(param);
|
long isbn = Long.parseLong(param);
|
||||||
db.deleteBook(isbn);
|
db.deleteBook(isbn);
|
||||||
resp.sendRedirect("booklist");
|
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 title = req.getParameter("title");
|
||||||
String authorName = req.getParameter("author");
|
String authorName = req.getParameter("author");
|
||||||
int price = Integer.parseInt(req.getParameter("price"));
|
int price = Integer.parseInt(req.getParameter("price"));
|
||||||
|
@ -57,7 +59,7 @@ public class BookList extends HttpServlet {
|
||||||
}
|
}
|
||||||
Category category = new Category();
|
Category category = new Category();
|
||||||
category.setName(categoryName);
|
category.setName(categoryName);
|
||||||
Book book = new Book(
|
Book book = new Book(isbn,
|
||||||
authors,
|
authors,
|
||||||
new Publisher(publisher),
|
new Publisher(publisher),
|
||||||
category,
|
category,
|
||||||
|
@ -107,7 +109,7 @@ public class BookList extends HttpServlet {
|
||||||
out.println(" <div class=\"m-col-l-4\">");
|
out.println(" <div class=\"m-col-l-4\">");
|
||||||
out.println(" <div class=\"m-button m-danger\">");
|
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("Entfernen");
|
||||||
out.println("</a>");
|
out.println("</a>");
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
CREATE TABLE IF NOT EXISTS test(id bigint primary key auto_increment, hello varchar(25));
|
CREATE TABLE IF NOT EXISTS test(id bigint primary key auto_increment, hello varchar(25));
|
||||||
INSERT IGNORE INTO test(hello) values ('Welt');
|
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 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(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));
|
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));
|
||||||
|
|
Loading…
Reference in New Issue