com.jenkov.mrpersister.util
Class JdbcUtil

java.lang.Object
  extended by com.jenkov.mrpersister.util.JdbcUtil

public class JdbcUtil
extends java.lang.Object


Constructor Summary
JdbcUtil()
           
 
Method Summary
static void close(java.sql.Connection connection)
          Closes the provided connection, if it is not already closed.
static void close(java.sql.Connection connection, java.sql.Statement statement, java.sql.ResultSet result)
          Closes the ResultSet, then the Statement or PreparedStatement, and finally the Connection in the same sequence as mentioned here.
static void close(IGenericDao dao)
          Closes the connection enclosed in the given IGenericDao instance.
static void close(java.sql.ResultSet result)
          Closes the provided ResultSet.
static void close(java.sql.Statement statement)
          Closes the provided Statement or PreparedStatement.
static void closeIgnore(java.sql.Connection connection)
          Closes the provided connection.
static void closeIgnore(java.sql.Connection connection, java.sql.Statement statement, java.sql.ResultSet result)
          Closes the ResultSet, then the Statement or PreparedStatement, and finally the Connection in the same sequence as mentioned here.
static void closeIgnore(IGenericDao dao)
          Closes the connection enclosed in the given IGenericDao instance.
static void closeIgnore(java.sql.ResultSet result)
          Closes the provided ResultSet.
static void closeIgnore(java.sql.Statement statement)
          Closes the provided Statement or PreparedStatement.
static java.sql.Connection getConnection(java.lang.String driverClass, java.lang.String url, java.lang.String user, java.lang.String password)
          Opens a connection to the specified database using the JDBC driver class , the url, the user name, and the password provided.
static void insertParameter(java.sql.PreparedStatement statement, int index, java.lang.Object parameter)
          Inserts a parameter into a PreparedStatement on the given index.
static void insertParameters(java.sql.PreparedStatement statement, java.util.Collection parameters)
          Inserts all parameters in the collection into the PreparedStatement instance in the sequence their are returned by the collection's iterator.
static void insertParameters(java.sql.PreparedStatement statement, java.lang.Object[] parameters)
          Inserts all parameters in the array into the PreparedStatement instance in the sequence their are located in the array.
static int parameterCount(java.sql.PreparedStatement statement)
           
static java.sql.PreparedStatement prepareStatement(java.sql.Connection connection, java.lang.String sql)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcUtil

public JdbcUtil()
Method Detail

close

public static void close(IGenericDao dao)
                  throws PersistenceException
Closes the connection enclosed in the given IGenericDao instance. If the dao parameter is null, or its enclosed connection reference is null, nothing happens.

Parameters:
dao - The dao to close the enclosed connection of.
Throws:
PersistenceException - If an error occurs when closing the connection.

closeIgnore

public static void closeIgnore(IGenericDao dao)
Closes the connection enclosed in the given IGenericDao instance. If the dao parameter is null, or its enclosed connection reference is null, nothing happens. Any exceptions thrown while closing the connection are ignored.

Parameters:
dao - The dao to close the enclosed connection of.

getConnection

public static java.sql.Connection getConnection(java.lang.String driverClass,
                                                java.lang.String url,
                                                java.lang.String user,
                                                java.lang.String password)
                                         throws java.lang.ClassNotFoundException,
                                                java.sql.SQLException,
                                                java.lang.IllegalAccessException,
                                                java.lang.InstantiationException
Opens a connection to the specified database using the JDBC driver class , the url, the user name, and the password provided.

Parameters:
driverClass - The name of the JDBC driver class.
url - The URL to the database server / JDBC connection URL.
user - The user name to be used to connect to the database.
password - The password of the user to be used to connect to the database.
Returns:
A connection to the database if it could be opened.
Throws:
java.lang.ClassNotFoundException - If the JDBC driver class could not be found (is not on the classpath)
java.sql.SQLException - If something goes wrong during the connection, for instance if the database/catalogue name in the URL is not recognized by the database server.
java.lang.IllegalAccessException - If your application does not have permission to instantiate the database driver.
java.lang.InstantiationException - If an instance of the JDBC driver class could not be created.

close

public static void close(java.sql.Connection connection)
                  throws PersistenceException
Closes the provided connection, if it is not already closed. If you pass a null connection, nothing happens.

Parameters:
connection - The connection to close.
Throws:
PersistenceException - If an SQLException is thrown when calling connection.close()

closeIgnore

public static void closeIgnore(java.sql.Connection connection)
Closes the provided connection. If you provide a null connection nothing happens. Any exceptions thrown when closing the connection are ignored.

Parameters:
connection - The connection to close.

close

public static void close(java.sql.Statement statement)
                  throws PersistenceException
Closes the provided Statement or PreparedStatement. If you provide a null statement nothing happens.

Parameters:
statement - The Statement or PreparedStatement to close.
Throws:
PersistenceException - If an SQLException is thrown when calling statement.close()

closeIgnore

public static void closeIgnore(java.sql.Statement statement)
Closes the provided Statement or PreparedStatement. If you provide a null statement nothing happens. Any exceptions thrown while closing the Statement or PreparedStatement are ignored.

Parameters:
statement - The Statement or PreparedStatement to close.

close

public static void close(java.sql.ResultSet result)
                  throws PersistenceException
Closes the provided ResultSet. If you provide a null result set nothing happens.

Parameters:
result - The ResultSet to close.
Throws:
PersistenceException - If an SQLException is thrown when calling result.close()

closeIgnore

public static void closeIgnore(java.sql.ResultSet result)
Closes the provided ResultSet. If you provide a null result set nothing happens. Any exceptions thrown when closing the ResultSet are ignored.

Parameters:
result - The ResultSet to close.

close

public static void close(java.sql.Connection connection,
                         java.sql.Statement statement,
                         java.sql.ResultSet result)
                  throws PersistenceException
Closes the ResultSet, then the Statement or PreparedStatement, and finally the Connection in the same sequence as mentioned here. If any of the parameters are null they will be ignored (not attempted closed).

Parameters:
connection - The Connection to close.
statement - The Statement or PreparedStatement to close.
result - The ResultSet to close.
Throws:
PersistenceException - If one or more SQLExceptions are thrown when closing the result set, statement or connection. The error messages from all thrown exceptions are collected and included in the one PersistenceException that is thrown.

closeIgnore

public static void closeIgnore(java.sql.Connection connection,
                               java.sql.Statement statement,
                               java.sql.ResultSet result)
Closes the ResultSet, then the Statement or PreparedStatement, and finally the Connection in the same sequence as mentioned here. If any of the parameters are null they will be ignored (not attempted closed). All exceptions thrown are ignored.

Parameters:
connection - The Connection to close.
statement - The Statement or PreparedStatement to close.
result - The ResultSet to close.

insertParameter

public static void insertParameter(java.sql.PreparedStatement statement,
                                   int index,
                                   java.lang.Object parameter)
                            throws PersistenceException
Inserts a parameter into a PreparedStatement on the given index. This method will try to determine what class the parameter is instance of, and call the coresponding setter method on the PreparedStatement.

Parameters:
statement - The PreparedStatement to insert the parameter into.
parameter - The parameter to be inserted.
index - The index of the parameter in the PreparedStatement.
Throws:
PersistenceException - If anything goes wrong when calling the setter method on the PreparedStatement instance.
java.lang.IllegalArgumentException - If the parameter class is not supported.

insertParameters

public static void insertParameters(java.sql.PreparedStatement statement,
                                    java.util.Collection parameters)
                             throws PersistenceException
Inserts all parameters in the collection into the PreparedStatement instance in the sequence their are returned by the collection's iterator.

Parameters:
statement - The PreparedStatement to insert the parameters into.
parameters - The parameters to insert.
Throws:
PersistenceException - If anything goes wrong during the insertion of the parameters.

insertParameters

public static void insertParameters(java.sql.PreparedStatement statement,
                                    java.lang.Object[] parameters)
                             throws PersistenceException
Inserts all parameters in the array into the PreparedStatement instance in the sequence their are located in the array.

Parameters:
statement - The PreparedStatement to insert the parameters into
parameters - The parameters to insert.
Throws:
PersistenceException - If anything goes wrong during the insertion of the parameters.

prepareStatement

public static java.sql.PreparedStatement prepareStatement(java.sql.Connection connection,
                                                          java.lang.String sql)
                                                   throws PersistenceException
Throws:
PersistenceException

parameterCount

public static int parameterCount(java.sql.PreparedStatement statement)
                          throws PersistenceException
Throws:
PersistenceException