|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.scaffold.sql.StatementUtils
public final class StatementUtils
General purpose SQL Statements.
Constructor Summary | |
---|---|
StatementUtils()
|
Method Summary | |
---|---|
static Object |
createKey(String resource,
int column,
String query,
Object key,
String update)
Returns next sequential key for given table. |
static int |
createTable(String resource,
String tableName,
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 ResultSet |
executeQuery(Connection connection,
String command,
Object[] parameters)
Merges a SQL command with an array of parameters. |
static int |
executeUpdate(Connection connection,
String resource,
String command,
Object[] parameters)
Prepares statement using SQL statement and executes with DBMS configured with the ConnectionPool. |
static int |
executeUpdate(String command)
Convenience method for calling executeUpdate(String,String,Object[]);
without a parameter list. |
static int |
executeUpdate(String resource,
String command)
Convenience method for calling executeUpdate(String,String,Object[]);
without a parameter list. |
static int |
executeUpdate(String resource,
String command,
Object parameter)
Convenience method for calling executeUpdate(String,String,Object[]);
with a single Object parameter. |
static int |
executeUpdate(String resource,
String command,
Object[] parameters)
Prepares statement using SQL statement and executes with DBMS configured with the ConnectionPool. |
static Collection |
getCollection(String resource,
Object target,
String command)
Convenience method for calling getCollection(String,Object,String,Object[]);
without a parameter list. |
static Collection |
getCollection(String resource,
Object target,
String command,
int key)
Convenience method for calling getCollection(String,Object,String,Object[]);
with a single int parameter. |
static Collection |
getCollection(String resource,
Object target,
String command,
Object key)
Convenience method for calling getCollection(String,Object,String,Object[]);
with a single object parameter. |
static Collection |
getCollection(String resource,
Object target,
String command,
Object[] parameters)
Merges a SQL command with an array of parameters. |
static Object |
getColumn(String resource,
int column,
String command)
Convenience method for calling getColumn(String,Object,String,Object[]);
without a parameter list. |
static Object |
getColumn(String resource,
int column,
String command,
int key)
Convenience method for calling getColumn(String,Object,String,Object[]);
with a single int parameter. |
static Object |
getColumn(String resource,
int column,
String command,
Object key)
Convenience method for calling getColumn(String,int,String,Object[]);
with a single object parameter. |
static Object |
getColumn(String resource,
int column,
String command,
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(String resource,
Object target,
String command)
Convenience method for calling getElement(String,Object,String,Object[]);
without a parameter list. |
static boolean |
getElement(String resource,
Object target,
String command,
int key)
Convenience method for calling getElement(String,Object,String,Object[]);
with a single int parameter. |
static boolean |
getElement(String resource,
Object target,
String command,
Object key)
Convenience method for calling getElement(String,Object,String,Object[]);
with a single object parameter. |
static boolean |
getElement(String resource,
Object target,
String command,
Object[] parameters)
Merges a SQL command with an array of parameters. |
static String |
like(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 |
---|
public StatementUtils()
Method Detail |
---|
public static final String like(String parameter)
public static final int createTable(String resource, String tableName, String tableCreate) throws SQLException
For safety, does not drop table first.
Returns false if a SQL exception is thrown; exception is written to the servlet log.
resource
- The database resource key or null for defaulttableName
- The name of the table to createtableCreate
- The SQL command defining the
fields and indices
SQL
- Exception if SQL error occurs
SQLException
public static final Object createKey(String resource, int column, String query, Object key, String update) throws SQLException
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.
resource
- The database resource key or null for defaultcolumn
- 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.query
- The SQL statement to increment the next key.
SQLException
- if SQL error occurspublic static final int executeUpdate(Connection connection, String resource, String command, Object[] parameters) throws SQLException
Command may be an INSERT, UPDATE, or DELETE statement or anything that returns nothing, such as a DDL statement.
connection
- The database connection or null to acquireresource
- The database resource key or null for defaultcommand
- The SQL statement to execute.parameters
- An array of parameter objects
SQLException
- if SQL error occurspublic static final int executeUpdate(String resource, String command, Object parameter) throws SQLException
executeUpdate(String,String,Object[]);
with a single Object parameter.
resource
- The database resource key or null for defaultcommand
- The SQL statement to execute.parameter
- A single parameter to use with command
SQLException
- if SQL error occurspublic static final int executeUpdate(String resource, String command) throws SQLException
executeUpdate(String,String,Object[]);
without a parameter list.
resource
- The database resource key or null for defaultcommand
- The SQL statement to execute.
SQLException
- if SQL error occurspublic static final int executeUpdate(String resource, String command, Object[] parameters) throws SQLException
Command may be an INSERT, UPDATE, or DELETE statement or anything that returns nothing, such as a DDL statement.
resource
- The database resource key or null for defaultcommand
- The SQL statement to execute.parameters
- An array of parameter objects
SQLException
- if SQL error occurspublic static final int executeUpdate(String command) throws SQLException
executeUpdate(String,String,Object[]);
without a parameter list.
resource
- The database resource key or null for defaultcommand
- The SQL statement to execute.
SQLException
- if SQL error occurspublic static final ResultSet executeQuery(Connection connection, String command, Object[] parameters) throws SQLException
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.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to return in the collection.command
- The SQL statement to prepare and execute.parameters
- The replaceable parameters to use with command.
SQLException
- if SQL error occurspublic static final Object getColumn(String resource, int column, String command, Object[] parameters) throws SQLException
ExecuteQuery()
.
The ResultSet is converted to a collection using
ResultSetUtils.getCollection(Object,ResultSet)
.
The ResultSet is released, and the Collection returned.
resource
- The database resource key or null for defaultcolumn
- The column to return from the result setcommand
- The SQL statement to prepare and execute.parameters
- The replaceable parameters to use with command.
SQLException
- if SQL error occurspublic static final Object getColumn(String resource, int column, String command, Object key) throws SQLException
getColumn(String,int,String,Object[]);
with a single object parameter.
resource
- The database resource key or null for defaultcolumn
- The column to return from the result set.command
- The SQL statement to prepare and execute.key
- The replaceable parameter
SQLException
- if SQL error occurspublic static final Object getColumn(String resource, int column, String command, int key) throws SQLException
getColumn(String,Object,String,Object[]);
with a single int parameter.
resource
- The database resource key or null for defaultcolumn
- The column to return from the result set.command
- The SQL statement to prepare and execute.key
- The replaceable parameter to use, if any.
SQLException
- if SQL error occurspublic static final Object getColumn(String resource, int column, String command) throws SQLException
getColumn(String,Object,String,Object[]);
without a parameter list.
resource
- The database resource key or null for defaultcolumn
- The column to return from the result set.command
- The SQL statement to prepare and execute.key
- The replaceable parameter.
SQLException
- if SQL error occurspublic static final boolean getElement(String resource, Object target, String command, Object[] parameters) throws SQLException
ExecuteQuery()
.
The first record of the ResultSet is used to populate
ResultSetUtils.getElement(Object,ResultSet)
.
The ResultSet is released, and the bean returned.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to populate.command
- The SQL statement to prepare and execute.parameters
- The replaceable parameters to use with
command.
SQLException
- if SQL error occurspublic static final boolean getElement(String resource, Object target, String command, Object key) throws SQLException
getElement(String,Object,String,Object[]);
with a single object parameter.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to populate.command
- The SQL statement to prepare and execute.key
- The replaceable parameter to use with LIKE.
SQLException
- if SQL error occurspublic static final boolean getElement(String resource, Object target, String command, int key) throws SQLException
getElement(String,Object,String,Object[]);
with a single int parameter.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to populate.command
- The SQL statement to prepare and execute.key
- The replaceable parameter to use with LIKE.
SQLException
- if SQL error occurspublic static final boolean getElement(String resource, Object target, String command) throws SQLException
getElement(String,Object,String,Object[]);
without a parameter list.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to populate.command
- The SQL statement to prepare and execute.key
- The replaceable parameter to use with LIKE.
SQLException
- if SQL error occurspublic static final Collection getCollection(String resource, Object target, String command, Object[] parameters) throws SQLException
ExecuteQuery()
.
The ResultSet is converted to a collection using
ResultSetUtils.getCollection(Object,ResultSet)
.
The ResultSet is released, and the Collection returned.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to return in the collection.command
- The SQL statement to prepare and execute.parameters
- The replaceable parameters to use with command.
SQLException
- if SQL error occurspublic static final Collection getCollection(String resource, Object target, String command, Object key) throws SQLException
getCollection(String,Object,String,Object[]);
with a single object parameter.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to return in the collection.command
- The SQL statement to prepare and execute.key
- The replaceable parameter to use with LIKE.
SQLException
- if SQL error occurspublic static final Collection getCollection(String resource, Object target, String command, int key) throws SQLException
getCollection(String,Object,String,Object[]);
with a single int parameter.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to return in the collection.command
- The SQL statement to prepare and execute.key
- The replaceable parameter to use with LIKE.
SQLException
- if SQL error occurspublic static final Collection getCollection(String resource, Object target, String command) throws SQLException
getCollection(String,Object,String,Object[]);
without a parameter list.
resource
- The database resource key or null for defaulttarget
- The JavaBean object to return in the collection.command
- The SQL statement to prepare and execute.key
- The replaceable parameter to use with LIKE.
SQLException
- if SQL error occurs
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |