baliuka 2002/12/01 05:12:54
Modified: dbutils/src/java/org/apache/commons/dbutils DbUtils.java
Log:
added a few static methods to DbUtils
Revision Changes Path
1.5 +123 -15
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.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- DbUtils.java 21 Nov 2002 23:17:55 -0000 1.4
+++ DbUtils.java 1 Dec 2002 13:12:54 -0000 1.5
@@ -63,42 +63,117 @@
public final class DbUtils {
- /**
- * 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
+ static interface ResultSetHandler{
+
+ public Object handle(ResultSet rs)throws SQLException;
+
+ }
+
+ static Object executeQuery(Connection connection, String query,
+ Object[] vals,ResultSetHandler rsh
) 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++) {
+ if( vals[i] != null ){
stmt.setObject((i + 1), vals[i]);
+ }else{
+ stmt.setNull((i + 1), Types.OTHER);
+ }
}
}
- rs = stmt.executeQuery();
-
- ArrayList list = new ArrayList();
- while (rs.next()) {
- list.add(resultSetToArray(rs));
+
+ try{
+ rs = stmt.executeQuery();
+ }catch(SQLException sqle){
+ String msg = sqle.getMessage() + " in query " + query +
+ java.util.Arrays.asList(vals).toString();
+ SQLException e = new SQLException(msg);
+ e.setNextException(sqle);
+ throw e;
}
- return list.iterator();
+
+ return rsh.handle(rs);
+
} finally {
closeQuietly(rs);
closeQuietly(stmt);
}
-
+
+ }
+
+
+ /**
+ * 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
+ {
+ return (Iterator)executeQuery(connection,query,vals,
+
+ new ResultSetHandler(){
+ public Object handle(ResultSet rs)throws SQLException{
+ ArrayList list = new ArrayList();
+ while (rs.next()) {
+ list.add(resultSetToArray(rs));
+ }
+ return list.iterator();
+ }
+ }//ResultSetHandler
+ );
+
+ }
+
+
+ /**
+ * Creates a PreparedStatement using the String and Object array,
+ * executes this using the Connection, and returns the result
+ * as int.
+ * Null values in the Object array will be passed to the driver.
+ */
+ public static int executeIntQuery(Connection connection, final String query,
+ Object[] vals
+ ) throws SQLException
+ {
+ return ((Number) executeQuery(connection, query, vals,
+
+ new ResultSetHandler(){
+ public Object handle(ResultSet rs)throws SQLException{
+ ResultSetMetaData rsmd = rs.getMetaData();
+ if(rsmd.getColumnCount() > 1 ){
+ throw new SQLException("multiple results returned by query " +
query);
+ }
+ Number result = null;
+ if (rs.next()) {
+ result = new Integer( rs.getInt(1) );
+ }else{
+ throw new SQLException("No results returned by query " + query);
+ }
+ if(rs.next()){
+ throw new SQLException("multiple results returned by query " + query);
+ }
+ return result;
+ }
+ }
+ // ResultSetHandler
+ )).intValue();
+
}
+
/**
* Ensures that a database driver class is loaded.
* If this succeeds, then it returns true, else it returns false.
@@ -222,4 +297,37 @@
}
}
+ public static void printStackTrace(SQLException sqle, java.io.PrintStream ps){
+
+ SQLException next = sqle;
+ while( next != null ){
+ next.printStackTrace(ps);
+ next = next.getNextException();
+ }
+
+
+ }
+
+ public static void printStackTrace(SQLException sqle){
+
+ printStackTrace( sqle, System.err );
+
+ }
+
+ public static void printWarnings(Connection connection, java.io.PrintStream
ps){
+ if( connection != null ){
+ try{
+ printStackTrace(connection.getWarnings(), ps);
+ }catch(SQLException sqle){
+ printStackTrace(sqle, ps);
+ }
+ }
+
+ }
+
+ public static void printWarnings(Connection connection ){
+ printWarnings(connection,System.err);
+ }
+
+
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>