dgraham 2003/10/19 11:30:24
Modified: dbutils/src/java/org/apache/commons/dbutils DbUtils.java
Log:
Refactored executeUpdate() and executeQuery(), added javadoc, removed
ListAdapter inner class because ArrayListHandler performs the needed
functionality.
Revision Changes Path
1.38 +74 -89
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java
Index: DbUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- DbUtils.java 18 Oct 2003 19:02:22 -0000 1.37
+++ DbUtils.java 19 Oct 2003 18:30:24 -0000 1.38
@@ -68,7 +68,6 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@@ -83,23 +82,6 @@
*/
public final class DbUtils {
- private static abstract class ListAdapter
- extends ArrayList
- implements ResultSetHandler {
-
- public final Object handle(ResultSet rs, Object[] params, Object userObject)
- throws SQLException {
-
- while (rs.next()) {
- add(fetch(rs));
- }
-
- return this;
- }
-
- public abstract Object fetch(ResultSet rs) throws SQLException;
- }
-
public static boolean execute(
Connection conn,
String query,
@@ -116,38 +98,31 @@
stmt = conn.prepareStatement(query);
fillStatement(stmt, vals);
- try {
- if (stmt.execute()) {
- do {
- rs = stmt.getResultSet();
- if (rs != null) {
- if (rsmdh != null) {
- rsmdh.handle(rs.getMetaData());
- }
- }
- rsh.handle(rs, vals, userObject);
-
- } while (stmt.getMoreResults());
-
- return true;
-
- } else {
+ if (!stmt.execute()) {
+ return false;
+ }
- return false;
+ do {
+ rs = stmt.getResultSet();
+ if (rs != null) {
+ if (rsmdh != null) {
+ rsmdh.handle(rs.getMetaData());
+ }
+ rsh.handle(rs, vals, userObject);
}
- } catch (SQLException sqle) {
-
- closeQuietly(rs);
- rethrow(sqle, query, vals);
- }
- return false;
+ } while (stmt.getMoreResults());
+ } catch (SQLException e) {
+ rethrow(e, query, vals);
+
} finally {
+ closeQuietly(rs);
closeQuietly(stmt);
}
-
+
+ return true;
}
public static Object executeQuery(
@@ -172,38 +147,38 @@
PreparedStatement stmt = null;
ResultSet rs = null;
+ Object result = null;
try {
-
stmt = conn.prepareStatement(query);
fillStatement(stmt, vals);
- try {
- rs = stmt.executeQuery();
-
- } catch (SQLException sqle) {
- rethrow(sqle, query, vals);
- }
-
- if (rs != null) {
- if (rsmdh != null) {
- try {
- rsmdh.handle(rs.getMetaData());
- } catch (SQLException sqle) {
- rethrow(sqle, query, vals);
- }
- }
+ rs = stmt.executeQuery();
+
+ if (rsmdh != null) {
+ rsmdh.handle(rs.getMetaData());
}
-
- return rsh.handle(rs, vals, userObject);
+ result = rsh.handle(rs, vals, userObject);
+
+ } catch (SQLException sqle) {
+ rethrow(sqle, query, vals);
} finally {
closeQuietly(rs);
closeQuietly(stmt);
}
+ return result;
}
+ /**
+ * Throws a new exception with a more informative error message.
+ * @param cause The original exception that will be chained to the new
+ * exception when it's rethrown.
+ * @param sql The query that was executing when the exception happened.
+ * @param vals The query replacement paramaters.
+ * @throws SQLException
+ */
private static void rethrow(SQLException cause, String sql, Object[] vals)
throws SQLException {
@@ -218,38 +193,56 @@
throw newsqle;
}
- private static void fillStatement(PreparedStatement stmt, Object[] vals)
+ /**
+ * Fill the <code>PreparedStatement</code> replacement parameters with
+ * the given objects.
+ * @param stmt
+ * @param params
+ * @throws SQLException
+ */
+ private static void fillStatement(PreparedStatement stmt, Object[] params)
throws SQLException {
- if (vals == null) {
+ if (params == null) {
return;
}
- for (int i = 0; i < vals.length; i++) {
- if (vals[i] != null) {
- stmt.setObject((i + 1), vals[i]);
+ for (int i = 0; i < params.length; i++) {
+ if (params[i] != null) {
+ stmt.setObject(i + 1, params[i]);
} else {
- stmt.setNull((i + 1), Types.OTHER);
+ stmt.setNull(i + 1, Types.OTHER);
}
}
-
}
- public static int executeUpdate(Connection connection, String query,Object[]
vals)
- throws SQLException {
- PreparedStatement stmt = null;
+ /**
+ * Execute an SQL INSERT, UPDATE, or DELETE query.
+ * @param conn The connection to use to run the query.
+ * @param query The SQL to execute.
+ * @param params The query replacement parameters.
+ * @return The number of rows updated.
+ * @throws SQLException
+ */
+ public static int executeUpdate(Connection conn, String query, Object[] params)
+ throws SQLException {
+
+ PreparedStatement stmt = conn.prepareStatement(query);
+ fillStatement(stmt, params);
+
+ int rows = 0;
- stmt = connection.prepareStatement(query);
- fillStatement(stmt, vals);
try {
- return stmt.executeUpdate();
- } catch(SQLException sqle) {
- rethrow( sqle, query, vals);
- //assert true
- return 0;
+ rows = stmt.executeUpdate();
+
+ } catch (SQLException e) {
+ rethrow(e, query, params);
+
} finally {
closeQuietly(stmt);
}
+
+ return rows;
}
public static List executeListQuery(
@@ -275,16 +268,8 @@
ResultSetMetaDataHandler rsmdh,
Object userObject)
throws SQLException {
-
- ListAdapter la = new ListAdapter() {
- private RowProcessor convert = BasicRowProcessor.instance();
-
- public Object fetch(ResultSet rs) throws SQLException {
- return convert.toArray(rs);
- }
- };
- return (List) executeQuery(conn, query, vals, la, rsmdh, userObject);
+ return (List) executeQuery(conn, query, vals, new ArrayListHandler(),
rsmdh, userObject);
}
public static Iterator executeQuery(
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]