|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.buzzsurf.sql.BuzzSQL
com.buzzsurf.sql.Update
com.buzzsurf.sql.Insert
public class Insert
An Insert object inserts new rows to a table and queries the number of rows inserted. It is a combination of the underlying JDBC classes;
java.sql.Connection
java.sql.PreparedStatement
getInsertCount() or
getRowCount().
getGeneratedKey(). This method allows the retrieval of only the first auto increment column
in the table, and only as an int. BuzzSQL always allows access to the underlying base JDBC
object if you need more control, such as to retrieve other auto increment column values.
com.buzzsurf.sql.Insert extends com.buzzsurf.sql.Update, but add only minor functionality
beyond that which is contained in the parent class; getGeneratedKey() being the primary
addition. Therefore you may choose to use com.buzzsurf.sql.Update to perform inserts if it
helps to streamline your object structure.
Insert object using the automatic
default connection to the database. The SQL statement is passed via constructor, and the arguments are passed using the
setArgs(Object...) method. execute() is called to execute the
database call, and then the primary key column value is retrieved using getGeneratedKey().
The object is then closed to cleanup any resources and return the connection to the pool.
Insert insert = new Insert("insert into example.table_example1(col_str, col_int) values (?,?)")
insert.setArgs("asdf", 123);
insert.execute();
int pk = insert.getGeneratedKey();
System.out.println(pk);
insert.close();
Note: This code does not handle exceptions. You normally should wrap your calls in try/catch/finally blocks to handle
any problems. It is advisable to put your close() call in a finally block to insure the
connection is always released.
BuzzSQL,
Update| Field Summary |
|---|
| Fields inherited from class com.buzzsurf.sql.Update |
|---|
rowCount |
| Fields inherited from class com.buzzsurf.sql.BuzzSQL |
|---|
args, con, DATABASE_FORMATTER, dataSourceName, sql, stmt, usingExplicitConnection |
| Constructor Summary | |
|---|---|
Insert()
A zero argument constructor is provided for simplified operation with JavBeans, SOAP, and reflection scenarios where having such a constructor is necessary or convenient. |
|
Insert(java.lang.String sql)
A single argument constructor that accepts your SQL statement and uses the default DataSource. |
|
Insert(java.lang.String sql,
java.sql.Connection con)
A dual argument constructor that accepts your SQL statement and a java.sql.Connection object. |
|
Insert(java.lang.String sql,
java.lang.String dataSourceName)
A dual argument constructor that accepts your SQL statement and the explicit name of a DataSource to use. |
|
| Method Summary | |
|---|---|
Insert |
execute()
During execution a database connection is obtained (if needed), SQL and arguments are merged, and the PreparedStatement is executed against the database. |
int |
getGeneratedKey()
If the database table contains an auto increment column, the value can be retrieved using getGeneratedKey(). |
int |
getInsertCount()
Get the number of rows inserted. |
| Methods inherited from class com.buzzsurf.sql.Update |
|---|
getRowCount, getUpdateCount |
| Methods inherited from class com.buzzsurf.sql.BuzzSQL |
|---|
addArgs, close, close, getArgs, getConnection, getDataSourceName, getReleaseInfo, getSQL, getStatement, merge, prepare, queryToString, setArgs, setConnection, setDataSourceName, setSQL, usingExplicitConnection |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Insert()
setSQL(String) before execution when using this constructor.
public Insert(java.lang.String sql)
sql - The SQL statement
public Insert(java.lang.String sql,
java.lang.String dataSourceName)
sql - The SQL statementdataSourceName - The explicit name of the dataSource to obtain a connecton from in DataSourceManager.
public Insert(java.lang.String sql,
java.sql.Connection con)
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.
sql - The SQL statementcon - The explicit database Connection| Method Detail |
|---|
public int getInsertCount()
public int getGeneratedKey()
throws java.sql.SQLException
getGeneratedKey(). This method allows the retrieval of only the first auto increment column
in the table, and only as an int. BuzzSQL always allows access to the underlying base JDBC
object if you need more control, such as to retrieve other auto increment column values.
java.sql.SQLException - if a database access error occurs
public Insert 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.
execute in class Updatejava.sql.SQLException - if any of the executing JDBC operations failed
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||