Modifier and Type | Class and Description |
---|---|
static class |
QueryUtil.ResultSetPrintDirection
Specifies the orientation of the result set when printed (via
methods like
printResultSet() ). |
Modifier and Type | Method and Description |
---|---|
static boolean |
abortTransaction(Connection con)
Calls
abortTransaction with
an empty message string. |
static boolean |
abortTransaction(Connection con,
String message)
Aborts (rolls back) the current transaction on the specified connection.
|
static void |
close(Connection con)
Closes the specified connection, ignoring any exceptions encountered
in the connection.close() method itself.
|
static void |
close(ResultSet rs,
Statement stmt,
Connection con)
Closes the specified connection, statement and resultset, logging any
errors to the stderr.
|
static boolean |
endTransaction(Connection con)
Calls
endTransaction with
an empty message string. |
static boolean |
endTransaction(Connection con,
String message)
Commits the specified connection.
|
static void |
ensureCount(int rowcount,
int expected)
Checks to see if actual row count was same as expected row count, for some
query.
|
static void |
ensureScrollable(ResultSet rs)
Checks to see if the specified result set is scrollable.
|
static List |
executeQuery(Connection con,
String query)
Convenience method that calls
executeQuery(Connection, String, boolean)
specifying no header options (i.e., column headers are not returned as part
of the query results). |
static List |
executeQuery(Connection con,
String query,
boolean headers)
Performs the specified query and returns a List (the result of
converting the ResultSet via the
rsToList(java.sql.ResultSet, boolean) method). |
static DBName |
getDBName(Connection con)
Returns the dbname corresponding that the database for the specified
jdbc connection.
|
static long |
getLastInsertID(Connection con,
DBName dbname,
Object info)
Gets the last inserted id, typically auto-increment(mysql) or serial
(postgresql) columns.
|
static long |
getNewTxID()
Returns a montonically increasing number starting from 1.
|
static PreparedStatement |
getRewindablePreparedStmt(Connection con,
String sql)
Creates a new connection that will return ResultSet's of
TYPE_SCROLL_INSENSITIVE and CONCUR_READ_ONLY.
|
static Statement |
getRewindableStmt(Connection con)
Creates a new connection that will return ResultSet's of
TYPE_SCROLL_INSENSITIVE and CONCUR_READ_ONLY.
|
static long |
getRowCount(ResultSet rset)
Returns the number of rows in the specified ResultSet.
|
static boolean |
hasExactlyOneRow(Connection con,
String query)
Peforms the specified query and returns true if the query has
only one row of data.
|
static boolean |
hasExactlyZeroRows(Connection con,
String query)
Peforms the specified query and returns true if the query returns
no data.
|
static void |
init(Log logger)
This method can be called to initialize a logger for this class (else
a default logger will used).
|
static void |
printResultSetHTMLTable(ResultSet rs,
javax.servlet.jsp.JspWriter out,
QueryUtil.ResultSetPrintDirection direction)
Prints the given ResultSet as a HTML table to the specified JspWriter.
|
static void |
printResultSetHTMLTable(ResultSet rs,
PrintWriter out,
QueryUtil.ResultSetPrintDirection direction)
Prints the given ResultSet as a HTML table to the specified PrintWriter.
|
static void |
printResultSetTable(ResultSet rs,
PrintStream ps)
Delegates to
printResultSetTable(ResultSet, PrintStream,
ResultSetPrintDirection, TablePrinter.PrintConfig, boolean) so that the
table is printed horizontally
with the default table style and headers set to true. |
static void |
printResultSetTable(ResultSet rs,
PrintStream ps,
QueryUtil.ResultSetPrintDirection direction,
TablePrinter.PrintConfig config,
boolean headers)
Prints the specified result set in a tabular format.
|
static void |
printRS(ResultSet rs)
Prints the given java.sql.ResultSet (including result set headers) in a
simple straightforward fashion to System.out
|
static void |
rollback(Connection con)
Rollsback the transaction, ignoring any errors in the rollback itself.
|
static String |
rsListToString(List list)
Converts the list returned by the
method to a String,
consisting of all the rows contained in the list. |
static List |
rsToList(ResultSet rs,
boolean headers)
Converts a java.sql.ResultSet into a List of Object[], where each Object[]
represents all the columns in one row.
|
static boolean |
startTransaction(Connection con)
Calls
startTransaction with
a empty message string. |
static boolean |
startTransaction(Connection con,
int transactionIsolation,
String message)
Starts a transaction on the specified connection.
|
static boolean |
startTransaction(Connection con,
String message)
Starts a transaction on the specified connection.
|
public static final void init(Log logger)
logger
- Sets the log to which methods in this class
will send logging output.public static final long getNewTxID()
public static DBName getDBName(Connection con) throws SQLException
ConnectionMgr
SQLException
public static final void ensureScrollable(ResultSet rs) throws SQLException
SQLException
- if the specified result set is not
scrollable.public static final void ensureCount(int rowcount, int expected) throws SQLException
rowcount
- the actual count (returned by executeUpdate() etc.)expected
- the expected countSQLException
- if the actual and expected counts don't matchpublic static final long getRowCount(ResultSet rset) throws SQLException
ResultSet.TYPE_SCROLL_INSENSITIVE
, then this method moves
the result set pointer back to the beginning, after it is finished. If the
result set is not scroll insensitive, then this method will still work
properly, but the contents of the result set will not be usable again
(since it cannot be rewinded).
Note: the count can also be retreived directly (and more efficiently) for many queries by including the COUNT() SQL function as part of the query (in which case one would read the returned count column directly and NOT call this method)
rset
- the result set to examineSQLException
public static final long getLastInsertID(Connection con, DBName dbname, Object info) throws SQLException
con
- the connection to get the last id for. Both
mysql and postgresql (and probably other db's) treat the
last inserted id as a per connection value (associated with
the last statement on that connection that returned such a
value).dbname
- the name of the target database. Needed because
this must be implemented in a database specific
way.info
- optional further info needed to implement this
method for some databases. This currently is:
- mysql: not needed, specify null
- postgresql: specify the name of the sequence, which for serial columns is (by default) tablename_colname_seq, for example "tablefoo_myserialid_seq"
SQLException
- if the last insert id could not be found
or if some other datbase problem occurred.public static final List executeQuery(Connection con, String query) throws SQLException
executeQuery(Connection, String, boolean)
specifying no header options (i.e., column headers are not returned as part
of the query results).con
- the database connection to use.query
- the query to be performedheaders
- if true, the first row contains the column headersSQLException
public static final List executeQuery(Connection con, String query, boolean headers) throws SQLException
rsToList(java.sql.ResultSet, boolean)
method).
Important Note: this method is useful for program generated queries, but should not be used for queries where unknown data could be send by a malicious user (since the query string is sent as-is to the server). For secure queries, use PreparedStatements instead.
con
- the database connection to use.query
- the query to be peformedheaders
- if true, the first row contains the column headersSQLException
public static final boolean hasExactlyOneRow(Connection con, String query) throws SQLException
con
- the database connection to use.SQLException
public static final boolean hasExactlyZeroRows(Connection con, String query) throws SQLException
con
- the database connection to use.SQLException
public static final Statement getRewindableStmt(Connection con) throws SQLException
SQLException
public static final PreparedStatement getRewindablePreparedStmt(Connection con, String sql) throws SQLException
Note, by default a java.sql.Connection.prepareStatment(String) method
returns statements that support ResultSet's of forward_only. This means we
will not be able to determine the row count via the getRowCount(java.sql.ResultSet)
and also be able to then rewind the ResultSet and read it's
contents.
SQLException
public static final boolean startTransaction(Connection con, int transactionIsolation, String message)
endTransaction
method must be called to end this transaction.
If the transaction cannot be started, the connection is closed.
con
- the connection to committransactionIsolation
- the transaction isolation level
for this transactionmessage
- commit description; shown when debug
logging is turned onIllegalArgumentException
- if the transaction isolation level is not a valid value (as defined in Connection
public static final boolean startTransaction(Connection con, String message)
endTransaction(java.sql.Connection, java.lang.String)
method must be called to end this transaction. The
transaction isolation is whatever the default transaction isolation is for
this connection, driver or database (this method does not explicitly set
the isolation level). See startTransaction(Connection, int,
String)
.
If the transaction cannot be started, the connection is closed.
con
- the connection to commitmessage
- commit description; shown if debug logging
is turned onpublic static final boolean startTransaction(Connection con)
startTransaction
with
a empty message string.public static final boolean abortTransaction(Connection con, String message)
After the transaction is rolled back, the connection is set to autoCommit(true).
con
- the connection to rollbackmessage
- a description; shown if logging is turned onpublic static final boolean abortTransaction(Connection con)
abortTransaction
with
an empty message string.public static final boolean endTransaction(Connection con, String message)
After the transaction completes, the connection is set to autoCommit(true).
con
- the connection to commitmessage
- commit description; shown if logging is turned onpublic static final boolean endTransaction(Connection con)
endTransaction
with
an empty message string.public static final void rollback(Connection con)
public static final void close(ResultSet rs, Statement stmt, Connection con)
public static final void close(Connection con)
public static final List rsToList(ResultSet rs, boolean headers) throws SQLException
rs
- the ResultSetheaders
- if set to true, the first row of the returned List
will contain an Object[] of the column header names.SQLException
public static final String rsListToString(List list)
method
to a String,
consisting of all the rows contained in the list. Each row is rendered
within brackets [..row1..], with different rows seperated by
commas ([..row1..], [..row2..], ...) but this format may be
changed in the future.public static final void printRS(ResultSet rs) throws SQLException
rs
- the result set to printSQLException
public static final void printResultSetTable(ResultSet rs, PrintStream ps) throws SQLException
printResultSetTable(ResultSet, PrintStream,
ResultSetPrintDirection, TablePrinter.PrintConfig, boolean)
so that the
table is printed horizontally
with the default table style and headers set to true.SQLException
public static final void printResultSetTable(ResultSet rs, PrintStream ps, QueryUtil.ResultSetPrintDirection direction, TablePrinter.PrintConfig config, boolean headers) throws SQLException
PrintConfig
object.rs
- the ResultSetps
- the destination print streamdirection
- the result set printing orientation.config
- the printing configuration.
Specify nullfor to use the
default style.headers
- true to print headers, false to omit
headers. Headers are obtained from the
ResultSet's Meta Data.SQLException
public static final void printResultSetHTMLTable(ResultSet rs, javax.servlet.jsp.JspWriter out, QueryUtil.ResultSetPrintDirection direction) throws IOException, SQLException
QueryUtil.ResultSetPrintDirection.HORIZONTAL
).
The output table has the following CSS styles added to it:
IOException
SQLException
public static final void printResultSetHTMLTable(ResultSet rs, PrintWriter out, QueryUtil.ResultSetPrintDirection direction) throws IOException, SQLException
QueryUtil.ResultSetPrintDirection.HORIZONTAL
).
The output table has the following CSS styles added to it:
IOException
SQLException