|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object nu.staldal.lsp.framework.DBConnection
public class DBConnection
Wrapper around a JDBC Connection
with convenience methods.
Field Summary | |
---|---|
protected java.text.DateFormat |
dateFormatter
Formatter for the SQL DATE type. |
protected java.sql.Connection |
dbConn
The wrapped Connection . |
static java.text.DateFormat |
DEFAULT_DATE_FORMAT
Default formatter for SQL DATE type. |
static java.text.DateFormat |
DEFAULT_TIME_FORMAT
Default formatter for the SQL TIME type. |
static java.text.DateFormat |
DEFAULT_TIMESTAMP_FORMAT
Default formatter for the SQL TIMESTAMP type. |
protected java.lang.Object |
nullReplacement
The object to use when copying a ResultSet (row) and a JDBC NULL occurs. |
protected java.text.DateFormat |
timeFormatter
Formatter for the SQL TIME type. |
protected java.text.DateFormat |
timestampFormatter
Formatter for the SQL TIMESTAMP type. |
Constructor Summary | |
---|---|
DBConnection(java.sql.Connection dbConn)
Create a DBConnection. |
Method Summary | |
---|---|
void |
close()
Close connection. |
void |
closeResultSet(java.sql.ResultSet rs)
Close a ResultSet and its Statement . |
void |
commit()
Commit transaction. |
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
copyResultSet(java.sql.ResultSet rs)
Copy a ResultSet into a List of
Map s. |
java.util.Map<java.lang.String,java.lang.Object> |
copyResultSetRow(java.sql.ResultSet rs)
Copy a ResultSet row into a Map . |
int |
deleteRow(java.lang.String table,
java.lang.String whereClause,
java.lang.Object... params)
Delete rows from a table. |
java.sql.ResultSet |
executeQuery(java.lang.String query,
java.lang.Object... params)
Execute a parameterized query. |
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
executeQueryAndCopy(java.lang.String query,
java.lang.Object... params)
Execute a parameterized query, copy and close the ResultSet. |
int |
executeUpdate(java.lang.String query,
java.lang.Object... params)
Execute a parameterized update query. |
boolean |
exists(java.lang.String query,
java.lang.Object... params)
Execute a parameterized query which and check whether it returns any rows. |
java.sql.Connection |
getConnection()
Return the wrapped Connection . |
java.text.DateFormat |
getDateFormatter()
Get formatter for the SQL DATE type. |
java.lang.Object |
getNullReplacement()
Get the object to use when copying a ResultSet (row) and a JDBC NULL occurs. |
java.text.DateFormat |
getTimeFormatter()
Get formatter for the SQL TIME type. |
java.text.DateFormat |
getTimestampFormatter()
Get formatter for the SQL TIMESTAMP type. |
int |
insertRow(java.lang.String table,
java.lang.String colList,
java.lang.Object... params)
Insert a row into a table |
boolean |
lookupBoolean(java.lang.String query,
java.lang.Object... params)
Execute a parameterized query which and return the first object in the first row of the ResultSet as a boolean. |
int |
lookupInt(java.lang.String query,
java.lang.Object... params)
Execute a parameterized query which and return the first object in the first row of the ResultSet as an integer. |
java.lang.Object |
lookupObject(java.lang.String query,
java.lang.Object... params)
Execute a parameterized query which and return the first object in the first row of the ResultSet. |
java.util.Map<java.lang.String,java.lang.Object> |
lookupRow(java.lang.String query,
java.lang.Object... params)
Execute a parameterized query which and return a copy of the first row of the ResultSet. |
java.lang.String |
lookupString(java.lang.String query,
java.lang.Object... params)
Execute a parameterized query which and return the first object in the first row of the ResultSet as a string. |
void |
rollback()
Rollback transaction. |
void |
setDateFormatter(java.text.DateFormat df)
Set formatter for the SQL DATE type. |
void |
setNullReplacement(java.lang.Object o)
Set the object to use when copying a ResultSet (row) and a JDBC NULL occurs. |
void |
setTimeFormatter(java.text.DateFormat df)
Set formatter for the SQL TIME type. |
void |
setTimestampFormatter(java.text.DateFormat df)
Set formatter for the SQL TIMESTAMP type. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.text.DateFormat DEFAULT_DATE_FORMAT
public static final java.text.DateFormat DEFAULT_TIME_FORMAT
public static final java.text.DateFormat DEFAULT_TIMESTAMP_FORMAT
protected java.lang.Object nullReplacement
Default is the empty string.
protected java.text.DateFormat dateFormatter
Default is SimpleDateFormat("yyyy-MM-dd")
.
protected java.text.DateFormat timestampFormatter
Default is SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.
protected java.text.DateFormat timeFormatter
Default is SimpleDateFormat("HH:mm:ss")
.
protected final java.sql.Connection dbConn
Connection
.
Constructor Detail |
---|
public DBConnection(java.sql.Connection dbConn)
dbConn
- the JDBC Connection
to wrap.Method Detail |
---|
public void setNullReplacement(java.lang.Object o)
Default is the empty string.
Set to null
to not do any replacement.
o
- the replacement objectpublic java.lang.Object getNullReplacement()
Default is the empty string.
public void setDateFormatter(java.text.DateFormat df)
Default is SimpleDateFormat("yyyy-MM-dd")
.
Set to null
to not do any formatting.
df
- the DateFormatpublic java.text.DateFormat getDateFormatter()
public void setTimestampFormatter(java.text.DateFormat df)
Default is SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.
Set to null
to not do any formatting.
df
- the DateFormatpublic java.text.DateFormat getTimestampFormatter()
public void setTimeFormatter(java.text.DateFormat df)
Default is SimpleDateFormat("HH:mm:ss")
.
Set to null
to not do any formatting.
df
- the DateFormatpublic java.text.DateFormat getTimeFormatter()
public java.sql.Connection getConnection()
Connection
.
Connection
public void commit() throws java.sql.SQLException
java.sql.SQLException
- if a database error occursConnection.commit()
public void rollback() throws java.sql.SQLException
java.sql.SQLException
- if a database error occursConnection.rollback()
public void close() throws java.sql.SQLException
java.sql.SQLException
- if a database error occursConnection.close()
public java.sql.ResultSet executeQuery(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException
You should pass the returned ResultSet to closeResultSet(java.sql.ResultSet)
when done (unless you pass it to copyResultSet(java.sql.ResultSet)
).
query
- the SQL query with '?' parametersparams
- parameter values
ResultSet
java.sql.SQLException
- if a database error occursPreparedStatement.executeQuery()
,
closeResultSet(java.sql.ResultSet)
public java.util.List<java.util.Map<java.lang.String,java.lang.Object>> executeQueryAndCopy(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException
query
- the SQL query with '?' parametersparams
- parameter values
java.sql.SQLException
- if a database error occursPreparedStatement.executeQuery()
,
setNullReplacement(java.lang.Object)
public void closeResultSet(java.sql.ResultSet rs) throws java.sql.SQLException
ResultSet
and its Statement
.
Does nothing if rs is null
.
rs
- the ResultSet
to close
java.sql.SQLException
- if a database error occursexecuteQuery(java.lang.String, java.lang.Object...)
public java.util.List<java.util.Map<java.lang.String,java.lang.Object>> copyResultSet(java.sql.ResultSet rs) throws java.sql.SQLException
ResultSet
into a List
of
Map
s. Useful for passing a ResultSet to an LSP page.
The ResultSet is closed after copying.
rs
- the ResultSet to copy
java.sql.SQLException
- if a database error occurssetNullReplacement(java.lang.Object)
public java.util.Map<java.lang.String,java.lang.Object> copyResultSetRow(java.sql.ResultSet rs) throws java.sql.SQLException
ResultSet
row into a Map
.
The current row of the ResultSet is copied, and the ResultSet is not
advanced.
rs
- the ResultSet to copy a row from
java.sql.SQLException
- if a database error occurssetNullReplacement(java.lang.Object)
public java.util.Map<java.lang.String,java.lang.Object> lookupRow(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException, java.util.NoSuchElementException
Will do the same translation of dates and null as
copyResultSetRow(ResultSet)
.
query
- the SQL query with '?' parametersparams
- parameter values
java.sql.SQLException
- if a database error occurs
java.util.NoSuchElementException
- if the query rerurns no rowsPreparedStatement.executeQuery()
,
copyResultSetRow(ResultSet)
public java.lang.Object lookupObject(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException, java.util.NoSuchElementException
Will not do any translation of dates and null.
query
- the SQL query with '?' parametersparams
- parameter values
java.sql.SQLException
- if a database error occurs
java.util.NoSuchElementException
- if the query rerurns no rowsPreparedStatement.executeQuery()
public java.lang.String lookupString(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException, java.util.NoSuchElementException
query
- the SQL query with '?' parametersparams
- parameter values
java.sql.SQLException
- if a database error occurs
java.util.NoSuchElementException
- if the query rerurns no rowsPreparedStatement.executeQuery()
public int lookupInt(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException, java.util.NoSuchElementException
query
- the SQL query with '?' parametersparams
- parameter values
java.sql.SQLException
- if a database error occurs
java.util.NoSuchElementException
- if the query rerurns no rowsPreparedStatement.executeQuery()
public boolean lookupBoolean(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException, java.util.NoSuchElementException
query
- the SQL query with '?' parametersparams
- parameter values
java.sql.SQLException
- if a database error occurs
java.util.NoSuchElementException
- if the query rerurns no rowsPreparedStatement.executeQuery()
public boolean exists(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException
query
- the SQL query with '?' parametersparams
- parameter values
true
if the query returns any rows,
false
otherwise
java.sql.SQLException
- if a database error occursPreparedStatement.executeQuery()
public int executeUpdate(java.lang.String query, java.lang.Object... params) throws java.sql.SQLException
query
- the SQL query with '?' parametersparams
- parameters
java.sql.SQLException
- if a database error occursPreparedStatement.executeUpdate()
public int insertRow(java.lang.String table, java.lang.String colList, java.lang.Object... params) throws java.sql.SQLException
table
- name of the tablecolList
- list of column names, separated with ','params
- values to insert into the columns
java.sql.SQLException
- if a database error occursexecuteUpdate(java.lang.String, java.lang.Object...)
public int deleteRow(java.lang.String table, java.lang.String whereClause, java.lang.Object... params) throws java.sql.SQLException
table
- name of the tablewhereClause
- WHERE clause (without "WHERE") for the DELETE query,
with with '?' parametersparams
- parameter values to the WHERE clause
java.sql.SQLException
- if a database error occursexecuteUpdate(java.lang.String, java.lang.Object...)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |