commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bali...@apache.org
Subject cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils DbUtils.java
Date Sat, 22 Feb 2003 08:58:52 GMT
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


Mime
View raw message