|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javautils.collections.Algs | +--javautils.jdbc.JDBC
Static utility methods for dealing with JDBC.
Field Summary |
Fields inherited from class javautils.collections.Algs |
EMPTY_ARRAY, EMPTY_LIST, EMPTY_MAP, EMPTY_SEQUENCE, EMPTY_SET |
Constructor Summary | |
JDBC()
|
Method Summary | |
static void |
executeTransaction(java.sql.Connection connection,
Function nullaryProc)
Performs a transaction. |
static java.lang.Object |
fold(java.lang.Object accumulator,
java.sql.ResultSet rs,
Function fun)
Folds a function into the row sequence of the result set. |
static void |
forEach(java.sql.ResultSet rs,
Function proc)
Executes a procedure for each row of the result set. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public JDBC()
Method Detail |
public static void executeTransaction(java.sql.Connection connection, Function nullaryProc)
Performs a transaction. The intention of this method is to simplify the implementation of transactions.
This method:
Note that this method does not automatically roll back any mutations made to the application state. The mutations made to the database through the connection are rolled back in case the transaction procedure throws an exception.
public static java.lang.Object fold(java.lang.Object accumulator, java.sql.ResultSet rs, Function fun)
Folds a function into the row sequence of the result set.
The accumulator is passed as the first argument to the function. The return value from the function then replaces the accumulator. Finally the value of the accumulator is returned from fold.
The columns of the result set are matched to the arguments of the
function. The second argument of the function gets the value of the
first column, the third arguments gets the second column, etc... The
type of the argument determines the getter used to get the value of
column from the result set. Basically, if the type of an argument is
a primitive type, then a getter with the same primitive type will be
used. Otherwise either the getObject(int i)
or the
getter of the specific non-primitive type will be used.
Suppose that you have a table that maps names (strings) to ages (integers). To make a simple list of the table, you could use code like this:
String result =fold
(new StringBuffer("<dl>\n"), stmt.executeQuery(" SELECT name " + " , age " + " FROM players " + " ORDER BY name DESC "), newFunction
() { public StringBuffer with(StringBuffer accumulator, String name, int age) { accumulator.append(" <dt>" + name + "</dt>\n" + " <dd>" + age + "</dd>\n"); return accumulator; }}) + "</dl>\n";
public static void forEach(java.sql.ResultSet rs, Function proc)
Executes a procedure for each row of the result set.
The columns of the result set are matched to the arguments of the
procedure. The first argument of the procedure gets the value of the
first column, the second arguments gets the second column, etc... The
type of the argument determines the getter used to get the value of
column from the result set. Basically, if the type of an argument is
a primitive type, then a getter with the same primitive type will be
used. Otherwise either the getObject(int i)
or the
getter of the specific non-primitive type will be used.
Suppose that you have a table that maps names (strings) to ages (integers). To make a simple list of the table, you could use code like this:
final StringBuffer result = new StringBuffer("<dl>\n");forEach
(stmt.executeQuery(" SELECT name, age " + " FROM players " + " ORDER BY name DESC "), newFunction
() { public void with(String name, int age) { result.append(" <dt>" + name + "</dt>\n" + " <dd>" + age + "</dd>\n"); }}); result.append("</dl>\n");
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |