commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils DbUtils.java
Date Mon, 10 Feb 2003 17:55:36 GMT
bayard      2003/02/10 09:55:36

  Modified:    dbutils/src/java/org/apache/commons/dbutils DbUtils.java
  Log:
  Refactored fillStatement out,though I've not made it public yet.
  Added an executeUpdate.
  
  Revision  Changes    Path
  1.7       +37 -12    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DbUtils.java	14 Jan 2003 18:20:55 -0000	1.6
  +++ DbUtils.java	10 Feb 2003 17:55:36 -0000	1.7
  @@ -70,7 +70,7 @@
       }
       
       static Object executeQuery(Connection connection, String query, 
  -                                        Object[] vals,ResultSetHandler rsh
  +                                        Object[] vals, ResultSetHandler rsh
                                          ) throws SQLException 
       {
       
  @@ -80,17 +80,8 @@
           try {
                   
               stmt = connection.prepareStatement(query);
  +            fillStatement(stmt, vals);
   
  -            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);
  -                   } 
  -                }
  -            }
               
             try{
                 rs = stmt.executeQuery();
  @@ -110,7 +101,41 @@
           }
       
       }
  -    
  +   
  +    static void fillStatement(PreparedStatement stmt, Object[] vals) throws SQLException
{
  +        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);
  +               } 
  +            }
  +        }
  +    }
  +
  +    public static int executeUpdate(Connection connection, String query, 
  +                                        Object[] vals
  +                                       ) throws SQLException 
  +    {
  +        PreparedStatement stmt = null;
  +
  +        stmt = connection.prepareStatement(query);
  +        fillStatement(stmt, vals);
  +        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;
  +        } finally {
  +            closeQuietly(stmt);
  +        }
  +    }
  +      
       
       /**
        * Creates a PreparedStatement using the String and Object array, 
  
  
  

---------------------------------------------------------------------
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