diff --git a/Zutil.jar b/Zutil.jar index 54a3c41..f20f212 100755 Binary files a/Zutil.jar and b/Zutil.jar differ diff --git a/src/zutil/db/DBConnection.java b/src/zutil/db/DBConnection.java index dc38bc7..f606673 100755 --- a/src/zutil/db/DBConnection.java +++ b/src/zutil/db/DBConnection.java @@ -43,8 +43,9 @@ public class DBConnection implements Closeable{ MySQL, SQLite } + // The connection - private Connection conn = null; + private Connection conn; // The pool that this connection belongs to private DBConnectionPool pool; @@ -57,8 +58,8 @@ public class DBConnection implements Closeable{ public DBConnection(String jndi) throws NamingException, SQLException{ InitialContext ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/"+jndi); - conn = ds.getConnection(); - } + this.conn = ds.getConnection(); + } /** * Creates an Connection to a MySQL server @@ -110,7 +111,7 @@ public class DBConnection implements Closeable{ * @param db is the DB type * @return the protocol name of the DBMS */ - public String initDriver(DBMS db) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ + public static String initDriver(DBMS db) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ switch(db){ case MySQL: Class.forName("com.mysql.jdbc.Driver").newInstance(); @@ -129,12 +130,35 @@ public class DBConnection implements Closeable{ */ public long getLastInsertID(){ try{ - return exec("SELECT LAST_INSERT_ID()", new SimpleSQLResult()).longValue(); + return exec("SELECT LAST_INSERT_ID()", new SimpleSQLResult()); }catch(SQLException e){ logger.log(Level.WARNING, null, e); } return -1; } + /** + * @return the last inserted id or -1 if there was an error + */ + public long getLastInsertID(Statement stmt){ + ResultSet result = null; + try{ + result = stmt.getGeneratedKeys(); + if(result != null){ + return new SimpleSQLResult().handleQueryResult(stmt, result); + } + }catch(SQLException e){ + logger.log(Level.WARNING, null, e); + } finally { + if(result != null) { + try { + result.close(); + } catch (SQLException e) { + logger.log(Level.WARNING, null, e); + } + } + } + return -1; + } /** * Runs a Prepared Statement.
diff --git a/src/zutil/db/bean/DBBean.java b/src/zutil/db/bean/DBBean.java index d662215..35ae080 100755 --- a/src/zutil/db/bean/DBBean.java +++ b/src/zutil/db/bean/DBBean.java @@ -212,28 +212,44 @@ public abstract class DBBean { Long id = this.getId(); // Generate the SQL StringBuilder query = new StringBuilder(); - if( id == null ) - query.append("INSERT INTO "); - else query.append("UPDATE "); - query.append( config.tableName ); + if( id == null ) { + query.append("INSERT INTO ").append( config.tableName ); + query.append( " (" ); + for( Field field : config.fields ){ + if( !List.class.isAssignableFrom(field.getType()) ){ + query.append(" "); + query.append(field.getName()); + query.append(","); + } + } + if(query.charAt(query.length()-1) == ',') + query.deleteCharAt(query.length()-1); + query.append( ") VALUES(" ); + for( Field field : config.fields ){ + query.append( "?," ); + } + if(query.charAt(query.length()-1) == ',') + query.deleteCharAt(query.length()-1); + query.append( ")" ); + } + else{ + query.append("UPDATE ").append( config.tableName ); + query.append( " SET" ); + for( Field field : config.fields ){ + if( !List.class.isAssignableFrom(field.getType()) ){ + query.append(" "); + query.append(field.getName()); + query.append("=?,"); + } + } + if(query.charAt(query.length()-1) == ',') + query.deleteCharAt(query.length()-1); + query.append(" WHERE ").append(config.idColumn).append("=?"); + } - StringBuilder params = new StringBuilder(); - for( Field field : config.fields ){ - if( !List.class.isAssignableFrom(field.getType()) ){ - params.append(" "); - params.append(field.getName()); - params.append("=?,"); - } - } - if( params.length() > 0 ){ - params.delete( params.length()-1, params.length()); - query.append( " SET" ); - query.append( params ); - if( id != null ) - query.append(" WHERE ").append(config.idColumn).append("=?"); - } - logger.finest("Save query("+c.getName()+" id:"+this.getId()+"): "+query.toString()); - PreparedStatement stmt = db.getPreparedStatement( query.toString() ); + String sql = query.toString(); + logger.finest("Save query("+c.getName()+" id:"+this.getId()+"): "+ sql); + PreparedStatement stmt = db.getPreparedStatement( sql ); // Put in the variables in the SQL int index = 1; for(Field field : config.fields){ @@ -253,7 +269,7 @@ public abstract class DBBean { // A list of DBBeans else if( List.class.isAssignableFrom( field.getType() ) && field.getAnnotation( DBLinkTable.class ) != null){ - // DO NOTING + // Du stuff later } // Normal field else{ @@ -268,7 +284,7 @@ public abstract class DBBean { // Execute the SQL DBConnection.exec(stmt); if( id == null ) - this.id = db.getLastInsertID(); + this.id = db.getLastInsertID(stmt); // Save the list, after we get the object id for(Field field : config.fields){