org.pfsw.db.util
Class SQLExecutor

java.lang.Object
  extended by org.pfsw.db.util.SQLExecutor

public class SQLExecutor
extends java.lang.Object

Provides an easy to use interface to execute SQL statements against a database.


Constructor Summary
SQLExecutor(javax.sql.DataSource aDataSource)
          Initialize the new instance with default values.
 
Method Summary
 boolean close()
          Closes all open connections.
 boolean closeStatement(java.sql.Statement statement)
          Closes the given statement.
protected  java.sql.Connection connection()
           
protected  void connection(java.sql.Connection newValue)
           
 int execute(java.lang.String sql)
          Executes the given SQL command and returns the number of affected rows.
 boolean executeSQL(java.lang.String sql)
          Executes the given SQL command and returns true if the execution was successful.
 int executeWriteStatement(java.sql.PreparedStatement statement)
          Executes the given statement and returns the number of affected rows.
protected  java.sql.Connection getConnection()
           
protected  javax.sql.DataSource getDataSource()
           
protected  boolean isClosed()
           
protected  java.sql.Connection newConnection()
           
 java.sql.PreparedStatement prepareWriteStatement(java.lang.String sql)
          Returns a PreparedStatement for the given SQL command.
protected  void setDataSource(javax.sql.DataSource newValue)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SQLExecutor

public SQLExecutor(javax.sql.DataSource aDataSource)
Initialize the new instance with default values.

Parameters:
aDataSource - The datasource the executor should connect to (must not be null)
Method Detail

prepareWriteStatement

public java.sql.PreparedStatement prepareWriteStatement(java.lang.String sql)
                                                 throws java.sql.SQLException
Returns a PreparedStatement for the given SQL command.

Parameters:
sql - A valid SQL statement with placeholders (?) (no SELECT allowed here!)
Throws:
java.sql.SQLException - Any problem that occurs during execution

executeWriteStatement

public int executeWriteStatement(java.sql.PreparedStatement statement)
                          throws java.sql.SQLException
Executes the given statement and returns the number of affected rows.

Parameters:
statement - A valid statement, created before by this executor
Throws:
java.sql.SQLException - Any problem that occurs during execution

execute

public int execute(java.lang.String sql)
            throws java.sql.SQLException
Executes the given SQL command and returns the number of affected rows.

Parameters:
sql - A valid SQL statement (no SELECT allowed here!)
Throws:
java.sql.SQLException - Any problem that occurs during execution

executeSQL

public boolean executeSQL(java.lang.String sql)
Executes the given SQL command and returns true if the execution was successful.

Parameters:
sql - A valid SQL statement (no SELECT allowed here!)

close

public boolean close()
Closes all open connections. Connections will be reopened automatically if necessary.


closeStatement

public boolean closeStatement(java.sql.Statement statement)
Closes the given statement.

Returns:
true if it is closed.

getConnection

protected java.sql.Connection getConnection()
                                     throws java.sql.SQLException
Throws:
java.sql.SQLException

newConnection

protected java.sql.Connection newConnection()
                                     throws java.sql.SQLException
Throws:
java.sql.SQLException

isClosed

protected boolean isClosed()

getDataSource

protected javax.sql.DataSource getDataSource()

setDataSource

protected void setDataSource(javax.sql.DataSource newValue)

connection

protected java.sql.Connection connection()

connection

protected void connection(java.sql.Connection newValue)