diff options
Diffstat (limited to 'MobiReader')
-rw-r--r-- | MobiReader/my_books.db | bin | 0 -> 4096 bytes | |||
-rw-r--r-- | MobiReader/sample.db | bin | 5120 -> 5120 bytes | |||
-rw-r--r-- | MobiReader/src/mobireader/SqliteDatabaseHandler.java | 18 | ||||
-rw-r--r-- | MobiReader/src/mobireader/gui/ui/AppMain.java | 6 | ||||
-rw-r--r-- | MobiReader/test/tests/SqlConnectionJUnitTest.java | 30 |
5 files changed, 47 insertions, 7 deletions
diff --git a/MobiReader/my_books.db b/MobiReader/my_books.db Binary files differnew file mode 100644 index 0000000..408a0ac --- /dev/null +++ b/MobiReader/my_books.db diff --git a/MobiReader/sample.db b/MobiReader/sample.db Binary files differindex de744ad..0798753 100644 --- a/MobiReader/sample.db +++ b/MobiReader/sample.db diff --git a/MobiReader/src/mobireader/SqliteDatabaseHandler.java b/MobiReader/src/mobireader/SqliteDatabaseHandler.java index 7cc16ad..087809e 100644 --- a/MobiReader/src/mobireader/SqliteDatabaseHandler.java +++ b/MobiReader/src/mobireader/SqliteDatabaseHandler.java @@ -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()); } diff --git a/MobiReader/src/mobireader/gui/ui/AppMain.java b/MobiReader/src/mobireader/gui/ui/AppMain.java index 562d972..8022f3a 100644 --- a/MobiReader/src/mobireader/gui/ui/AppMain.java +++ b/MobiReader/src/mobireader/gui/ui/AppMain.java @@ -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() diff --git a/MobiReader/test/tests/SqlConnectionJUnitTest.java b/MobiReader/test/tests/SqlConnectionJUnitTest.java index 4176cc8..89b6365 100644 --- a/MobiReader/test/tests/SqlConnectionJUnitTest.java +++ b/MobiReader/test/tests/SqlConnectionJUnitTest.java @@ -67,6 +67,7 @@ public class SqlConnectionJUnitTest { assertEquals(path, bookFromDB.getPathToContent()); } + @Test public void testAddingFewBooksOfSameAuthorToDb() throws ClassNotFoundException { @@ -92,6 +93,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 { String authorName = "Geronimo"; @@ -103,6 +132,7 @@ public class SqlConnectionJUnitTest { assertEquals(additionalInfo, authorFromDB.getAdditionalInfo()); } + @Test public void testAddingTheSameAuthorToDbFewTimes() throws Exception { |