Some problems with sql to get all books. I need internet connection to fix it (to read docs).

master
Justyna Ilczuk 2013-04-26 17:20:04 +02:00
parent cb39a2b373
commit 1598363031
2 changed files with 73 additions and 24 deletions

View File

@ -19,6 +19,13 @@ import java.util.ArrayList;
class StatementBuilder class StatementBuilder
{ {
private PreparedStatement prepareStatement(Connection connection, String statementText) throws SQLException
{
PreparedStatement statement = connection.prepareStatement(
statementText);
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public PreparedStatement findAuthorByNameStatement(Connection connection) throws SQLException public PreparedStatement findAuthorByNameStatement(Connection connection) throws SQLException
{ {
PreparedStatement statement = connection.prepareStatement( PreparedStatement statement = connection.prepareStatement(
@ -30,61 +37,61 @@ class StatementBuilder
public PreparedStatement addAuthorStatement(Connection connection) public PreparedStatement addAuthorStatement(Connection connection)
throws SQLException, ClassNotFoundException throws SQLException, ClassNotFoundException
{ {
PreparedStatement statement = connection.prepareStatement( String statementText;
"insert or replace into authors(name, additional_info) values( ?, ?)"); statementText = "insert or replace into authors(name, additional_info) values( ?, ?)";
statement.setQueryTimeout(30); // set timeout to 30 sec. return prepareStatement(connection, statementText);
return statement;
} }
public PreparedStatement addBookStatement(Connection connection) public PreparedStatement addBookStatement(Connection connection)
throws SQLException, ClassNotFoundException throws SQLException, ClassNotFoundException
{ {
PreparedStatement statement = connection.prepareStatement( String statementText = "insert or replace into books(title, author_name, path_to_content) values( ?, ?, ?)";
"insert or replace into books(title, author_name, path_to_content) values( ?, ?, ?)"); return prepareStatement(connection, statementText);
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
} }
public PreparedStatement findBookStatement(Connection connection) public PreparedStatement findBookStatement(Connection connection)
throws SQLException, ClassNotFoundException throws SQLException, ClassNotFoundException
{ {
PreparedStatement statement = connection.prepareStatement( String statementText = "select * from books where title=?";
"select * from books where title=?"); return prepareStatement(connection, statementText);
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
} }
public PreparedStatement findBookByAuthorStatement(Connection connection) public PreparedStatement findBookByAuthorStatement(Connection connection)
throws SQLException, ClassNotFoundException throws SQLException, ClassNotFoundException
{ {
PreparedStatement statement = connection.prepareStatement( String statementText = "select * from books where author_name=?";
"select * from books where author_name=?"); return prepareStatement(connection, statementText);
statement.setQueryTimeout(30); // set timeout to 30 sec. }
return statement;
public PreparedStatement getAllBooksStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
String statementText = "select * from books";
return prepareStatement(connection, statementText);
} }
} }
public class SqliteDatabaseHandler public class SqliteDatabaseHandler
{ {
static String DATABASE_FILE = "sample.db"; String dbFile = "sample.db";
String driver; String driver = "org.sqlite.JDBC";
StatementBuilder statBld = new StatementBuilder(); StatementBuilder statBld = new StatementBuilder();
public SqliteDatabaseHandler(String driver) public SqliteDatabaseHandler(String databaseFile)
{ {
this.driver = driver; this.dbFile = databaseFile;
} }
public SqliteDatabaseHandler() public SqliteDatabaseHandler()
{ {
this.driver = "org.sqlite.JDBC"; this.dbFile = "sample.db";
} }
public Connection prepareConnection() throws ClassNotFoundException, SQLException public Connection prepareConnection() throws ClassNotFoundException, SQLException
{ {
Class.forName(this.driver); Class.forName(this.driver);
return DriverManager.getConnection("jdbc:sqlite:" + DATABASE_FILE); return DriverManager.getConnection("jdbc:sqlite:" + dbFile);
} }
@ -211,6 +218,44 @@ public class SqliteDatabaseHandler
return null; return null;
} }
public ArrayList<Book> getAllBooks(Author author) throws ClassNotFoundException
{
try
{
Connection connection = prepareConnection();
try
{
PreparedStatement stat;
stat = statBld.getAllBooksStatement(connection);
ResultSet rs = stat.executeQuery();
ArrayList<Book> books = new ArrayList<>();
while(rs.next())
{
String path = rs.getString("path_to_content");
String title = rs.getString("title");
books.add(new Book(title, author, path));
}
return books;
}
catch(SQLException e) { System.err.println(e.getMessage()); }
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e) { System.err.println(e.getMessage()); }
}
}
catch (SQLException e)
{
System.err.println("Problem occured during making connection to db");
System.err.println(e.getMessage());
}
return null;
}
public void addAuthor(Author author) throws ClassNotFoundException public void addAuthor(Author author) throws ClassNotFoundException
{ {
try try

View File

@ -103,6 +103,7 @@ import javafx.stage.Popup;
import javafx.stage.PopupBuilder; import javafx.stage.PopupBuilder;
import javafx.stage.Stage; import javafx.stage.Stage;
import mobireader.Book; import mobireader.Book;
import mobireader.SqliteDatabaseHandler;
import mobireader.gui.model.GuiModel; import mobireader.gui.model.GuiModel;
/** /**
@ -120,14 +121,17 @@ public class AppMain extends Application {
Label htmlLabel; Label htmlLabel;
Popup alertPopup; Popup alertPopup;
Tab webViewTab; Tab webViewTab;
List<Book> myBooks = Book.createSomeExamples(); ArrayList<Book> myBooks = Book.createSomeExamples();
SqliteDatabaseHandler db = new SqliteDatabaseHandler("my_books.db");
public static void main(String[] args) { public static void main(String[] args) {
Application.launch(args); Application.launch(args);
} }
@Override @Override
public void start(final Stage primaryStage) { public void start(final Stage primaryStage) throws ClassNotFoundException {
db.addBooks(myBooks);
myBooks = db.getAllBooks();
stage = primaryStage; stage = primaryStage;
Scene scene = SceneBuilder.create() Scene scene = SceneBuilder.create()
.width(800) .width(800)