Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 43469 invoked from network); 22 Feb 2003 08:58:56 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 22 Feb 2003 08:58:56 -0000 Received: (qmail 10308 invoked by uid 97); 22 Feb 2003 09:00:43 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 10301 invoked from network); 22 Feb 2003 09:00:42 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 22 Feb 2003 09:00:42 -0000 Received: (qmail 43216 invoked by uid 500); 22 Feb 2003 08:58:54 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 43205 invoked by uid 500); 22 Feb 2003 08:58:53 -0000 Received: (qmail 43202 invoked from network); 22 Feb 2003 08:58:53 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 22 Feb 2003 08:58:53 -0000 Received: (qmail 58465 invoked by uid 1440); 22 Feb 2003 08:58:52 -0000 Date: 22 Feb 2003 08:58:52 -0000 Message-ID: <20030222085852.58464.qmail@icarus.apache.org> From: baliuka@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils DbUtils.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N baliuka 2003/02/22 00:58:52 Modified: dbutils/src/java/org/apache/commons/dbutils DbUtils.java Log: Made result set handler public Revision Changes Path 1.8 +75 -18 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.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- DbUtils.java 10 Feb 2003 17:55:36 -0000 1.7 +++ DbUtils.java 22 Feb 2003 08:58:52 -0000 1.8 @@ -60,16 +60,17 @@ import java.util.Iterator; import java.util.ArrayList; +import java.util.List; public final class DbUtils { - static interface ResultSetHandler{ + public static interface ResultSetHandler{ public Object handle(ResultSet rs)throws SQLException; } - static Object executeQuery(Connection connection, String query, + public static Object executeQuery(Connection connection, String query, Object[] vals, ResultSetHandler rsh ) throws SQLException { @@ -86,11 +87,7 @@ 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; + rethrow( sqle, query, vals); } return rsh.handle(rs); @@ -102,6 +99,16 @@ } + static void rethrow(SQLException cause, String sql,Object[] vals )throws SQLException{ + + String msg = cause.getMessage() + " in query " + sql + + java.util.Arrays.asList(vals).toString(); + SQLException newsqle = new SQLException(msg); + newsqle.setNextException(cause); + throw newsqle; + + } + static void fillStatement(PreparedStatement stmt, Object[] vals) throws SQLException { if (vals != null) { int size = vals.length; @@ -126,28 +133,26 @@ try { return stmt.executeUpdate(); } catch(SQLException sqle) { - String msg = sqle.getMessage() + " in query " + query + - java.util.Arrays.asList(vals).toString(); - SQLException newsqle = new SQLException(msg); - newsqle.setNextException(sqle); - throw newsqle; + rethrow( sqle, query, vals); + //assert true + return 0; } finally { closeQuietly(stmt); } } - + - /** + /** * Creates a PreparedStatement using the String and Object array, * executes this using the Connection, and returns the results - * inside an Iterator. + * inside an List. * Null values in the Object array will be passed to the driver. */ - public static Iterator executeQuery(Connection connection, String query, + public static List executeListQuery(Connection connection, String query, Object[] vals ) throws SQLException { - return (Iterator)executeQuery(connection,query,vals, + return (List)executeQuery(connection,query,vals, new ResultSetHandler(){ public Object handle(ResultSet rs)throws SQLException{ @@ -155,13 +160,29 @@ while (rs.next()) { list.add(resultSetToArray(rs)); } - return list.iterator(); + return list; } }//ResultSetHandler ); } + + + /** + * 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 executeListQuery(connection, query, vals ).iterator(); + + } + /** * Creates a PreparedStatement using the String and Object array, @@ -195,6 +216,42 @@ } // ResultSetHandler )).intValue(); + + } + +/** + * Creates a PreparedStatement using the String and Object array, + * executes this using the Connection, and returns the result + * as row. + * Null values in the Object array will be passed to the driver. + */ + public static Object[] executeRowQuery(Connection connection, final String query, + final Object[] vals + ) throws SQLException + { + return ((Object[]) executeQuery(connection, query, vals, + + new ResultSetHandler(){ + public Object handle(ResultSet rs)throws SQLException{ + ResultSetMetaData rsmd = rs.getMetaData(); + int count = rsmd.getColumnCount(); + Object row[] = new Object[count]; + + if (rs.next()) { + for(int i = 0; i < count ; i++ ){ + row[i] = rs.getObject(i + 1) ; + } + }else{ + rethrow( new SQLException("No results returned"), query, vals ); + } + if(rs.next()){ + rethrow( new SQLException("Multiple results returned"), query, vals ); + } + return row; + } + } + // ResultSetHandler + )); } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org