commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Juozas Baliuka" <bali...@centras.lt>
Subject Re: [dbutils] public access to ResultSetHandler
Date Fri, 21 Feb 2003 16:08:25 GMT
Hi,
It will be public if we will not find better ways to handle resultsets.
 I am not sure about "connection" parameter too,
connection can be stored in ThreadLoacal, but this will add more "public
API" to component.
It is possible to live without "cln" parameter, handler can be implemented
this way :

class MyHandler implemente ResultSetHandler{
       Collection collection;
       MyHandler(Collection collection){
          this.collection = collection;
       }
      public boolean handle( ResultSet rs ){
         collection.add(rs.getObject(1));
        return true;
     }

}

    static final  ThreadLoacal CURRENT_CONNECTION = new  ThreadLoacal();

     static void executeQuery(String query,Object[] vals, ResultSetHandler
rsh ) throws RuntimeSQLException
             // SQLException has no info and it is no ways to handle it in
user code, SQLSTATE ?
     {

           Connection connection = (Connection)CURRENT_CONNECTION.get();
           if( connection == null ){
                connection = ConnectionFactory.getConnection(); //pluggable
connection pool implementation
               CURRENT_CONNECTION.set(connection);
            }
           PreparedStatement stmt = null;
>         ResultSet rs = null;
>
>         try {
>
>             stmt = connection.prepareStatement(query);
>             fillStatement(stmt, vals);
>
>             try{
>                 rs = stmt.executeQuery();
>                 while (rs.next()) {

                      if( !rsh.handle(rs) ){
                           break;//break  if handler doe's not want more
results
                        }



>                 }
>
>             }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;
>             }
>
>         } finally {
>             closeQuietly(rs);
>             closeQuietly(stmt);
>         }
>
>     }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>


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