|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.buzzsurf.sql.BuzzSQL
public abstract class BuzzSQL
Every SQL object in BuzzSQL extends from the base class com.buzzsurf.sql.BuzzSQL, and therefore all operate
in a very similar way.
Four different constructors are provided for each object to handle the different cases of how a connection
to the database is obtained. See Connecting to the Database
for more information on obtaining a connection to the database explicitly or automatically.
setSQL(String) before execution when using this constructor. java.sql.Connection
object. This constructor provides a great deal of flexibility by allowing the use of "explicit" connections
that are supplied by the user rather than being obtained automatically by BuzzSQL. Using a explicit
connection also allows BuzzSQL to support database transactions. java.sql.PreparedStatement object that expects argument
placeholders in the SQL as question marks (?). The SQL statement can be passed in via constructor or using
the setSQL(String) method. setArgs(Object...) or addArgs(Object...) methods,
which are Java 5 variable arguments methods. Therefore you can call setArgs(Object...) and
pass any type of object or primitive in any combination. The order of your arguments must only match the
order of your question marks in the SQL statement. The difference between setArgs and addArgs is that
setArgs will first clear an previously set values. setArgs(Object...)
as the appropriate native Java type. byte, short, int, long if the database type is numeric float, double if the database type is numeric with precision String, char if the database type is varchar based java.util.Date, java.util.Calendar if the database type is a date/time based com.buzzsurf.sql.OutParameter, com.buzzsurf.sql.InOutParameter if the database type is
a stored procedure OUT or INOUT parameter. See Stored Procedure for more
information. execute() throws an exception if any of
these steps fails for any reason. ResultSet and ResultSetMetaData, while Update based objects
will query and save the updated row count to a local variable. execute() returns a reference to the current object to support method chaining. See Method Chaining for more information. You can assume execution succeeded if
no exception is thrown. close() after you have finished using any BuzzSQL object.
close() will release any resources including the database connection if appropriate. It will
never throw an exception, so it is always safe to call close(). close().
DataSourceManager,
Select,
Update,
Insert,
Delete,
StoredProcedure| Field Summary | |
|---|---|
protected java.util.List<java.lang.Object> |
args
|
protected java.sql.Connection |
con
|
protected static java.lang.ThreadLocal<java.text.SimpleDateFormat> |
DATABASE_FORMATTER
|
protected java.lang.String |
dataSourceName
|
protected java.lang.String |
sql
|
protected java.sql.PreparedStatement |
stmt
|
protected boolean |
usingExplicitConnection
|
| Constructor Summary | |
|---|---|
protected |
BuzzSQL()
|
| Method Summary | ||
|---|---|---|
|
addArgs(java.lang.Object... args)
Add objects to the current SQL statement arguments. |
|
|
close()
Close and commit the SQL object. |
|
|
close(boolean commit)
Close the SQL object with a choice to commit. |
|
abstract
|
execute()
During execution a database connection is obtained (if needed), SQL and arguments are merged, and the PreparedStatement is executed against the database. |
|
java.util.List<java.lang.Object> |
getArgs()
Get array of the currently set args. |
|
java.sql.Connection |
getConnection()
Get the current database connection. |
|
java.lang.String |
getDataSourceName()
Get the current dataSource name. |
|
static ReleaseInfo |
getReleaseInfo()
Get information on this BuzzSQL release. |
|
java.lang.String |
getSQL()
Get SQL statement string. |
|
java.sql.PreparedStatement |
getStatement()
Get the internal java.sql.PreparedStatement. |
|
protected java.sql.PreparedStatement |
merge(java.sql.PreparedStatement stmt)
|
|
protected void |
prepare()
|
|
static java.lang.String |
queryToString(java.lang.String sql,
java.util.List<java.lang.Object> args)
Utility method to create a nice readable representation of the results of merging a statment with ? placeholders with object args. |
|
|
setArgs(java.lang.Object... args)
Set the SQL statement arguments. |
|
|
setConnection(java.sql.Connection con)
Set the current connection to an explicit connection. |
|
|
setDataSourceName(java.lang.String dataSourceName)
Set the dataSource name. |
|
|
setSQL(java.lang.String sql)
Set the SQL statement. |
|
boolean |
usingExplicitConnection()
usingExplicitConnection() will return true if you pass a
java.sql.Connection in via the constructor, or call
setConnection(Connection con) with a non-null java.sql.Connection object. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected static java.lang.ThreadLocal<java.text.SimpleDateFormat> DATABASE_FORMATTER
protected java.lang.String dataSourceName
protected java.lang.String sql
protected java.util.List<java.lang.Object> args
protected java.sql.Connection con
protected java.sql.PreparedStatement stmt
protected boolean usingExplicitConnection
| Constructor Detail |
|---|
protected BuzzSQL()
| Method Detail |
|---|
public java.lang.String getDataSourceName()
execute() to obtain a
connection from DataSourceManager.
DataSourceManagerpublic <E extends BuzzSQL> E setDataSourceName(java.lang.String dataSourceName)
execute() to obtain a connection from
DataSourceManager. dataSourceName will usually be passed via constructor, but access is
provided here to support beans, soap, etc. Will have no effect if the object is currently executing or
using an explicit connection.
dataSourceName - dataSource name
usingExplicitConnection(),
DataSourceManagerpublic java.sql.Connection getConnection()
public <E extends BuzzSQL> E setConnection(java.sql.Connection con)
usingExplicitConnection true. When using an explicit connection you are responsible for
opening and closing the connection. setConnection has no effect if the object is
currently executing.
con - The external connection or null to make the connection null and not use an external
connection
usingExplicitConnection()public java.lang.String getSQL()
public <E extends BuzzSQL> E setSQL(java.lang.String sql)
setSQL
has no effect if the object is currently executing.
sql - The SQL statement
public java.util.List<java.lang.Object> getArgs()
public <E extends BuzzSQL> E setArgs(java.lang.Object... args)
setArgs has no effect if the object is currently executing.
args - Object args or null if there are no args to the SQL statement.
public <E extends BuzzSQL> E addArgs(java.lang.Object... args)
setArgs has no effect if the object is currently
executing.
args - Object args or null if there are no args to the SQL statement.
public boolean usingExplicitConnection()
usingExplicitConnection() will return true if you pass a
java.sql.Connection in via the constructor, or call
setConnection(Connection con) with a non-null java.sql.Connection object.
You can reset the BuzzSQL object's state to use an automatic connections by passing null to
setConnection(Connection con). java.sql.Connection can come from any source such as an external connection
pool, a JNDI lookup, or BuzzSQL's DataSource Manager. It is common for an application to use BuzzSQL's
automatic connection handling for 99% of the database access, but need to wrap a few specific calls in
a transaction. In this case you can call DataSourceManager.getConnection() to obtain an
explicit connection for the calls that need to be executed in a transaction, and allow BuzzSQL to use
automatic connection handling for the rest.
public java.sql.PreparedStatement getStatement()
java.sql.PreparedStatement.
PreparedStatement or null if the object is not executing.
public abstract <E extends BuzzSQL> E execute()
throws java.sql.SQLException
execute() throws an exception if any
of these steps fails for any reason. ResultSet and ResultSetMetaData, while Update
based objects will query and save the updated row count to a local variable. execute() returns a reference to the current object to support method chaining. See Method Chaining for more information. You can assume execution succeeded
if no exception is thrown.
java.sql.SQLException - if any of the executing JDBC operations failedpublic <E extends BuzzSQL> E close()
close() after you have finished using any BuzzSQL object.
close() will release any resources including the database connection if appropriate. It
will never throw an exception, so it is always safe to call close(). close(). close()
in a finally block.
usingExplicitConnection()public <E extends BuzzSQL> E close(boolean commit)
close() after you have finished using any BuzzSQL object.
close() will release any resources including the database connection if appropriate. It
will never throw an exception, so it is always safe to call close(). close(). close()
in a finally block.
commit - If true BuzzSQL will commit the transaction before closing the connection.
usingExplicitConnection()
protected java.sql.PreparedStatement merge(java.sql.PreparedStatement stmt)
throws java.sql.SQLException
java.sql.SQLException
protected void prepare()
throws java.sql.SQLException
java.sql.SQLExceptionpublic static ReleaseInfo getReleaseInfo()
public static java.lang.String queryToString(java.lang.String sql,
java.util.List<java.lang.Object> args)
sql - Statementargs - Args
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||