Added sqlite support and fixed some issues
This commit is contained in:
parent
78205d97af
commit
fcbaef3e76
26 changed files with 348 additions and 371 deletions
|
|
@ -44,7 +44,8 @@ public class DBConnection implements Closeable{
|
|||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
public enum DBMS{
|
||||
MySQL
|
||||
MySQL,
|
||||
SQLite
|
||||
}
|
||||
// The connection
|
||||
private Connection conn = null;
|
||||
|
|
@ -88,6 +89,17 @@ public class DBConnection implements Closeable{
|
|||
String dbms_name = initDriver(dbms);
|
||||
conn = DriverManager.getConnection ("jdbc:"+dbms_name+"://"+url+"/"+db, user, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Connection to a DB file
|
||||
*
|
||||
* @param dbms is the DB type
|
||||
* @param db is the database to connect to
|
||||
*/
|
||||
public DBConnection(DBMS dbms, String db) throws Exception{
|
||||
String dbms_name = initDriver(dbms);
|
||||
conn = DriverManager.getConnection ("jdbc:"+dbms_name+":"+db);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the underlying connection
|
||||
|
|
@ -105,11 +117,15 @@ public class DBConnection implements Closeable{
|
|||
public String initDriver(DBMS db) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
|
||||
switch(db){
|
||||
case MySQL:
|
||||
Class.forName ("com.mysql.jdbc.Driver").newInstance();
|
||||
Class.forName("com.mysql.jdbc.Driver").newInstance();
|
||||
DriverManager.setLoginTimeout(10);
|
||||
return "mysql";
|
||||
case SQLite:
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
return "sqlite";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -162,16 +178,23 @@ public class DBConnection implements Closeable{
|
|||
* @return update count or -1 if the query is not an update query
|
||||
*/
|
||||
public static int exec(PreparedStatement stmt) throws SQLException {
|
||||
return exec(stmt, new SQLResultHandler<Integer>(){
|
||||
Integer ret = exec(stmt, new SQLResultHandler<Integer>(){
|
||||
public Integer handleQueryResult(Statement stmt, ResultSet result) {
|
||||
try {
|
||||
return stmt.getUpdateCount();
|
||||
if(stmt != null)
|
||||
return stmt.getUpdateCount();
|
||||
else
|
||||
return -1;
|
||||
} catch (SQLException e) {
|
||||
logger.log(Level.WARNING, null, e);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
});
|
||||
|
||||
if(ret != null)
|
||||
return ret;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -203,23 +226,35 @@ public class DBConnection implements Closeable{
|
|||
if( handler != null ){
|
||||
ResultSet result = null;
|
||||
try{
|
||||
result = stmt.getResultSet();
|
||||
return handler.handleQueryResult(stmt, result);
|
||||
if(stmt.getMoreResults()){
|
||||
result = stmt.getResultSet();
|
||||
return handler.handleQueryResult(stmt, result);
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}catch(SQLException sqlex){
|
||||
logger.throwing(null, null, sqlex);
|
||||
}finally{
|
||||
if(result != null){
|
||||
try {
|
||||
result.close();
|
||||
} catch (SQLException e) { }
|
||||
} catch (SQLException sqlex) {
|
||||
logger.throwing(null, null, sqlex);
|
||||
}
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch(SQLException sqlex){
|
||||
logger.throwing(null, null, sqlex);
|
||||
// Cleanup
|
||||
} finally {
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException sqlex) { }
|
||||
} catch (SQLException sqlex) {
|
||||
logger.throwing(null, null, sqlex);
|
||||
}
|
||||
stmt = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,12 +43,13 @@ import java.util.logging.Logger;
|
|||
import zutil.db.DBConnection;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
/**
|
||||
* <XMP>
|
||||
* The class that extends this will be able to save its state to a DB.
|
||||
/**
|
||||
* The class that extends this will be able to save its state to a database.
|
||||
* Fields that are transient will be ignored, and fields that extend
|
||||
* DBBean will be replaced with the id field of that class in the database.
|
||||
* DBBean will be replaced with the an id which corresponds to the field
|
||||
* of that object.
|
||||
*
|
||||
* <XMP>
|
||||
* Supported fields:
|
||||
* *Boolean
|
||||
* *Integer
|
||||
|
|
@ -59,8 +60,7 @@ import zutil.log.LogUtil;
|
|||
* *Character
|
||||
* *DBBean
|
||||
* *java.sql.Timestamp
|
||||
* *List<DBBean>
|
||||
*
|
||||
* *List<DBBean>
|
||||
* </XMP>
|
||||
* @author Ziver
|
||||
*/
|
||||
|
|
@ -340,11 +340,11 @@ public abstract class DBBean {
|
|||
}
|
||||
|
||||
/**
|
||||
* Loads all the rows in the table into a LinkedList
|
||||
* Loads all rows from the table into a LinkedList
|
||||
*
|
||||
* @param <T> is the class of the bean
|
||||
* @param c is the class of the bean
|
||||
* @return a LinkedList with all the Beans in the DB
|
||||
* @return a LinkedList with all the beans in the DB
|
||||
*/
|
||||
public static <T extends DBBean> List<T> load(DBConnection db, Class<T> c) throws SQLException {
|
||||
// Initiate a BeanConfig if there is non
|
||||
|
|
@ -359,12 +359,12 @@ public abstract class DBBean {
|
|||
}
|
||||
|
||||
/**
|
||||
* Loads all the rows in the table into a LinkedList
|
||||
* Loads a specific instance of the bean from the table with the specific id
|
||||
*
|
||||
* @param <T> is the class of the bean
|
||||
* @param c is the class of the bean
|
||||
* @param id is the id value of the bean
|
||||
* @return a DBBean Object with the specific id or null
|
||||
* @return a DBBean Object with the specific id or null if the id was not found
|
||||
*/
|
||||
public static <T extends DBBean> T load(DBConnection db, Class<T> c, long id) throws SQLException {
|
||||
// Initiate a BeanConfig if there is non
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue