commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Juozas Baliuka" <bali...@centras.lt>
Subject Re: [jdbc] JdbcUtil etc
Date Sat, 09 Nov 2002 07:39:53 GMT

Hi,
I use callbacks to handle resultsets, possible it can be usefull for this
project.
It eleminates some code dublication and helps to "close"  jdbc resources.

 interface ResultsetHandler {
    boolean handleNext( Connection connection, ResultSet rs );
    //or
      boolean handleNext( ResultSet rs );
     //or
    boolean handleNext( Object[] rs );
     //or
    boolean handleNext( Object result, int index, String name, int
dbcType  );
}

 void execute( String sql, Object[] params,  ResultsetHandler rh )throws
SQLException {
  Connection connection = open();// uses configured connection pool
   try{

    //set parameters and execute statement
     while( rs.next()  ){
        if(! rh.handleNext(rs) ){
            break;
        }
      }


  }finally{

      closeJdbcStuff(rs);//  connection.close(), ...

  }


}






> > 5)
>
> public static Long readLong(ResultSet rs, int index) throws SQLException {
>   Long result = new Long(rs.getLong(index));
>   if (rs.wasNull()) {
>     return null;
>   } else {
>     return result;
>   }
> }
>
> public static void setLong(PreparedStatement pstmt, int index, Long value)
throws SQLException {
>   if (value == null) {
>     pstmt.setNull(index,  Types.NUMBER);
>   } else {
>     pstmt.setLong(index, value.longValue();
>   }
> }
>
> And similar methods for the rest of supported types?
>
> Regards
> Tomek Pik
> pikus@ais.pl
>
> > Hen
>
> --
> To unsubscribe, e-mail:
<mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
<mailto:commons-dev-help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message