diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/db/MysqlStatement.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/db/MysqlStatement.java | 77 |
1 files changed, 51 insertions, 26 deletions
diff --git a/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java b/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java index 3dda36a..f2b80a3 100644 --- a/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java +++ b/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java @@ -5,6 +5,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; @@ -15,8 +16,8 @@ import java.util.Map; /** * Class for creating {@link PreparedStatement}s with named parameters. Based on * <a href= - * "http://www.javaworld.com/article/2077706/core-java/named-parameters-for-preparedstatement.html?page=2" - * >Named Parameters for PreparedStatement</a> + * "http://www.javaworld.com/article/2077706/core-java/named-parameters-for-preparedstatement.html?page=2">Named + * Parameters for PreparedStatement</a> */ public class MysqlStatement implements Closeable { @@ -29,7 +30,7 @@ public class MysqlStatement implements Closeable private final List<ResultSet> openResultSets = new ArrayList<>(); - MysqlStatement( Connection con, String sql, boolean getKeys ) throws SQLException + MysqlStatement( Connection con, String sql ) throws SQLException { PreparsedQuery query; synchronized ( cache ) { @@ -42,11 +43,12 @@ public class MysqlStatement implements Closeable } } this.query = query; - if ( getKeys ) { - this.statement = con.prepareStatement( query.sql, Statement.RETURN_GENERATED_KEYS ); - } else { - this.statement = con.prepareStatement( query.sql ); - } + this.statement = con.prepareStatement( query.sql, Statement.RETURN_GENERATED_KEYS ); + } + + public String getQuery() + { + return query.sql; } /** @@ -208,6 +210,41 @@ public class MysqlStatement implements Closeable } /** + * Retrieves any auto-generated keys created as a result of executing this + * <code>Statement</code> object. If this <code>Statement</code> object did + * not generate any keys, an empty <code>ResultSet</code> + * object is returned. + * + * <p> + * <B>Note:</B>If the columns which represent the auto-generated keys were not specified, + * the JDBC driver implementation will determine the columns which best represent the + * auto-generated keys. + * + * @return a <code>ResultSet</code> object containing the auto-generated key(s) + * generated by the execution of this <code>Statement</code> object + * @exception SQLException if a database access error occurs or + * this method is called on a closed <code>Statement</code> + * @throws SQLFeatureNotSupportedException if the JDBC driver does not support this method + */ + public ResultSet getGeneratedKeys() throws SQLException + { + ResultSet rs = statement.getGeneratedKeys(); + openResultSets.add( rs ); + return rs; + } + + public int lastInsertId() throws SQLException + { + int result = -1; + try ( ResultSet rs = statement.getGeneratedKeys() ) { + if ( rs.next() ) { + result = rs.getInt( 1 ); + } + } + return result; + } + + /** * Closes the statement. * * @see Statement#close() @@ -223,6 +260,11 @@ public class MysqlStatement implements Closeable } } try { + statement.cancel(); + } catch ( SQLException e ) { + // Nothing to do + } + try { statement.close(); } catch ( SQLException e ) { // Nothing to do @@ -252,23 +294,6 @@ public class MysqlStatement implements Closeable return statement.executeBatch(); } - /** - * Get the generated key from the last insert. Assumes that one row was inserted, and the - * generated key is an int. - * - * @return the generated key - * @throws SQLException if no key was generated by this statement - */ - public int getGeneratedKeys() throws SQLException - { - try ( ResultSet generatedKeys = statement.getGeneratedKeys() ) { - if ( generatedKeys.next() ) { - return generatedKeys.getInt( 1 ); - } - throw new SQLException( "Could not obtain generated key" ); - } - } - // static methods private static PreparsedQuery parse( String query ) @@ -319,7 +344,7 @@ public class MysqlStatement implements Closeable indexList = new ArrayList<>(); paramMap.put( name, indexList ); } - indexList.add( new Integer( index ) ); + indexList.add( Integer.valueOf( index ) ); index++; } |