mobi_reader/MobiReader/src/mobireader/SqliteDatabaseHandler.java

187 lines
5.5 KiB
Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mobireader;
/**
*
* @author att
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class SqliteDatabaseHandler
{
static String DATABASE_FILE = "sample.db";
String driver;
public SqliteDatabaseHandler(String driver)
{
this.driver = driver;
}
public SqliteDatabaseHandler()
{
this.driver = "org.sqlite.JDBC";
}
public Connection prepareConnection() throws ClassNotFoundException, SQLException
{
Class.forName(this.driver);
return DriverManager.getConnection("jdbc:sqlite:" + DATABASE_FILE);
}
public PreparedStatement prepareAddingBookStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
"insert into books values($next_id, ?, ?, ?)");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public PreparedStatement prepareFindingBookStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
"select * from books where title=?");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public void addBooks(ArrayList<Book> books) throws ClassNotFoundException
{
for(Book book : books)
{
addBook(book);
}
}
public void addBook(Book book) throws ClassNotFoundException
{
try
{
Connection connection = prepareConnection();
try
{
//TODO
//Check if author is in db, if he is, check his id
int authorId = 1;
PreparedStatement stat;
stat= this.prepareAddingBookStatement(connection);
stat.setString(2, book.getTitle());
stat.setInt(3, authorId);
stat.setString(4, book.getPathToContent());
stat.executeUpdate();
}
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());
}
}
public Book findBook(String title) throws ClassNotFoundException
{
try
{
Connection connection = prepareConnection();
try
{
PreparedStatement stat;
stat= this.prepareFindingBookStatement(connection);
stat.setString(1, title);
stat.executeQuery();
ResultSet rs = stat.executeQuery();
int authorId = rs.getInt("author_id");
String path = rs.getString("path_to_content");
Author author = new Author("Gepetto", title);
return new Book(title, author, path);
}
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 Book getBook(Integer id)
{
return Book.exemplaryBook();
}
public static void main () throws Exception
{// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists books");
statement.executeUpdate("create table books (id integer, title string, " +
"author_id integer, path_to_content string)");
statement.executeUpdate("insert into books values(1, 'leo', 1, '/somepath/leo.txt')");
statement.executeUpdate("insert into books values(2, 'yui', 1, '/somepath/yui.txt')");
ResultSet rs = statement.executeQuery("select * from books");
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
}
}