bayard 2002/11/21 15:17:55
Modified: dbutils/src/java/org/apache/commons/dbutils DbUtils.java
Log:
Added in executeQuery method.
Submitted by: Eric Alexander <[EMAIL PROTECTED]>
Revision Changes Path
1.4 +64 -31
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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DbUtils.java 10 Nov 2002 07:33:36 -0000 1.3
+++ DbUtils.java 21 Nov 2002 23:17:55 -0000 1.4
@@ -56,47 +56,80 @@
*/
package org.apache.commons.dbutils;
-import java.sql.Connection;
-import java.sql.Statement;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
+import java.sql.*;
import java.util.Iterator;
+import java.util.ArrayList;
public final class DbUtils {
/**
- * Ensures that a database driver class is loaded.
+ * Creates a PreparedStatement using the String and Object array,
+ * executes this using the Connection, and returns the results
+ * inside an Iterator.
+ * Null values in the Object array will be passed to the driver.
+ */
+ public static Iterator executeQuery(Connection connection, String query,
+ Object[] vals
+ ) throws SQLException
+ {
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+
+ try {
+ stmt = connection.prepareStatement(query);
+
+ if (vals != null) {
+ int size = vals.length;
+ for (int i = 0; i < size; i++) {
+ stmt.setObject((i + 1), vals[i]);
+ }
+ }
+ rs = stmt.executeQuery();
+
+ ArrayList list = new ArrayList();
+ while (rs.next()) {
+ list.add(resultSetToArray(rs));
+ }
+ return list.iterator();
+ } finally {
+ closeQuietly(rs);
+ closeQuietly(stmt);
+ }
+
+ }
+
+ /**
+ * Ensures that a database driver class is loaded.
* If this succeeds, then it returns true, else it returns false.
*/
public static boolean ensureLoaded(String name) {
try {
Class.forName(name).newInstance();
return true;
- } catch(ClassNotFoundException cnfe) {
+ } catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return false;
- } catch(IllegalAccessException iae) {
+ } catch (IllegalAccessException iae) {
iae.printStackTrace();
return false;
- } catch(InstantiationException ie) {
+ } catch (InstantiationException ie) {
ie.printStackTrace();
return false;
}
}
/**
- * Create an Object array from a ResultSet.
+ * Create an Object array from a ResultSet.
* It is assumed that next() has already been called on the ResultSet.
*/
public static Object[] resultSetToArray(ResultSet rs) throws SQLException {
ResultSetMetaData meta = rs.getMetaData();
int sz = meta.getColumnCount();
Object[] objs = new Object[sz];
- for(int i=0; i<sz; i++) {
- Object obj = rs.getObject(i+1);
- if(rs.wasNull()) {
+ for (int i = 0; i < sz; i++) {
+ Object obj = rs.getObject(i + 1);
+ if (rs.wasNull()) {
obj = null;
}
objs[i] = obj;
@@ -105,9 +138,9 @@
}
/**
- * Iterate over a result set, getting an Object[] back
+ * Iterate over a result set, getting an Object[] back
* for each row of the result set.
- * This version uses JDBC-2 code, which Oracle 9i fails
+ * This version uses JDBC-2 code, which Oracle 9i fails
* to support.
*/
public static Iterator iterateResultSet(ResultSet rs) {
@@ -115,7 +148,7 @@
}
/**
- * Iterate over a result set, getting an Object[] back
+ * Iterate over a result set, getting an Object[] back
* for each row of the result set.
* This version uses JDBC-1 code.
*/
@@ -124,67 +157,67 @@
}
/**
- * Close a connection, avoid closing if null.
+ * Close a connection, avoid closing if null.
*/
public static void close(Connection conn) throws SQLException {
- if(conn == null) {
+ if (conn == null) {
return;
}
conn.close();
}
/**
- * Close a statement, avoid closing if null.
+ * Close a statement, avoid closing if null.
*/
public static void close(Statement stat) throws SQLException {
- if(stat == null) {
+ if (stat == null) {
return;
}
stat.close();
}
/**
- * Close a result set, avoid closing if null.
+ * Close a result set, avoid closing if null.
*/
public static void close(ResultSet rs) throws SQLException {
- if(rs == null) {
+ if (rs == null) {
return;
}
rs.close();
}
/**
- * Close a connection, avoid closing if null and hide
- * any exceptions that occur.
+ * Close a connection, avoid closing if null and hide
+ * any exceptions that occur.
*/
public static void closeQuietly(Connection conn) {
try {
close(conn);
- } catch(SQLException sqle) {
+ } catch (SQLException sqle) {
// quiet
}
}
/**
- * Close a statement, avoid closing if null and hide
- * any exceptions that occur.
+ * Close a statement, avoid closing if null and hide
+ * any exceptions that occur.
*/
public static void closeQuietly(Statement stat) {
try {
close(stat);
- } catch(SQLException sqle) {
+ } catch (SQLException sqle) {
// quiet
}
}
/**
- * Close a result set, avoid closing if null and hide
- * any exceptions that occur.
+ * Close a result set, avoid closing if null and hide
+ * any exceptions that occur.
*/
public static void closeQuietly(ResultSet rs) {
try {
close(rs);
- } catch(SQLException sqle) {
+ } catch (SQLException sqle) {
// quiet
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>