org.jcoderz.commons.util
Class DbUtil

java.lang.Object
  extended by org.jcoderz.commons.util.DbUtil

public final class DbUtil
extends Object

Utility class for some DB routines.

Author:
Albrecht Messner

Method Summary
static void close(Connection con)
          Close a DB connection according to resource handling idiom.
static void close(ResultSet resultSet)
          Close a result set according to resource handling idiom.
static void close(Statement stmt)
          Close a statement according to resource handling idiom.
static PreparedStatement getLimitedBatchSizePreparedStatement(PreparedStatement pstmt, int maxBatchSize)
          This method wraps the PreparedStatement given as argument into a delegate object that calls the executeBatch method every maxBatchSizeth time a batch is added to the statement with the addBatch method.
static void registerOracleDriver()
          Registers the oracle JDBC driver.
static void setCharOrNull(PreparedStatement pstmt, int paramIndex, String value)
          Sets a parameter in a PreparedStatement to either a value or null.
static void setClobOrNull(PreparedStatement pstmt, int paramIndex, String value)
          Sets a parameter in a PreparedStatement to either a value or null.
static void setIntegerOrNull(PreparedStatement pstmt, int paramIndex, Integer value)
          Sets a parameter in a PreparedStatement to either a value or null.
static void setLongOrNull(PreparedStatement pstmt, int paramIndex, Long value)
          Sets a parameter in a PreparedStatement to either a value or null.
static void setTimestampOrNull(PreparedStatement pstmt, int paramIndex, Timestamp value)
          Sets a parameter in a PreparedStatement to either a value or null.
static void setVarcharOrNull(PreparedStatement pstmt, int paramIndex, String value)
          Sets a parameter in a PreparedStatement to either a value or null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

close

public static void close(Statement stmt)
Close a statement according to resource handling idiom.

Parameters:
stmt - the statement to be closed

close

public static void close(Connection con)
Close a DB connection according to resource handling idiom.

Parameters:
con - the connection to be closed

close

public static void close(ResultSet resultSet)
Close a result set according to resource handling idiom.

Parameters:
resultSet - the result set to close

setVarcharOrNull

public static void setVarcharOrNull(PreparedStatement pstmt,
                                    int paramIndex,
                                    String value)
                             throws SQLException
Sets a parameter in a PreparedStatement to either a value or null.

Parameters:
pstmt - the prepared statement
paramIndex - the parameter index of the parameter to be set
value - the value to which the parameter must be set
Throws:
SQLException - if a database access error occurs.

setCharOrNull

public static void setCharOrNull(PreparedStatement pstmt,
                                 int paramIndex,
                                 String value)
                          throws SQLException
Sets a parameter in a PreparedStatement to either a value or null.

Parameters:
pstmt - the prepared statement
paramIndex - the parameter index of the parameter to be set
value - the value to which the parameter must be set
Throws:
SQLException - if a database access error occurs.

setClobOrNull

public static void setClobOrNull(PreparedStatement pstmt,
                                 int paramIndex,
                                 String value)
                          throws SQLException
Sets a parameter in a PreparedStatement to either a value or null.

Parameters:
pstmt - the prepared statement
paramIndex - the parameter index of the parameter to be set
value - the value to which the parameter must be set
Throws:
SQLException - if a database access error occurs.

setTimestampOrNull

public static void setTimestampOrNull(PreparedStatement pstmt,
                                      int paramIndex,
                                      Timestamp value)
                               throws SQLException
Sets a parameter in a PreparedStatement to either a value or null.

Parameters:
pstmt - the prepared statement
paramIndex - the parameter index of the parameter to be set
value - the value to which the parameter must be set
Throws:
SQLException - if a database access error occurs.

setIntegerOrNull

public static void setIntegerOrNull(PreparedStatement pstmt,
                                    int paramIndex,
                                    Integer value)
                             throws SQLException
Sets a parameter in a PreparedStatement to either a value or null.

Parameters:
pstmt - the prepared statement
paramIndex - the parameter index of the parameter to be set
value - the value to which the parameter must be set
Throws:
SQLException - if a database access error occurs.

setLongOrNull

public static void setLongOrNull(PreparedStatement pstmt,
                                 int paramIndex,
                                 Long value)
                          throws SQLException
Sets a parameter in a PreparedStatement to either a value or null.

Parameters:
pstmt - the prepared statement
paramIndex - the parameter index of the parameter to be set
value - the value to which the parameter must be set
Throws:
SQLException - if a database access error occurs.

registerOracleDriver

public static void registerOracleDriver()
Registers the oracle JDBC driver. NOTE: only use either in testcases that need a direct JDBC connection or in stand-alone applications, never in the Application Server.


getLimitedBatchSizePreparedStatement

public static PreparedStatement getLimitedBatchSizePreparedStatement(PreparedStatement pstmt,
                                                                     int maxBatchSize)
This method wraps the PreparedStatement given as argument into a delegate object that calls the executeBatch method every maxBatchSizeth time a batch is added to the statement with the addBatch method.

Parameters:
pstmt - the prepared statement to wrap
maxBatchSize - the maximum batch size at which an executeBatch should be called
Returns:
a PreparedStatement implementation that does automatic batch updates


Copyright 2007 The jCoderZ Project.