database handling works perfectly now

master
Justyna Ilczuk 2013-04-26 17:58:33 +02:00
parent 1598363031
commit 1db006b285
5 changed files with 47 additions and 7 deletions

BIN
MobiReader/my_books.db Normal file

Binary file not shown.

Binary file not shown.

View File

@ -53,7 +53,9 @@ class StatementBuilder
public PreparedStatement findBookStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
String statementText = "select * from books where title=?";
String statementText = "select b.title, b.author_name, b.path_to_content,"
+ " a.additional_info from books b left join authors a on"
+ " b.author_name = a.name where b.title=?";
return prepareStatement(connection, statementText);
}
@ -67,7 +69,9 @@ class StatementBuilder
public PreparedStatement getAllBooksStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
String statementText = "select * from books";
String statementText = "select b.title, b.author_name, b.path_to_content,"
+ " a.additional_info from books b left join authors a on"
+ " b.author_name = a.name";
return prepareStatement(connection, statementText);
}
@ -156,7 +160,9 @@ public class SqliteDatabaseHandler
stat.executeQuery();
ResultSet rs = stat.executeQuery();
String path = rs.getString("path_to_content");
Author author = new Author("Gepetto", title);
String author_name = rs.getString("author_name");
String info_about_author = rs.getString("additional_info");
Author author = new Author(author_name, info_about_author);
return new Book(title, author, path);
}
catch(SQLException e) { System.err.println(e.getMessage()); }
@ -218,7 +224,7 @@ public class SqliteDatabaseHandler
return null;
}
public ArrayList<Book> getAllBooks(Author author) throws ClassNotFoundException
public ArrayList<Book> getAllBooks() throws ClassNotFoundException
{
try
{
@ -233,8 +239,12 @@ public class SqliteDatabaseHandler
{
String path = rs.getString("path_to_content");
String title = rs.getString("title");
String author_name = rs.getString("author_name");
String info_about_author = rs.getString("additional_info");
Author author = new Author(author_name, info_about_author);
books.add(new Book(title, author, path));
}
rs.close();
return books;
}
catch(SQLException e) { System.err.println(e.getMessage()); }

View File

@ -4,6 +4,7 @@
*/
package mobireader.gui.ui;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
@ -121,7 +122,7 @@ public class AppMain extends Application {
Label htmlLabel;
Popup alertPopup;
Tab webViewTab;
ArrayList<Book> myBooks = Book.createSomeExamples();
ArrayList<Book> myBooks;
SqliteDatabaseHandler db = new SqliteDatabaseHandler("my_books.db");
public static void main(String[] args) {
@ -129,8 +130,7 @@ public class AppMain extends Application {
}
@Override
public void start(final Stage primaryStage) throws ClassNotFoundException {
db.addBooks(myBooks);
public void start(final Stage primaryStage) throws ClassNotFoundException, SQLException {
myBooks = db.getAllBooks();
stage = primaryStage;
Scene scene = SceneBuilder.create()

View File

@ -67,6 +67,7 @@ public class SqlConnectionJUnitTest {
assertEquals(path, bookFromDB.getPathToContent());
}
@Test
public void testAddingFewBooksOfSameAuthorToDb() throws ClassNotFoundException
{
@ -91,6 +92,34 @@ public class SqlConnectionJUnitTest {
}
@Test
public void testGettingAllBooksFromDb() throws ClassNotFoundException
{
String title1 = "Title1";
String title2 = "Title2";
String title3 = "Title3";
String authorName1 = "Gepetto";
String authorName2 = "Geronimo";
Author gepetto = new Author(authorName1);
Author geronimo = new Author(authorName2);
String path = "/somepath/book.txt";
ArrayList<Book> books = new ArrayList<>();
books.add(new Book(title1, gepetto, path));
books.add(new Book(title2, gepetto, path));
books.add(new Book(title3, gepetto, path));
books.add(new Book(title1, geronimo, path));
books.add(new Book(title2, geronimo, path));
books.add(new Book(title3, geronimo, path));
handler.addBooks(books);
ArrayList<Book> booksFromDB = handler.getAllBooks();
assertEquals(books.size(), booksFromDB.size());
for(int i = 0; i < books.size(); i++ )
{
assertEquals(books.get(i).getTitle(), booksFromDB.get(i).getTitle());
assertEquals(books.get(i).getAuthor().getName(), booksFromDB.get(i).getAuthor().getName());
}
}
@Test
public void testAddingAuthorToDb() throws Exception
{
@ -103,6 +132,7 @@ public class SqlConnectionJUnitTest {
assertEquals(additionalInfo, authorFromDB.getAdditionalInfo());
}
@Test
public void testAddingTheSameAuthorToDbFewTimes() throws Exception
{