http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java b/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java index cb1773e..934c00b 100644 --- a/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java @@ -22,23 +22,30 @@ import java.sql.Types; /** * This class provides the ability to execute a batch of statements. - * + * * It is really just a facade to an array of UpdateExecutors. - * + * + * @since 2.0 * @author William Speirs <[email protected]> */ public class BatchExecutor extends AbstractExecutor<BatchExecutor> { private final boolean closeConn; - - public BatchExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { + + /** + * Constructs a BatchExecutor given a connection and SQL statement. + * @param conn The connection to use during execution. + * @param sql The SQL statement. + * @param closeConnection If the connection should be closed or not. + * @throws SQLException thrown if there is an error during execution. + */ + BatchExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { super(conn, sql); this.closeConn = closeConnection; } - + /** * Binds a parameter name to a value for a given statement. - * @param statement the statement number to operate on. * @param name the name of the parameter. * @param value the value to bind to the parameter. * @return this object. @@ -49,7 +56,7 @@ public class BatchExecutor extends AbstractExecutor<BatchExecutor> { public BatchExecutor bind(final String name, final Object value) throws SQLException { return bind(name, value, false); } - + /** * Binds null to a parameter. * Types.VARCHAR is used as the type's parameter. @@ -62,7 +69,7 @@ public class BatchExecutor extends AbstractExecutor<BatchExecutor> { public BatchExecutor bindNull(final String name) throws SQLException { return bindNull(name, Types.VARCHAR, false); } - + /** * Binds null to a parameter, specifying the parameter's type. * @param name the name of the parameter. @@ -74,7 +81,7 @@ public class BatchExecutor extends AbstractExecutor<BatchExecutor> { public BatchExecutor bindNull(final String name, final int sqlType) throws SQLException { return bindNull(name, sqlType, false); } - + /** * Adds the statement to the batch after binding all of the parameters. * @return this object. @@ -85,10 +92,10 @@ public class BatchExecutor extends AbstractExecutor<BatchExecutor> { try { getStatement().addBatch(); clearValueMap(); - } catch(SQLException e) { + } catch (SQLException e) { rethrow(e); } - + return this; }
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/DbUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/DbUtils.java b/src/main/java/org/apache/commons/dbutils2/DbUtils.java index 5968d4e..515392f 100644 --- a/src/main/java/org/apache/commons/dbutils2/DbUtils.java +++ b/src/main/java/org/apache/commons/dbutils2/DbUtils.java @@ -17,8 +17,10 @@ package org.apache.commons.dbutils2; import static java.sql.DriverManager.registerDriver; + import java.io.PrintWriter; import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverPropertyInfo; @@ -216,14 +218,27 @@ public final class DbUtils { try { Driver driver = driverConstructor.newInstance(); registerDriver(new DriverProxy(driver)); + } catch (SQLException e) { + return false; + } catch (InstantiationException e) { + return false; + } catch (IllegalAccessException e) { + return false; + } catch (IllegalArgumentException e) { + return false; + } catch (InvocationTargetException e) { + return false; } finally { driverConstructor.setAccessible(isConstructorAccessible); } return true; - } catch (Exception e) { + } catch (ClassNotFoundException e) { + return false; + } catch (NoSuchMethodException e) { + return false; + } catch (SecurityException e) { return false; - } } @@ -396,7 +411,6 @@ public final class DbUtils { /** * Java 1.7 method. */ - @SuppressWarnings("unused") public Logger getParentLogger() throws SQLFeatureNotSupportedException { throw new SQLFeatureNotSupportedException(); } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java b/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java index b85b66e..0904da8 100644 --- a/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java +++ b/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java @@ -28,17 +28,17 @@ import java.util.Arrays; * columns to Java Bean properties. */ public class GenerousBeanProcessor extends BeanProcessor { - + /** * Default constructor. */ public GenerousBeanProcessor() { super(); } - + @Override protected int[] mapColumnsToProperties(final ResultSetMetaData rsmd, - final PropertyDescriptor[] props) throws SQLException { + final PropertyDescriptor[] props) throws SQLException { final int cols = rsmd.getColumnCount(); final int[] columnToProperty = new int[cols + 1]; @@ -46,19 +46,19 @@ public class GenerousBeanProcessor extends BeanProcessor { for (int col = 1; col <= cols; col++) { String columnName = rsmd.getColumnLabel(col); - + if (null == columnName || 0 == columnName.length()) { columnName = rsmd.getColumnName(col); } - - final String generousColumnName = columnName.replace("_",""); + + final String generousColumnName = columnName.replace("_", ""); for (int i = 0; i < props.length; i++) { final String propName = props[i].getName(); - + // see if either the column name, or the generous one matches if (columnName.equalsIgnoreCase(propName) || - generousColumnName.equalsIgnoreCase(propName)) { + generousColumnName.equalsIgnoreCase(propName)) { columnToProperty[col] = i; break; } @@ -67,5 +67,5 @@ public class GenerousBeanProcessor extends BeanProcessor { return columnToProperty; } - + } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java b/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java index 39a6db6..9b5cecb 100644 --- a/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java @@ -21,18 +21,32 @@ import java.sql.ResultSet; import java.sql.SQLException; +/** + * Fluent class for executing inserts. + * + * @since 2.0 + * @author William Speirs <[email protected]> + */ public class InsertExecutor extends AbstractExecutor<InsertExecutor> { private final boolean closeConn; - public InsertExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { + /** + * Constructs an InsertExecutor given a connection and SQL statement. + * @param conn The connection to use during execution. + * @param sql The SQL statement. + * @param closeConnection If the connection should be closed or not. + * @throws SQLException thrown if there is an error during execution. + */ + InsertExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { super(conn, sql); this.closeConn = closeConnection; } /** * Executes the given INSERT SQL statement. - * + * + * @param <T> the type returned by the ResultSetHandler. * @param handler The handler used to create the result object from * the <code>ResultSet</code> of auto-generated keys. * @@ -54,10 +68,10 @@ public class InsertExecutor extends AbstractExecutor<InsertExecutor> { try { // execute the update getStatement().executeUpdate(); - + // get the result set final ResultSet resultSet = getStatement().getGeneratedKeys(); - + // run the handler over the results and return them return handler.handle(resultSet); } catch (SQLException e) { @@ -72,7 +86,7 @@ public class InsertExecutor extends AbstractExecutor<InsertExecutor> { // we get here only if something is thrown return null; } - + /** * Executes the given INSERT SQL statement. * @return the number of rows updated. @@ -93,8 +107,8 @@ public class InsertExecutor extends AbstractExecutor<InsertExecutor> { close(getConnection()); } } - + return 0; // only get here on an error } - + } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java b/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java index 4a7c317..de55919 100644 --- a/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java @@ -22,15 +22,22 @@ import java.sql.SQLException; /** * Fluent class for executing a query. - * + * * @since 2.0 * @author William Speirs <[email protected]> */ class QueryExecutor extends AbstractExecutor<QueryExecutor> { - + private final boolean closeConn; - public QueryExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { + /** + * Constructs a QueryExecutor given a connection and SQL statement. + * @param conn The connection to use during execution. + * @param sql The SQL statement. + * @param closeConnection If the connection should be closed or not. + * @throws SQLException thrown if there is an error during execution. + */ + QueryExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { super(conn, sql); this.closeConn = closeConnection; } @@ -46,7 +53,7 @@ class QueryExecutor extends AbstractExecutor<QueryExecutor> { public <T> T execute(ResultSetHandler<T> handler) throws SQLException { // throw an exception if there are unmapped parameters this.throwIfUnmappedParams(); - + // make sure our handler is not null if (handler == null) { if (closeConn) { http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/QueryRunner.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/QueryRunner.java b/src/main/java/org/apache/commons/dbutils2/QueryRunner.java index 9dbc292..eecb252 100644 --- a/src/main/java/org/apache/commons/dbutils2/QueryRunner.java +++ b/src/main/java/org/apache/commons/dbutils2/QueryRunner.java @@ -50,7 +50,7 @@ public class QueryRunner { public QueryRunner(final DataSource ds) { this.ds = ds; } - + /** * Returns the <code>DataSource</code> this runner is using. * <code>QueryRunner</code> methods always call this method to get the @@ -76,7 +76,7 @@ public class QueryRunner { if (this.getDataSource() == null) { throw new SQLException( "QueryRunner requires a DataSource to be " - + "invoked in this way, or a Connection should be passed in"); + + "invoked in this way, or a Connection should be passed in"); } return this.getDataSource().getConnection(); } @@ -98,10 +98,10 @@ public class QueryRunner { * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the insert will not be saved. The <code>Connection</code> is - * closed after the call. - * + * closed after the call. + * * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.BatchExecutor} for this SQL statement. * @throws SQLException If there are database or parameter errors. */ @@ -115,7 +115,7 @@ public class QueryRunner { * * @param conn The connection to use for the batch call. * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.BatchExecutor} for this SQL statement. * @throws SQLException If there are database or parameter errors. */ @@ -125,11 +125,11 @@ public class QueryRunner { /** * Creates an {@link org.apache.commons.dbutils2.BatchExecutor} for the given SQL statement and connection. - * + * * @param conn The connection to use for the batch call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.BatchExecutor} for this SQL statement. * @throws SQLException If there are database or parameter errors. */ @@ -144,7 +144,7 @@ public class QueryRunner { } throw new SQLException("Null SQL statement"); } - + return new BatchExecutor(conn, sql, closeConn); } @@ -153,10 +153,10 @@ public class QueryRunner { * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the insert will not be saved. The <code>Connection</code> is - * closed after the call. - * + * closed after the call. + * * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.QueryExecutor} for this SQL statement. * @throws SQLException If there are database or parameter errors. */ @@ -167,10 +167,10 @@ public class QueryRunner { /** * Creates an {@link org.apache.commons.dbutils2.QueryExecutor} for the given SQL statement and connection. * The connection is <b>NOT</b> closed after execution. - * + * * @param conn The connection to use for the update call. * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.QueryExecutor} for this SQL statement. * @throws SQLException If there are database or parameter errors. */ @@ -180,11 +180,11 @@ public class QueryRunner { /** * Creates an {@link org.apache.commons.dbutils2.QueryExecutor} for the given SQL statement and connection. - * + * * @param conn The connection to use for the query call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.QueryExecutor} for this SQL statement. * @throws SQLException If there are database or parameter errors. */ @@ -199,7 +199,7 @@ public class QueryRunner { } throw new SQLException("Null SQL statement"); } - + return new QueryExecutor(conn, sql, closeConn); } @@ -208,10 +208,10 @@ public class QueryRunner { * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the insert will not be saved. The <code>Connection</code> is - * closed after the call. + * closed after the call. * * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.UpdateExecutor} for this SQL statement. * @throws SQLException if a database access error occurs */ @@ -222,10 +222,10 @@ public class QueryRunner { /** * Creates an {@link org.apache.commons.dbutils2.UpdateExecutor} for the given SQL statement and connection. * The connection is <b>NOT</b> closed after execution. - * + * * @param conn The connection to use for the update call. * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.UpdateExecutor} for this SQL statement. * @throws SQLException If there are database or parameter errors. */ @@ -235,11 +235,11 @@ public class QueryRunner { /** * Creates an {@link org.apache.commons.dbutils2.UpdateExecutor} for the given SQL statement and connection. - * + * * @param conn The connection to use for the update call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. - * + * * @return An {@link org.apache.commons.dbutils2.UpdateExecutor} for this SQL statement. * @throws SQLException If there are database or parameter errors. */ @@ -263,8 +263,8 @@ public class QueryRunner { * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the insert will not be saved. The <code>Connection</code> is - * closed after the call. - * + * closed after the call. + * * @param sql The SQL statement to execute. * * @return An {@link org.apache.commons.dbutils2.InsertExecutor} for this SQL statement. @@ -277,7 +277,7 @@ public class QueryRunner { /** * Creates an {@link org.apache.commons.dbutils2.InsertExecutor} for the given SQL and connection * The connection is <b>NOT</b> closed after execution. - * + * * @param conn The connection to use for the query call. * @param sql The SQL statement to execute. * @@ -290,7 +290,7 @@ public class QueryRunner { /** * Creates an {@link org.apache.commons.dbutils2.InsertExecutor} for the given SQL and connection. - * + * * @param conn The connection to use for the insert call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. @@ -309,7 +309,7 @@ public class QueryRunner { } throw new SQLException("Null SQL statement"); } - + return new InsertExecutor(conn, sql, closeConn); } } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java b/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java index 590f069..882aa65 100644 --- a/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java @@ -19,12 +19,24 @@ package org.apache.commons.dbutils2; import java.sql.Connection; import java.sql.SQLException; - +/** + * Fluent class for executing updates. + * + * @since 2.0 + * @author William Speirs <[email protected]> + */ public class UpdateExecutor extends AbstractExecutor<UpdateExecutor> { private final boolean closeConn; - - public UpdateExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { + + /** + * Constructs an UpdateExecutor given a connection and SQL statement. + * @param conn The connection to use during execution. + * @param sql The SQL statement. + * @param closeConnection If the connection should be closed or not. + * @throws SQLException thrown if there is an error during execution. + */ + UpdateExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { super(conn, sql); this.closeConn = closeConnection; } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java index f7fe89a..245d2e7 100644 --- a/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java +++ b/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java @@ -55,18 +55,18 @@ public class KeyedHandler<K> extends AbstractKeyedHandler<K, Map<String, Object> * The RowProcessor implementation to use when converting rows * into Objects. */ - protected final RowProcessor convert; + private final RowProcessor convert; /** * The column index to retrieve key values from. Defaults to 1. */ - protected final int columnIndex; + private final int columnIndex; /** * The column name to retrieve key values from. Either columnName or * columnIndex will be used but never both. */ - protected final String columnName; + private final String columnName; /** * Creates a new instance of KeyedHandler. The value of the first column @@ -139,8 +139,8 @@ public class KeyedHandler<K> extends AbstractKeyedHandler<K, Map<String, Object> @Override protected K createKey(ResultSet rs) throws SQLException { return (columnName == null) ? - (K) rs.getObject(columnIndex) : - (K) rs.getObject(columnName); + (K) rs.getObject(columnIndex) : + (K) rs.getObject(columnName); } /** http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/3535b10b/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java b/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java index 7868e66..53e5a95 100644 --- a/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java +++ b/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java @@ -253,7 +253,7 @@ public class SqlNullCheckedResultSet implements InvocationHandler { * @return the value */ public Date getNullDate() { - return this.nullDate; + return new Date(this.nullDate.getTime()); } /** @@ -353,7 +353,7 @@ public class SqlNullCheckedResultSet implements InvocationHandler { * @return the value */ public Timestamp getNullTimestamp() { - return this.nullTimestamp; + return new Timestamp(this.nullTimestamp.getTime()); } /** @@ -380,7 +380,7 @@ public class SqlNullCheckedResultSet implements InvocationHandler { */ @Override public Object invoke(Object proxy, Method method, Object[] args) - throws Throwable { + throws Throwable { Object result = method.invoke(this.rs, args); @@ -389,8 +389,8 @@ public class SqlNullCheckedResultSet implements InvocationHandler { // Check nullMethod != null first so that we don't call wasNull() // before a true getter method was invoked on the ResultSet. return (nullMethod != null && this.rs.wasNull()) - ? nullMethod.invoke(this, (Object[]) null) - : result; + ? nullMethod.invoke(this, (Object[]) null) + : result; } /** @@ -492,7 +492,7 @@ public class SqlNullCheckedResultSet implements InvocationHandler { * @param nullDate the value */ public void setNullDate(Date nullDate) { - this.nullDate = nullDate; + this.nullDate = nullDate != null ? new Date(nullDate.getTime()) : null; } /** @@ -592,7 +592,7 @@ public class SqlNullCheckedResultSet implements InvocationHandler { * @param nullTimestamp the value */ public void setNullTimestamp(Timestamp nullTimestamp) { - this.nullTimestamp = nullTimestamp; + this.nullTimestamp = nullTimestamp != null ? new Timestamp(nullTimestamp.getTime()) : null; } /**
