com.jenkov.mrpersister.itf
Interface IJdbcDao

All Known Implementing Classes:
JdbcDao

public interface IJdbcDao

Represents a DAO capable of simplifying the most ordinary JDBC tasks like reading a long from the database, iterating a ResultSet and executing an update.

Author:
Jakob Jenkov - Copyright 2005 Jenkov Development

Method Summary
 java.lang.Object read(java.lang.String sql, IPreparedStatementManager statementManager, IResultSetProcessor processor)
          Executes the given SQL and calls the IResultSetProcessor's process(...) method for each record in the ResultSet.
 java.lang.Object read(java.lang.String sql, IResultSetProcessor processor)
          Executes the given SQL and calls the IResultSetProcessor's process(...) method for each record in the ResultSet.
 java.lang.Object read(java.lang.String sql, java.lang.Object[] parameters, IResultSetProcessor processor)
          Executes the given SQL and calls the IResultSetProcessor's process(...) method for each record in the ResultSet.
 java.lang.String readIdString(java.lang.String sql)
          Reads all the ids located by the given SQL into an id string that can be used in an SQL select ...
 java.lang.String readIdString(java.lang.String sql, IPreparedStatementManager statementManager)
          Reads all the ids located by the given SQL into an id string that can be used in an SQL select ...
 java.lang.String readIdString(java.lang.String sql, java.lang.Object[] parameters)
          Reads all the ids located by the given SQL into an id string that can be used in an SQL select ...
 java.lang.Long readLong(java.lang.String sql)
          Reads a long from the database using the given SQL query.
 java.lang.Long readLong(java.lang.String sql, java.lang.Object[] parameters)
          Reads a long from the database using the given SQL query.
 int update(java.lang.String sql)
          Executes the given SQL as an update (PreparedStatement.executeUpdate()).
 int update(java.lang.String sql, IPreparedStatementManager statementManager)
          Executes the given SQL as an update (PreparedStatement.executeUpdate()).
 int update(java.lang.String sql, java.lang.Object[] parameters)
          Executes the given SQL as an update (PreparedStatement.executeUpdate()).
 

Method Detail

readLong

java.lang.Long readLong(java.lang.String sql)
                        throws PersistenceException
Reads a long from the database using the given SQL query. The first column of the first record is returned as a long. This method is handy when you need just the id of some record. For instance:

select id from books where title = 'Summer Night Dream'

Parameters:
sql - The SQL that locates the record and column containing the long to read.
Returns:
A long read from the first record and first column in the ResultSet.
Throws:
PersistenceException - If something goes wrong during the read.

readLong

java.lang.Long readLong(java.lang.String sql,
                        java.lang.Object[] parameters)
                        throws PersistenceException
Reads a long from the database using the given SQL query. The first column of the first record is returned as a long. This method is handy when you need just the id of some record. For instance:

select id from books where title = 'Summer Night Dream'

Parameters:
sql - The SQL that locates the record and column containing the long to read. Formatted as an SQL query for a PreparedStatement, with ?-marks for parameters to insert.
parameters - The parameters to insert into the PreparedStatement before executing the SQL. For instance new Object[]{45}
Returns:
A long read from the first record and first column in the ResultSet.
Throws:
PersistenceException - If something goes wrong during the read.

readIdString

java.lang.String readIdString(java.lang.String sql)
                              throws PersistenceException
Reads all the ids located by the given SQL into an id string that can be used in an SQL select ... where in (id1, id2,...) query. This method generates the "(id1, id2, id3, etc.)" String so it is ready to concatenate with a "select * from ... where in" query String. The first column in the ResultSet is what will be interpreted as the id.

Parameters:
sql - The SQL query that locates the ids to add to the id string. For example: select book_id from books where author_id = 2
Returns:
An id string containing the ids concatenated. For instance "(1, 45, 37, 8, 220)"
Throws:
PersistenceException - If anything goes wrong during the read.

readIdString

java.lang.String readIdString(java.lang.String sql,
                              java.lang.Object[] parameters)
                              throws PersistenceException
Reads all the ids located by the given SQL into an id string that can be used in an SQL select ... where in (id1, id2,...) query. This method generates the "(id1, id2, id3, etc.)" String so it is ready to concatenate with a "select * from ... where in" query String. The first column in the ResultSet is what will be interpreted as the id.

Parameters:
sql - The SQL query that locates the ids to add to the id string. For example: select book_id from books where author_id = ?
parameters - The parameters to insert into the PreparedStatement before executing the SQL. For instance new Object[]{45}
Returns:
An id string containing the ids concatenated. For instance "(1, 45, 37, 8, 220)"
Throws:
PersistenceException - If anything goes wrong during the read.

readIdString

java.lang.String readIdString(java.lang.String sql,
                              IPreparedStatementManager statementManager)
                              throws PersistenceException
Reads all the ids located by the given SQL into an id string that can be used in an SQL select ... where in (id1, id2,...) query. This method generates the "(id1, id2, id3, etc.)" String so it is ready to concatenate with a "select * from ... where in" query String. The first column in the ResultSet is what will be interpreted as the id.

Parameters:
sql - The SQL query that locates the ids to add to the id string. For example: select book_id from books where author_id = ?
statementManager - An instance capable of preparing, initializing parameters of, and post-processing the PreparedStatement being used to execute the SQL. It is easiest to extend the PreparedStatementManagerBase which has default implementations for the prepare(...), init(...), execute(...) and postProcess() methods.
Returns:
An id string containing the ids concatenated. For instance "(1, 45, 37, 8, 220)"
Throws:
PersistenceException - If anything goes wrong during the read.

read

java.lang.Object read(java.lang.String sql,
                      IResultSetProcessor processor)
                      throws PersistenceException
Executes the given SQL and calls the IResultSetProcessor's process(...) method for each record in the ResultSet. Before iterating the ResultSet the IResultSetProcessor's init(...) method is called. When the ResultSet is fully iterated the IResultSetProcessor's getResult() method is called. The value returned from getResult() is the value returned from this read(...) method.

Parameters:
sql - The SQL that locates the records to iterate.
processor - The IResultSetProcessor implementation that processes the ResultSet. It is easiest to extend the ResultSetProcessorBase which has empty implementations for init(...), process(...), and getResult(). Then you only have to override the methods you need.
Returns:
Throws:
PersistenceException - If anything goes wrong during the execution of the SQL and the iteration of the ResultSet.

read

java.lang.Object read(java.lang.String sql,
                      java.lang.Object[] parameters,
                      IResultSetProcessor processor)
                      throws PersistenceException
Executes the given SQL and calls the IResultSetProcessor's process(...) method for each record in the ResultSet. Before iterating the ResultSet the IResultSetProcessor's init(...) method is called. When the ResultSet is fully iterated the IResultSetProcessor's getResult() method is called. The value returned from getResult() is the value returned from this read(...) method.

Parameters:
sql - The SQL that locates the records to iterate.
parameters - The parameters to insert into the PreparedStatement before executing the SQL.
processor - The IResultSetProcessor implementation that processes the ResultSet. It is easiest to extend the ResultSetProcessorBase which has empty implementations for init(...), process(...), and getResult(). Then you only have to override the methods you need.
Returns:
Throws:
PersistenceException - If anything goes wrong during the execution of the SQL and the iteration of the ResultSet.

read

java.lang.Object read(java.lang.String sql,
                      IPreparedStatementManager statementManager,
                      IResultSetProcessor processor)
                      throws PersistenceException
Executes the given SQL and calls the IResultSetProcessor's process(...) method for each record in the ResultSet. Before iterating the ResultSet the IResultSetProcessor's init(...) method is called. When the ResultSet is fully iterated the IResultSetProcessor's getResult() method is called. The value returned from getResult() is the value returned from this read(...) method.

Parameters:
sql - The SQL that locates the records to iterate.
statementManager - An instance capable of preparing, initializing parameters of, and post-processing the PreparedStatement being used to execute the SQL. It is easiest to extend the PreparedStatementManagerBase which has default implementations for the prepare(...), init(...), execute(...) and postProcess() methods.
processor - The IResultSetProcessor implementation that processes the ResultSet. It is easiest to extend the ResultSetProcessorBase which has empty implementations for init(...), process(...), and getResult(). Then you only have to override the methods you need.
Returns:
Throws:
PersistenceException - If anything goes wrong during the execution of the SQL and the iteration of the ResultSet.

update

int update(java.lang.String sql)
           throws PersistenceException
Executes the given SQL as an update (PreparedStatement.executeUpdate()). Useful for insert, update and delete statements.

Parameters:
sql - The SQL containing the update.
Returns:
The number of records affected as returned by the PreparedStatement.executeUpdate() method.
Throws:
PersistenceException - If anyting goes wrong during the update.

update

int update(java.lang.String sql,
           java.lang.Object[] parameters)
           throws PersistenceException
Executes the given SQL as an update (PreparedStatement.executeUpdate()). Useful for insert, update and delete statements.

Parameters:
sql - The SQL containing the update.
parameters - The parameters to insert into the PreparedStatement before executing the SQL.
Returns:
The number of records affected as returned by the PreparedStatement.executeUpdate() method.
Throws:
PersistenceException - If anyting goes wrong during the update.

update

int update(java.lang.String sql,
           IPreparedStatementManager statementManager)
           throws PersistenceException
Executes the given SQL as an update (PreparedStatement.executeUpdate()). Useful for insert, update and delete statements.

Parameters:
sql - The SQL containing the update.
statementManager - An instance capable of preparing, initializing parameters of, and post-processing the PreparedStatement being used to execute the SQL. It is easiest to extend the PreparedStatementManagerBase which has default implementations for the prepare(...), init(...), execute(...) and postProcess() methods.
Returns:
The number of records affected as returned by the PreparedStatement.executeUpdate() method.
Throws:
PersistenceException - If anyting goes wrong during the update.