org.apache.commons.scaffold.sql
Class StatementUtils

java.lang.Object
  extended byorg.apache.commons.scaffold.sql.StatementUtils

public final class StatementUtils
extends java.lang.Object

General purpose SQL Statements.

Version:
$Revision: 1.9 $ $Date: 2004/03/21 21:10:27 $

Constructor Summary
StatementUtils()
           
 
Method Summary
static java.lang.Object createKey(java.lang.String resource, int column, java.lang.String query, java.lang.Object key, java.lang.String update)
          Returns next sequential key for given table.
static int createTable(java.lang.String resource, java.lang.String tableName, java.lang.String tableCreate)
          Create a new database table in an existing database, by sending "CREATE TABLE " and the parameters to the DBMS configured with the ConnectionPool.
static java.sql.ResultSet executeQuery(java.sql.Connection connection, java.lang.String command, java.lang.Object[] parameters)
          Merges a SQL command with an array of parameters.
static int executeUpdate(java.sql.Connection connection, java.lang.String resource, java.lang.String command, java.lang.Object[] parameters)
          Prepares statement using SQL statement and executes with DBMS configured with the ConnectionPool.
static int executeUpdate(java.lang.String command)
          Convenience method for calling executeUpdate(String,String,Object[]); without a parameter list.
static int executeUpdate(java.lang.String resource, java.lang.String command)
          Convenience method for calling executeUpdate(String,String,Object[]); without a parameter list.
static int executeUpdate(java.lang.String resource, java.lang.String command, java.lang.Object parameter)
          Convenience method for calling executeUpdate(String,String,Object[]); with a single Object parameter.
static int executeUpdate(java.lang.String resource, java.lang.String command, java.lang.Object[] parameters)
          Prepares statement using SQL statement and executes with DBMS configured with the ConnectionPool.
static java.util.Collection getCollection(java.lang.String resource, java.lang.Object target, java.lang.String command)
          Convenience method for calling getCollection(String,Object,String,Object[]); without a parameter list.
static java.util.Collection getCollection(java.lang.String resource, java.lang.Object target, java.lang.String command, int key)
          Convenience method for calling getCollection(String,Object,String,Object[]); with a single int parameter.
static java.util.Collection getCollection(java.lang.String resource, java.lang.Object target, java.lang.String command, java.lang.Object key)
          Convenience method for calling getCollection(String,Object,String,Object[]); with a single object parameter.
static java.util.Collection getCollection(java.lang.String resource, java.lang.Object target, java.lang.String command, java.lang.Object[] parameters)
          Merges a SQL command with an array of parameters.
static java.lang.Object getColumn(java.lang.String resource, int column, java.lang.String command)
          Convenience method for calling getColumn(String,Object,String,Object[]); without a parameter list.
static java.lang.Object getColumn(java.lang.String resource, int column, java.lang.String command, int key)
          Convenience method for calling getColumn(String,Object,String,Object[]); with a single int parameter.
static java.lang.Object getColumn(java.lang.String resource, int column, java.lang.String command, java.lang.Object key)
          Convenience method for calling getColumn(String,int,String,Object[]); with a single object parameter.
static java.lang.Object getColumn(java.lang.String resource, int column, java.lang.String command, java.lang.Object[] parameters)
          Merges a SQL command with an array of parameters and returns a column from the first record of the result set as an Object.
static boolean getElement(java.lang.String resource, java.lang.Object target, java.lang.String command)
          Convenience method for calling getElement(String,Object,String,Object[]); without a parameter list.
static boolean getElement(java.lang.String resource, java.lang.Object target, java.lang.String command, int key)
          Convenience method for calling getElement(String,Object,String,Object[]); with a single int parameter.
static boolean getElement(java.lang.String resource, java.lang.Object target, java.lang.String command, java.lang.Object key)
          Convenience method for calling getElement(String,Object,String,Object[]); with a single object parameter.
static boolean getElement(java.lang.String resource, java.lang.Object target, java.lang.String command, java.lang.Object[] parameters)
          Merges a SQL command with an array of parameters.
static java.lang.String like(java.lang.String parameter)
          Prepare the parameter for use in a SQL "LIKE" expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StatementUtils

public StatementUtils()
Method Detail

like

public static final java.lang.String like(java.lang.String parameter)
Prepare the parameter for use in a SQL "LIKE" expression.


createTable

public static final int createTable(java.lang.String resource,
                                    java.lang.String tableName,
                                    java.lang.String tableCreate)
                             throws java.sql.SQLException
Create a new database table in an existing database, by sending "CREATE TABLE " and the parameters to the DBMS configured with the ConnectionPool.

For safety, does not drop table first.

Returns false if a SQL exception is thrown; exception is written to the servlet log.

Parameters:
resource - The database resource key or null for default
tableName - The name of the table to create
tableCreate - The SQL command defining the fields and indices
Returns:
Result of statement.execute()
Throws:
SQL - Exception if SQL error occurs
java.sql.SQLException

createKey

public static final java.lang.Object createKey(java.lang.String resource,
                                               int column,
                                               java.lang.String query,
                                               java.lang.Object key,
                                               java.lang.String update)
                                        throws java.sql.SQLException
Returns next sequential key for given table.

This ensures compatibility for DBMS products that do not support auto-incrementing a key field.

Intended to generate primary keys, but could be used to create other serial numbers based on an unsigned int.

Allocating the key involves reading the current key, and then incrementing the key for the next user. The method is synchronized so that two threads do not read the same key before it is incremented.

This routine is "unrolled" for efficiency, but the same effect can be accomplished using getColumn() to fetch the next key, and executeUpdate to increment the key.

Parameters:
resource - The database resource key or null for default
column - The column to return from the result set.
query - The SQL statement to fetch the next key.
key - The table name or other replaceable parameter.
Throws:
java.sql.SQLException - if SQL error occurs

executeUpdate

public static final int executeUpdate(java.sql.Connection connection,
                                      java.lang.String resource,
                                      java.lang.String command,
                                      java.lang.Object[] parameters)
                               throws java.sql.SQLException
Prepares statement using SQL statement and executes with DBMS configured with the ConnectionPool.

Command may be an INSERT, UPDATE, or DELETE statement or anything that returns nothing, such as a DDL statement.

Parameters:
connection - The database connection or null to acquire
resource - The database resource key or null for default
command - The SQL statement to execute.
parameters - An array of parameter objects
Throws:
java.sql.SQLException - if SQL error occurs

executeUpdate

public static final int executeUpdate(java.lang.String resource,
                                      java.lang.String command,
                                      java.lang.Object parameter)
                               throws java.sql.SQLException
Convenience method for calling executeUpdate(String,String,Object[]); with a single Object parameter.

Parameters:
resource - The database resource key or null for default
command - The SQL statement to execute.
parameter - A single parameter to use with command
Throws:
java.sql.SQLException - if SQL error occurs

executeUpdate

public static final int executeUpdate(java.lang.String resource,
                                      java.lang.String command)
                               throws java.sql.SQLException
Convenience method for calling executeUpdate(String,String,Object[]); without a parameter list.

Parameters:
resource - The database resource key or null for default
command - The SQL statement to execute.
Throws:
java.sql.SQLException - if SQL error occurs

executeUpdate

public static final int executeUpdate(java.lang.String resource,
                                      java.lang.String command,
                                      java.lang.Object[] parameters)
                               throws java.sql.SQLException
Prepares statement using SQL statement and executes with DBMS configured with the ConnectionPool.

Command may be an INSERT, UPDATE, or DELETE statement or anything that returns nothing, such as a DDL statement.

Parameters:
resource - The database resource key or null for default
command - The SQL statement to execute.
parameters - An array of parameter objects
Throws:
java.sql.SQLException - if SQL error occurs

executeUpdate

public static final int executeUpdate(java.lang.String command)
                               throws java.sql.SQLException
Convenience method for calling executeUpdate(String,String,Object[]); without a parameter list.

Parameters:
command - The SQL statement to execute.
Throws:
java.sql.SQLException - if SQL error occurs

executeQuery

public static final java.sql.ResultSet executeQuery(java.sql.Connection connection,
                                                    java.lang.String command,
                                                    java.lang.Object[] parameters)
                                             throws java.sql.SQLException
Merges a SQL command with an array of parameters. Any number or no parameters may be passed. The parameters parameter may also be null.

The Statement or PreparedStatement used internally are closed, but the Connection is left open so that the ResultSet remains valid. The caller should close the Connection and ResultSet as soon as possible, especially if they are pooled.

Parameters:
command - The SQL statement to prepare and execute.
parameters - The replaceable parameters to use with command.
Throws:
java.sql.SQLException - if SQL error occurs

getColumn

public static final java.lang.Object getColumn(java.lang.String resource,
                                               int column,
                                               java.lang.String command,
                                               java.lang.Object[] parameters)
                                        throws java.sql.SQLException
Merges a SQL command with an array of parameters and returns a column from the first record of the result set as an Object. If an empty set results, null is returned. Any number or no parameters may be passed. The parameters parameter may also be null. The SQL Statement/ResultSet are handled by ExecuteQuery(). The ResultSet is converted to a collection using ResultSetUtils.getCollection(Object,ResultSet). The ResultSet is released, and the Collection returned.

Parameters:
resource - The database resource key or null for default
column - The column to return from the result set
command - The SQL statement to prepare and execute.
parameters - The replaceable parameters to use with command.
Throws:
java.sql.SQLException - if SQL error occurs

getColumn

public static final java.lang.Object getColumn(java.lang.String resource,
                                               int column,
                                               java.lang.String command,
                                               java.lang.Object key)
                                        throws java.sql.SQLException
Convenience method for calling getColumn(String,int,String,Object[]); with a single object parameter.

Parameters:
resource - The database resource key or null for default
column - The column to return from the result set.
command - The SQL statement to prepare and execute.
key - The replaceable parameter
Throws:
java.sql.SQLException - if SQL error occurs

getColumn

public static final java.lang.Object getColumn(java.lang.String resource,
                                               int column,
                                               java.lang.String command,
                                               int key)
                                        throws java.sql.SQLException
Convenience method for calling getColumn(String,Object,String,Object[]); with a single int parameter.

Parameters:
resource - The database resource key or null for default
column - The column to return from the result set.
command - The SQL statement to prepare and execute.
key - The replaceable parameter to use, if any.
Throws:
java.sql.SQLException - if SQL error occurs

getColumn

public static final java.lang.Object getColumn(java.lang.String resource,
                                               int column,
                                               java.lang.String command)
                                        throws java.sql.SQLException
Convenience method for calling getColumn(String,Object,String,Object[]); without a parameter list.

Parameters:
resource - The database resource key or null for default
column - The column to return from the result set.
command - The SQL statement to prepare and execute.
Throws:
java.sql.SQLException - if SQL error occurs

getElement

public static final boolean getElement(java.lang.String resource,
                                       java.lang.Object target,
                                       java.lang.String command,
                                       java.lang.Object[] parameters)
                                throws java.sql.SQLException
Merges a SQL command with an array of parameters. Any number or no parameters may be passed. The parameters parameter may also be null. The SQL Statement/ResultSet are handled by ExecuteQuery(). The first record of the ResultSet is used to populate ResultSetUtils.getElement(Object,ResultSet). The ResultSet is released, and the bean returned.

Parameters:
resource - The database resource key or null for default
target - The JavaBean object to populate.
command - The SQL statement to prepare and execute.
parameters - The replaceable parameters to use with command.
Returns:
True if element is found
Throws:
java.sql.SQLException - if SQL error occurs

getElement

public static final boolean getElement(java.lang.String resource,
                                       java.lang.Object target,
                                       java.lang.String command,
                                       java.lang.Object key)
                                throws java.sql.SQLException
Convenience method for calling getElement(String,Object,String,Object[]); with a single object parameter.

Parameters:
resource - The database resource key or null for default
target - The JavaBean object to populate.
command - The SQL statement to prepare and execute.
key - The replaceable parameter to use with LIKE.
Returns:
True if element is found
Throws:
java.sql.SQLException - if SQL error occurs

getElement

public static final boolean getElement(java.lang.String resource,
                                       java.lang.Object target,
                                       java.lang.String command,
                                       int key)
                                throws java.sql.SQLException
Convenience method for calling getElement(String,Object,String,Object[]); with a single int parameter.

Parameters:
resource - The database resource key or null for default
target - The JavaBean object to populate.
command - The SQL statement to prepare and execute.
key - The replaceable parameter to use with LIKE.
Returns:
True if element is found
Throws:
java.sql.SQLException - if SQL error occurs

getElement

public static final boolean getElement(java.lang.String resource,
                                       java.lang.Object target,
                                       java.lang.String command)
                                throws java.sql.SQLException
Convenience method for calling getElement(String,Object,String,Object[]); without a parameter list.

Parameters:
resource - The database resource key or null for default
target - The JavaBean object to populate.
command - The SQL statement to prepare and execute.
Returns:
True if element is found
Throws:
java.sql.SQLException - if SQL error occurs

getCollection

public static final java.util.Collection getCollection(java.lang.String resource,
                                                       java.lang.Object target,
                                                       java.lang.String command,
                                                       java.lang.Object[] parameters)
                                                throws java.sql.SQLException
Merges a SQL command with an array of parameters. Any number or no parameters may be passed. The parameters parameter may also be null. The SQL Statement/ResultSet are handled by ExecuteQuery(). The ResultSet is converted to a collection using ResultSetUtils.getCollection(Object,ResultSet). The ResultSet is released, and the Collection returned.

Parameters:
resource - The database resource key or null for default
target - The JavaBean object to return in the collection.
command - The SQL statement to prepare and execute.
parameters - The replaceable parameters to use with command.
Throws:
java.sql.SQLException - if SQL error occurs

getCollection

public static final java.util.Collection getCollection(java.lang.String resource,
                                                       java.lang.Object target,
                                                       java.lang.String command,
                                                       java.lang.Object key)
                                                throws java.sql.SQLException
Convenience method for calling getCollection(String,Object,String,Object[]); with a single object parameter.

Parameters:
resource - The database resource key or null for default
target - The JavaBean object to return in the collection.
command - The SQL statement to prepare and execute.
key - The replaceable parameter to use with LIKE.
Throws:
java.sql.SQLException - if SQL error occurs

getCollection

public static final java.util.Collection getCollection(java.lang.String resource,
                                                       java.lang.Object target,
                                                       java.lang.String command,
                                                       int key)
                                                throws java.sql.SQLException
Convenience method for calling getCollection(String,Object,String,Object[]); with a single int parameter.

Parameters:
resource - The database resource key or null for default
target - The JavaBean object to return in the collection.
command - The SQL statement to prepare and execute.
key - The replaceable parameter to use with LIKE.
Throws:
java.sql.SQLException - if SQL error occurs

getCollection

public static final java.util.Collection getCollection(java.lang.String resource,
                                                       java.lang.Object target,
                                                       java.lang.String command)
                                                throws java.sql.SQLException
Convenience method for calling getCollection(String,Object,String,Object[]); without a parameter list.

Parameters:
resource - The database resource key or null for default
target - The JavaBean object to return in the collection.
command - The SQL statement to prepare and execute.
Throws:
java.sql.SQLException - if SQL error occurs


Copyright © 2002-2005 The Apache Software Foundation. All Rights Reserved.