nl.knowledgeplaza.util
Class JdbcUtil

java.lang.Object
  extended by nl.knowledgeplaza.util.JdbcUtil

public class JdbcUtil
extends Object

Several convenience methods for JDBC related tasks. TODO: can we push in some pooling and optional caching? TODO: we use a list-of-maps, which is very memory intensive. A map-of-lists would be better, or even beter: a single class encapsulating that (ResultSet? HA!).

Version:
$Revision: 1.22 $

Nested Class Summary
static class JdbcUtil.JdbcList
           
static class JdbcUtil.JdbcMap
           
static class JdbcUtil.Preprocess
          An interface for Preprocessing during the run
static class JdbcUtil.SyncDescriptor
          Class that provides all data needed for the synchronizeTableFullOneWay methods.
 
Field Summary
static String[] EMPTY_STRING_ARRAY
           
static String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
 
Constructor Summary
JdbcUtil()
           
 
Method Summary
static String buildSqlDeleteStatement(String tablename, String[] keys)
          Build the SQL statement that checks if a certain record exists select 1 from table where key=?, ...
static String buildSqlInsertStatement(String tablename, String[] fields, String[] keys)
          Build an insert statement with the same parameter list as the update statement would have.
static String buildSqlSelectAllStatement(String tablename, String[] fields, String[] keys)
           
static String buildSqlSelectAllStatement(String tablename, String[] fields, String[] keys, String whereclause)
          Build the SQL statement that selects the specified fields of one record select field, ...
static String buildSqlSelectChangedStatement(String tablename, String[] fields, String[] keys)
          Build the select statement that checks if any of the fields of the record have changed (using the key to identify record) select 1 from table where (fields<>? or ...) and key=? and ...
static String buildSqlSelectExistsStatement(String tablename, String[] keys)
           
static String buildSqlSelectExistsStatement(String tablename, String[] keys, String whereclause)
          Build the SQL statement that checks if a certain record exists select 1 from table where key=?, ...
static String buildSqlSelectStatement(String tablename, String[] fields, String[] keys)
          Build the SQL statement that selects the specified fields of one record select field, ...
static String buildSqlUpdateStatement(String tablename, String[] fields, String[] keys)
          Build an update statement with the same parameter list as the insert statement would have.
static Connection close(Connection c)
          Close and wrap the possible but highly unlikely exception in a RuntimeException
static PreparedStatement close(PreparedStatement c)
          Close and wrap the possible but highly unlikely exception in a RuntimeException
static ResultSet close(ResultSet c)
          Close and wrap the possible but highly unlikely exception in a RuntimeException
static Statement close(Statement c)
          Close and wrap the possible but highly unlikely exception in a RuntimeException
static void commit(Connection connection)
          Commit but throw a runtime exception instead of a SQLExeception.
static int determineColumnType(Connection connection, String tableAndColumnName)
          Return the java.sql.Types for a column NOTE: it may be smart to cache the results
static int determineColumnType(Connection connection, String tableName, String columnName)
          Return the java.sql.Types for a column NOTE: it may be smart to cache the results
static int execute(Connection connection, SqlBuilder sqlBuilder)
           
static int execute(Connection connection, String sql)
          Execute a single SQL
static int execute(Connection connection, String sql, Object... parameters)
          Execute a single SQL with parameters
static int execute(Connection connection, String sql, Object parameter)
           
static int execute(PreparedStatement preparedStatement)
           
static int execute(PreparedStatement preparedStatement, Object parameter)
           
static int execute(PreparedStatement preparedStatement, Object[] parameters)
          Execute a single SQL with parameters
static Object getDefaultValueFor(int lType)
          Return a default value for all java.sql.Types
static int getFieldLength(Connection connection, String tablenamePlusColumnname)
          Determines the length of a field using metadata
static int getFieldLength(Connection connection, String tablename, String columnname)
          Determines the length of a field using metadata
static Number getNextValueFromSequenceTable(Connection connection, String id)
          Get a single value from the sequence table: create table seq(seq_id varchar(20), seq_val integer default 0, primary key(seq_id));
static Number getNextValueFromSequenceTable(Connection connection, String tableName, String idColumnName, String id, String valueColumnName)
          Get a single value from a sequence table
static Number getNextValueFromSequenceTable(Connection connection, String tableName, String idColumnName, String id, String valueColumnName, int increment)
          Get a value block from a sequence table
static Number getNextValueFromSequenceTableAndCheck(Connection connection, String tableName, String idColumnName, String id, String valueColumnName, String sql, int maxTries)
          Get a single value from a sequence table
static boolean hasAtLeastOneRecord(Connection connection, SqlBuilder sqlBuilder)
           
static boolean hasAtLeastOneRecord(Connection connection, String sql)
           
static boolean hasAtLeastOneRecord(Connection connection, String sql, Object... parameters)
          Check to see if the resultset has enough records
static boolean hasAtLeastOneRecord(Connection connection, String sql, Object parameter)
           
static boolean hasAtLeastOneRecord(PreparedStatement preparedStatement)
           
static boolean hasAtLeastOneRecord(PreparedStatement preparedStatement, Object parameter)
           
static boolean hasAtLeastOneRecord(PreparedStatement preparedStatement, Object[] parameters)
          Check to see if the resultset has enough records
static boolean hasEnoughRecords(Connection connection, SqlBuilder sqlBuilder)
           
static boolean hasEnoughRecords(Connection connection, String sql)
           
static boolean hasEnoughRecords(Connection connection, String sql, Object parameter)
           
static boolean hasEnoughRecords(Connection connection, String sql, Object[] parameters, int numberOfRecords)
          Check to see if the resultset has enough records
static boolean hasEnoughRecords(PreparedStatement preparedStatement)
           
static boolean hasEnoughRecords(PreparedStatement preparedStatement, Object parameter)
           
static boolean hasEnoughRecords(PreparedStatement preparedStatement, Object[] parameters, int numberOfRecords)
          Check to see if the resultset has enough records
static boolean isColumnTypeDateOrTime(int type)
          Determine if the java.sql.Types is a date
static boolean isColumnTypeNumber(int type)
          Determine if the java.sql.Types is a number
static boolean isColumnTypeString(int type)
          Determine if the java.sql.Types is a date
static void main(String[] args)
          Some quick unit tests
static Object query(Connection connection, String sql)
          Fetch one row, one field
static Object query(Connection connection, String sql, Object[] parameters)
          Fetch one row, one field
static Object query(PreparedStatement preparedStatement, Object[] parameters)
          Fetch one row, one field
static Map queryAsKeyValueMap(Connection connection, String sql, Object[] parameters)
          Fetch key value pairs (1st first is key, 2nd is value, rest is ignored)
static Map queryAsKeyValueMap(PreparedStatement preparedStatement, Object[] parameters)
          Fetch key value pairs (1st first is key, 2nd is value, rest is ignored)
static List queryAsList(Connection connection, String sql)
          Fetch multiple rows, one field
static List queryAsList(Connection connection, String sql, Object[] parameters)
          Fetch multiple rows, one field
static List queryAsList(PreparedStatement preparedStatement, Object[] parameters)
          Fetch multiple rows, one field
static JdbcUtil.JdbcList queryAsListOfMaps(Connection connection, String sql)
          Fetch multiple rows, multiple fields
static JdbcUtil.JdbcList queryAsListOfMaps(Connection connection, String sql, Object[] parameters)
          Fetch multiple rows, multiple fields
static JdbcUtil.JdbcList queryAsListOfMaps(PreparedStatement preparedStatement, Object[] parameters)
          Fetch multiple rows, multiple fields
static JdbcUtil.JdbcMap queryAsMap(Connection connection, String sql)
          Fetch one row, multiple fields
static JdbcUtil.JdbcMap queryAsMap(Connection connection, String sql, Object[] parameters)
          Fetch one row, multiple fields
static JdbcUtil.JdbcMap queryAsMap(PreparedStatement preparedStatement, Object[] parameters)
          Fetch one row, multiple fields
static void rollback(Connection connection)
          Commit but throw a runtime exception instead of a SQLExeception.
static Object sanatizeParameter(Object p)
          try to convert parameters to something JDBC will accept, like Calendar to java.sql.Date
static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(Connection connection, SqlBuilder sqlBuilder)
           
static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(Connection connection, String sql)
           
static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(Connection connection, String sql, Object parameter)
           
static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(Connection connection, String sql, Object[] parameters)
           
static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(PreparedStatement preparedStatement)
           
static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(PreparedStatement preparedStatement, Object parameter)
           
static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(PreparedStatement preparedStatement, Object[] parameters)
           
static List selectManyRowsFirstField(Connection connection, SqlBuilder sqlBuilder)
           
static List selectManyRowsFirstField(Connection connection, String sql)
           
static List selectManyRowsFirstField(Connection connection, String sql, Object parameter)
           
static List selectManyRowsFirstField(Connection connection, String sql, Object[] parameters)
           
static List selectManyRowsFirstField(PreparedStatement preparedStatement)
           
static List selectManyRowsFirstField(PreparedStatement preparedStatement, Object parameter)
           
static List selectManyRowsFirstField(PreparedStatement preparedStatement, Object[] parameters)
           
static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(Connection connection, SqlBuilder sqlBuilder)
           
static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(Connection connection, String sql, Object parameter)
           
static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(Connection connection, String sql, Object[] parameters)
           
static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(PreparedStatement preparedStatement)
           
static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(PreparedStatement preparedStatement, Object parameter)
           
static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(PreparedStatement preparedStatement, Object[] parameters)
           
static JdbcUtil.JdbcMap selectOneRowAllFields(Connection connection, SqlBuilder sqlBuilder)
           
static JdbcUtil.JdbcMap selectOneRowAllFields(Connection connection, String sql)
           
static JdbcUtil.JdbcMap selectOneRowAllFields(Connection connection, String sql, Object parameter)
           
static JdbcUtil.JdbcMap selectOneRowAllFields(Connection connection, String sql, Object[] parameters)
           
static JdbcUtil.JdbcMap selectOneRowAllFields(PreparedStatement preparedStatement)
           
static JdbcUtil.JdbcMap selectOneRowAllFields(PreparedStatement preparedStatement, Object parameter)
           
static JdbcUtil.JdbcMap selectOneRowAllFields(PreparedStatement preparedStatement, Object[] parameters)
           
static Object selectOneRowOneField(Connection connection, SqlBuilder sqlBuilder)
           
static Object selectOneRowOneField(Connection connection, String sql)
           
static Object selectOneRowOneField(Connection connection, String sql, Object... parameters)
           
static Object selectOneRowOneField(PreparedStatement preparedStatement)
           
static Object selectOneRowOneField(PreparedStatement preparedStatement, Object parameter)
           
static Object selectOneRowOneField(PreparedStatement preparedStatement, Object[] parameters)
           
static void setParameters(PreparedStatement preparedStatement, Object... parameters)
          set parameters to a prepared statement
static void synchronizeTableFullOneWay_Copy(Connection fromConnection, Connection toConnection, String fromTablename, String toTablename, String[] keys, String[] fields, int commitEvery, org.apache.log4j.Logger log4j, JdbcUtil.Preprocess preprocess)
          Insert in the table in the to connection with any missing records in relation to the from connection
static void synchronizeTableFullOneWay_Delete(Connection fromConnection, Connection toConnection, String tablename, String[] keys, String[] fields, int commitEvery, org.apache.log4j.Logger log4j)
          Remove any records in the to connection that do not exist in the form connection
static void synchronizeTableFullOneWay_Delete(Connection fromConnection, String fromTablename, String[] fromKeys, String[] fromFields, Connection toConnection, String toTablename, String[] toKeys, String[] toFields, int commitEvery, boolean logExceptionButContinue, org.apache.log4j.Logger log4j)
          Remove any records in the to connection that do not exist in the form connection
static void synchronizeTableFullOneWay_Delete(JdbcUtil.SyncDescriptor syncDescriptor, org.apache.log4j.Logger log4j)
          Remove any records in the to connection that do not exist in the form connection
static void synchronizeTableFullOneWay_Insert(Connection fromConnection, Connection toConnection, String tablename, String[] keys, String[] fields, int commitEvery, org.apache.log4j.Logger log4j)
          Insert in the table in the to connection with any missing records in relation to the from connection
static void synchronizeTableFullOneWay_Insert(Connection fromConnection, Connection toConnection, String fromTablename, String toTablename, String[] keys, String[] fields, int commitEvery, org.apache.log4j.Logger log4j)
          Insert in the table in the to connection with any missing records in relation to the from connection
static void synchronizeTableFullOneWay_Insert(Connection fromConnection, Connection toConnection, String fromTablename, String toTablename, String[] keys, String[] fields, int commitEvery, org.apache.log4j.Logger log4j, JdbcUtil.Preprocess preprocess)
          Insert in the table in the to connection with any missing records in relation to the from connection
static void synchronizeTableFullOneWay_InsertUpdate(Connection fromConnection, Connection toConnection, String tablename, String[] keys, String[] fields, int commitEvery, org.apache.log4j.Logger log4j)
          Update the table in the to connection with any missing records in relation to the from connection
static void synchronizeTableFullOneWay_InsertUpdate(Connection fromConnection, String fromTablename, String[] fromKeys, String[] fromFields, Connection toConnection, String toTablename, String[] toKeys, String[] toFields, int commitEvery, boolean logExceptionButContinue, org.apache.log4j.Logger log4j)
          Update the table in the to connection with any missing records in relation to the from connection
static void synchronizeTableFullOneWay_InsertUpdate(JdbcUtil.SyncDescriptor syncDescriptor, org.apache.log4j.Logger log4j)
          Update the table in the to connection with any missing records in relation to the from connection
static void synchronizeTableFullOneWay(Connection fromConnection, Connection toConnection, String tablename, String[] keys, String[] fields, int commitEvery, boolean logExceptionButContinue, org.apache.log4j.Logger log4j)
          Insert, update or delete record so that the table in the to connection becomes identical to the from connection
static void synchronizeTableFullOneWay(Connection fromConnection, Connection toConnection, String tablename, String[] keys, String[] fields, int commitEvery, org.apache.log4j.Logger log4j)
          Insert, update or delete record so that the table in the to connection becomes identical to the from connection
static void synchronizeTableFullOneWay(Connection fromConnection, Connection toConnection, String fromSchemaName, String fromTablename, int commitEvery, org.apache.log4j.Logger log4j)
          Sync a table with all fields
static void synchronizeTableFullOneWay(Connection fromConnection, String fromTablename, String[] fromKeys, String[] fromFields, Connection toConnection, String toTablename, String[] toKeys, String[] toFields, int commitEvery, boolean logExceptionButContinue, org.apache.log4j.Logger log4j)
          Insert, update or delete record so that the table in the to connection becomes identical to the from connection
static void synchronizeTableFullOneWay(Connection fromConnection, String fromTablename, String[] fromKeys, String[] fromFields, Connection toConnection, String toTablename, String[] toKeys, String[] toFields, int commitEvery, org.apache.log4j.Logger log4j)
          Insert, update or delete record so that the table in the to connection becomes identical to the from connection
static void synchronizeTableFullOneWay(JdbcUtil.SyncDescriptor syncDescriptor, org.apache.log4j.Logger log4j)
          Insert, update or delete record so that the table in the to connection becomes identical to the from connection
static boolean tableExists(Connection connection, String tableName)
          Check if a table exists
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOURCECODE_VERSION

public static final String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values

EMPTY_STRING_ARRAY

public static final String[] EMPTY_STRING_ARRAY
Constructor Detail

JdbcUtil

public JdbcUtil()
Method Detail

buildSqlSelectAllStatement

public static String buildSqlSelectAllStatement(String tablename,
                                                String[] fields,
                                                String[] keys,
                                                String whereclause)
Build the SQL statement that selects the specified fields of one record select field, ... from table Optionally a where clause can be specified

Parameters:
tableName -
fields -
keys -
whereclause -

buildSqlSelectAllStatement

public static String buildSqlSelectAllStatement(String tablename,
                                                String[] fields,
                                                String[] keys)

buildSqlSelectStatement

public static String buildSqlSelectStatement(String tablename,
                                             String[] fields,
                                             String[] keys)
Build the SQL statement that selects the specified fields of one record select field, ... from table where key=?, ...

Parameters:
tableName -
fields -
keys -

buildSqlSelectExistsStatement

public static String buildSqlSelectExistsStatement(String tablename,
                                                   String[] keys,
                                                   String whereclause)
Build the SQL statement that checks if a certain record exists select 1 from table where key=?, ...

Parameters:
tableName -
keys -

buildSqlSelectExistsStatement

public static String buildSqlSelectExistsStatement(String tablename,
                                                   String[] keys)

buildSqlInsertStatement

public static String buildSqlInsertStatement(String tablename,
                                             String[] fields,
                                             String[] keys)
Build an insert statement with the same parameter list as the update statement would have. This means that there is only one set of "set" calls needed to fill either the insert or update. insert into table(fields..., keys...) values (?, ..., ?)

Parameters:
tableName -
fields -
keys -

buildSqlUpdateStatement

public static String buildSqlUpdateStatement(String tablename,
                                             String[] fields,
                                             String[] keys)
Build an update statement with the same parameter list as the insert statement would have. This means that there is only one set of "set" calls needed to fill either the insert or update. update table set field=?, ... where key=?, ...

Parameters:
tableName -
fields -
keys -

buildSqlDeleteStatement

public static String buildSqlDeleteStatement(String tablename,
                                             String[] keys)
Build the SQL statement that checks if a certain record exists select 1 from table where key=?, ...

Parameters:
tableName -
keys -

buildSqlSelectChangedStatement

public static String buildSqlSelectChangedStatement(String tablename,
                                                    String[] fields,
                                                    String[] keys)
Build the select statement that checks if any of the fields of the record have changed (using the key to identify record) select 1 from table where (fields<>? or ...) and key=? and ...

Parameters:
tableName -
fields -
keys -

close

public static Connection close(Connection c)
Close and wrap the possible but highly unlikely exception in a RuntimeException

Parameters:
c -

close

public static Statement close(Statement c)
Close and wrap the possible but highly unlikely exception in a RuntimeException

Parameters:
c -

close

public static PreparedStatement close(PreparedStatement c)
Close and wrap the possible but highly unlikely exception in a RuntimeException

Parameters:
c -

close

public static ResultSet close(ResultSet c)
Close and wrap the possible but highly unlikely exception in a RuntimeException

Parameters:
c -

execute

public static int execute(Connection connection,
                          String sql)
                   throws SQLException
Execute a single SQL

Parameters:
connection -
sql -
Returns:
number of rows modified
Throws:
SQLException

execute

public static int execute(Connection connection,
                          String sql,
                          Object... parameters)
                   throws SQLException
Execute a single SQL with parameters

Parameters:
connection -
sql -
Returns:
number of rows modified
Throws:
SQLException

execute

public static int execute(Connection connection,
                          String sql,
                          Object parameter)
                   throws SQLException
Throws:
SQLException

execute

public static int execute(PreparedStatement preparedStatement,
                          Object[] parameters)
                   throws SQLException
Execute a single SQL with parameters

Parameters:
connection -
sql -
Returns:
number of rows modified
Throws:
SQLException

execute

public static int execute(PreparedStatement preparedStatement,
                          Object parameter)
                   throws SQLException
Throws:
SQLException

execute

public static int execute(PreparedStatement preparedStatement)
                   throws SQLException
Throws:
SQLException

execute

public static int execute(Connection connection,
                          SqlBuilder sqlBuilder)
                   throws SQLException
Throws:
SQLException

rollback

public static void rollback(Connection connection)
Commit but throw a runtime exception instead of a SQLExeception. USE WITH CASE.

Parameters:
connection -

commit

public static void commit(Connection connection)
Commit but throw a runtime exception instead of a SQLExeception. USE WITH CASE.

Parameters:
connection -

hasAtLeastOneRecord

public static boolean hasAtLeastOneRecord(Connection connection,
                                          String sql,
                                          Object... parameters)
                                   throws SQLException
Check to see if the resultset has enough records

Parameters:
connection -
sql -
parameters -
Throws:
SQLException

hasAtLeastOneRecord

public static boolean hasAtLeastOneRecord(Connection connection,
                                          String sql,
                                          Object parameter)
                                   throws SQLException
Throws:
SQLException

hasAtLeastOneRecord

public static boolean hasAtLeastOneRecord(Connection connection,
                                          String sql)
                                   throws SQLException
Throws:
SQLException

hasAtLeastOneRecord

public static boolean hasAtLeastOneRecord(PreparedStatement preparedStatement,
                                          Object[] parameters)
                                   throws SQLException
Check to see if the resultset has enough records

Parameters:
preparedStatement -
parameters -
Throws:
SQLException

hasAtLeastOneRecord

public static boolean hasAtLeastOneRecord(PreparedStatement preparedStatement,
                                          Object parameter)
                                   throws SQLException
Throws:
SQLException

hasAtLeastOneRecord

public static boolean hasAtLeastOneRecord(PreparedStatement preparedStatement)
                                   throws SQLException
Throws:
SQLException

hasAtLeastOneRecord

public static boolean hasAtLeastOneRecord(Connection connection,
                                          SqlBuilder sqlBuilder)
                                   throws SQLException
Throws:
SQLException

hasEnoughRecords

public static boolean hasEnoughRecords(Connection connection,
                                       String sql,
                                       Object[] parameters,
                                       int numberOfRecords)
                                throws SQLException
Check to see if the resultset has enough records

Parameters:
connection -
sql -
parameters -
Throws:
SQLException

hasEnoughRecords

public static boolean hasEnoughRecords(Connection connection,
                                       String sql,
                                       Object parameter)
                                throws SQLException
Throws:
SQLException

hasEnoughRecords

public static boolean hasEnoughRecords(Connection connection,
                                       String sql)
                                throws SQLException
Throws:
SQLException

hasEnoughRecords

public static boolean hasEnoughRecords(PreparedStatement preparedStatement,
                                       Object[] parameters,
                                       int numberOfRecords)
                                throws SQLException
Check to see if the resultset has enough records

Parameters:
preparedStatement -
parameters -
numberOfRecords -
Throws:
SQLException

hasEnoughRecords

public static boolean hasEnoughRecords(PreparedStatement preparedStatement,
                                       Object parameter)
                                throws SQLException
Throws:
SQLException

hasEnoughRecords

public static boolean hasEnoughRecords(PreparedStatement preparedStatement)
                                throws SQLException
Throws:
SQLException

hasEnoughRecords

public static boolean hasEnoughRecords(Connection connection,
                                       SqlBuilder sqlBuilder)
                                throws SQLException
Throws:
SQLException

tableExists

public static boolean tableExists(Connection connection,
                                  String tableName)
Check if a table exists

Parameters:
connection -
tableName -

query

public static Object query(Connection connection,
                           String sql)
                    throws SQLException
Fetch one row, one field

Parameters:
connection -
sql -
Throws:
SQLException

selectOneRowOneField

public static Object selectOneRowOneField(Connection connection,
                                          String sql)
                                   throws SQLException
Throws:
SQLException

query

public static Object query(Connection connection,
                           String sql,
                           Object[] parameters)
                    throws SQLException
Fetch one row, one field

Parameters:
connection -
sql -
parameters -
Throws:
SQLException

selectOneRowOneField

public static Object selectOneRowOneField(Connection connection,
                                          String sql,
                                          Object... parameters)
                                   throws SQLException
Throws:
SQLException

query

public static Object query(PreparedStatement preparedStatement,
                           Object[] parameters)
                    throws SQLException
Fetch one row, one field

Parameters:
connection -
preparedStatement -
parameters -
Throws:
SQLException

selectOneRowOneField

public static Object selectOneRowOneField(PreparedStatement preparedStatement,
                                          Object[] parameters)
                                   throws SQLException
Throws:
SQLException

selectOneRowOneField

public static Object selectOneRowOneField(PreparedStatement preparedStatement,
                                          Object parameter)
                                   throws SQLException
Throws:
SQLException

selectOneRowOneField

public static Object selectOneRowOneField(PreparedStatement preparedStatement)
                                   throws SQLException
Throws:
SQLException

selectOneRowOneField

public static Object selectOneRowOneField(Connection connection,
                                          SqlBuilder sqlBuilder)
                                   throws SQLException
Throws:
SQLException

queryAsList

public static List queryAsList(Connection connection,
                               String sql)
                        throws SQLException
Fetch multiple rows, one field

Parameters:
connection -
sql -
Throws:
SQLException

selectManyRowsFirstField

public static List selectManyRowsFirstField(Connection connection,
                                            String sql)
                                     throws SQLException
Throws:
SQLException

queryAsList

public static List queryAsList(Connection connection,
                               String sql,
                               Object[] parameters)
                        throws SQLException
Fetch multiple rows, one field

Parameters:
connection -
sql -
parameters -
Throws:
SQLException

selectManyRowsFirstField

public static List selectManyRowsFirstField(Connection connection,
                                            String sql,
                                            Object[] parameters)
                                     throws SQLException
Throws:
SQLException

selectManyRowsFirstField

public static List selectManyRowsFirstField(Connection connection,
                                            String sql,
                                            Object parameter)
                                     throws SQLException
Throws:
SQLException

queryAsList

public static List queryAsList(PreparedStatement preparedStatement,
                               Object[] parameters)
                        throws SQLException
Fetch multiple rows, one field

Parameters:
connection -
preparedStatement -
parameters -
Throws:
SQLException

selectManyRowsFirstField

public static List selectManyRowsFirstField(PreparedStatement preparedStatement,
                                            Object[] parameters)
                                     throws SQLException
Throws:
SQLException

selectManyRowsFirstField

public static List selectManyRowsFirstField(PreparedStatement preparedStatement,
                                            Object parameter)
                                     throws SQLException
Throws:
SQLException

selectManyRowsFirstField

public static List selectManyRowsFirstField(PreparedStatement preparedStatement)
                                     throws SQLException
Throws:
SQLException

selectManyRowsFirstField

public static List selectManyRowsFirstField(Connection connection,
                                            SqlBuilder sqlBuilder)
                                     throws SQLException
Throws:
SQLException

queryAsMap

public static JdbcUtil.JdbcMap queryAsMap(Connection connection,
                                          String sql)
                                   throws SQLException
Fetch one row, multiple fields

Parameters:
connection -
sql -
Throws:
SQLException

selectOneRowAllFields

public static JdbcUtil.JdbcMap selectOneRowAllFields(Connection connection,
                                                     String sql)
                                              throws SQLException
Throws:
SQLException

queryAsMap

public static JdbcUtil.JdbcMap queryAsMap(Connection connection,
                                          String sql,
                                          Object[] parameters)
                                   throws SQLException
Fetch one row, multiple fields

Parameters:
connection -
sql -
Throws:
SQLException

selectOneRowAllFields

public static JdbcUtil.JdbcMap selectOneRowAllFields(Connection connection,
                                                     String sql,
                                                     Object[] parameters)
                                              throws SQLException
Throws:
SQLException

selectOneRowAllFields

public static JdbcUtil.JdbcMap selectOneRowAllFields(Connection connection,
                                                     String sql,
                                                     Object parameter)
                                              throws SQLException
Throws:
SQLException

queryAsMap

public static JdbcUtil.JdbcMap queryAsMap(PreparedStatement preparedStatement,
                                          Object[] parameters)
                                   throws SQLException
Fetch one row, multiple fields

Parameters:
connection -
preparedStatement -
parameters -
Throws:
SQLException

selectOneRowAllFields

public static JdbcUtil.JdbcMap selectOneRowAllFields(PreparedStatement preparedStatement,
                                                     Object[] parameters)
                                              throws SQLException
Throws:
SQLException

selectOneRowAllFields

public static JdbcUtil.JdbcMap selectOneRowAllFields(PreparedStatement preparedStatement,
                                                     Object parameter)
                                              throws SQLException
Throws:
SQLException

selectOneRowAllFields

public static JdbcUtil.JdbcMap selectOneRowAllFields(PreparedStatement preparedStatement)
                                              throws SQLException
Throws:
SQLException

selectOneRowAllFields

public static JdbcUtil.JdbcMap selectOneRowAllFields(Connection connection,
                                                     SqlBuilder sqlBuilder)
                                              throws SQLException
Throws:
SQLException

queryAsListOfMaps

public static JdbcUtil.JdbcList queryAsListOfMaps(Connection connection,
                                                  String sql)
                                           throws SQLException
Fetch multiple rows, multiple fields

Parameters:
connection -
sql -
Throws:
SQLException

selectManyRowsAllFieldsAsListOfMap

public static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(Connection connection,
                                                                   String sql)
                                                            throws SQLException
Throws:
SQLException

queryAsListOfMaps

public static JdbcUtil.JdbcList queryAsListOfMaps(Connection connection,
                                                  String sql,
                                                  Object[] parameters)
                                           throws SQLException
Fetch multiple rows, multiple fields

Parameters:
connection -
sql -
parameters -
Throws:
SQLException

selectManyRowsAllFieldsAsListOfMap

public static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(Connection connection,
                                                                   String sql,
                                                                   Object[] parameters)
                                                            throws SQLException
Throws:
SQLException

selectManyRowsAllFieldsAsListOfMap

public static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(Connection connection,
                                                                   String sql,
                                                                   Object parameter)
                                                            throws SQLException
Throws:
SQLException

queryAsListOfMaps

public static JdbcUtil.JdbcList queryAsListOfMaps(PreparedStatement preparedStatement,
                                                  Object[] parameters)
                                           throws SQLException
Fetch multiple rows, multiple fields

Parameters:
connection -
preparedStatement -
parameters -
Throws:
SQLException

selectManyRowsAllFieldsAsListOfMap

public static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(PreparedStatement preparedStatement,
                                                                   Object[] parameters)
                                                            throws SQLException
Throws:
SQLException

selectManyRowsAllFieldsAsListOfMap

public static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(PreparedStatement preparedStatement,
                                                                   Object parameter)
                                                            throws SQLException
Throws:
SQLException

selectManyRowsAllFieldsAsListOfMap

public static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(PreparedStatement preparedStatement)
                                                            throws SQLException
Throws:
SQLException

selectManyRowsAllFieldsAsListOfMap

public static JdbcUtil.JdbcList selectManyRowsAllFieldsAsListOfMap(Connection connection,
                                                                   SqlBuilder sqlBuilder)
                                                            throws SQLException
Throws:
SQLException

queryAsKeyValueMap

public static Map queryAsKeyValueMap(Connection connection,
                                     String sql,
                                     Object[] parameters)
                              throws SQLException
Fetch key value pairs (1st first is key, 2nd is value, rest is ignored)

Parameters:
connection -
sql -
parameters -
Throws:
SQLException

selectManyRowsFirstTwoFieldsAsKeyValueMap

public static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(Connection connection,
                                                            String sql,
                                                            Object[] parameters)
                                                     throws SQLException
Throws:
SQLException

selectManyRowsFirstTwoFieldsAsKeyValueMap

public static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(Connection connection,
                                                            String sql,
                                                            Object parameter)
                                                     throws SQLException
Throws:
SQLException

queryAsKeyValueMap

public static Map queryAsKeyValueMap(PreparedStatement preparedStatement,
                                     Object[] parameters)
                              throws SQLException
Fetch key value pairs (1st first is key, 2nd is value, rest is ignored)

Parameters:
connection -
preparedStatement -
parameters -
Throws:
SQLException

selectManyRowsFirstTwoFieldsAsKeyValueMap

public static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(PreparedStatement preparedStatement,
                                                            Object[] parameters)
                                                     throws SQLException
Throws:
SQLException

selectManyRowsFirstTwoFieldsAsKeyValueMap

public static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(PreparedStatement preparedStatement,
                                                            Object parameter)
                                                     throws SQLException
Throws:
SQLException

selectManyRowsFirstTwoFieldsAsKeyValueMap

public static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(PreparedStatement preparedStatement)
                                                     throws SQLException
Throws:
SQLException

selectManyRowsFirstTwoFieldsAsKeyValueMap

public static Map selectManyRowsFirstTwoFieldsAsKeyValueMap(Connection connection,
                                                            SqlBuilder sqlBuilder)
                                                     throws SQLException
Throws:
SQLException

setParameters

public static void setParameters(PreparedStatement preparedStatement,
                                 Object... parameters)
                          throws SQLException
set parameters to a prepared statement

Throws:
SQLException

sanatizeParameter

public static Object sanatizeParameter(Object p)
try to convert parameters to something JDBC will accept, like Calendar to java.sql.Date


synchronizeTableFullOneWay

public static void synchronizeTableFullOneWay(Connection fromConnection,
                                              Connection toConnection,
                                              String tablename,
                                              String[] keys,
                                              String[] fields,
                                              int commitEvery,
                                              org.apache.log4j.Logger log4j)
                                       throws SQLException
Insert, update or delete record so that the table in the to connection becomes identical to the from connection

Parameters:
fromConnection -
toConnection -
tablename -
keys -
fields -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay

public static void synchronizeTableFullOneWay(Connection fromConnection,
                                              Connection toConnection,
                                              String tablename,
                                              String[] keys,
                                              String[] fields,
                                              int commitEvery,
                                              boolean logExceptionButContinue,
                                              org.apache.log4j.Logger log4j)
                                       throws SQLException
Insert, update or delete record so that the table in the to connection becomes identical to the from connection

Parameters:
fromConnection -
toConnection -
tablename -
keys -
fields -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay

public static void synchronizeTableFullOneWay(Connection fromConnection,
                                              String fromTablename,
                                              String[] fromKeys,
                                              String[] fromFields,
                                              Connection toConnection,
                                              String toTablename,
                                              String[] toKeys,
                                              String[] toFields,
                                              int commitEvery,
                                              boolean logExceptionButContinue,
                                              org.apache.log4j.Logger log4j)
                                       throws SQLException
Insert, update or delete record so that the table in the to connection becomes identical to the from connection

Parameters:
fromConnection -
fromTablename -
fromKeys -
fromFields -
toConnection -
toTablename -
toKeys -
toFields -
commitEvery -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay

public static void synchronizeTableFullOneWay(Connection fromConnection,
                                              String fromTablename,
                                              String[] fromKeys,
                                              String[] fromFields,
                                              Connection toConnection,
                                              String toTablename,
                                              String[] toKeys,
                                              String[] toFields,
                                              int commitEvery,
                                              org.apache.log4j.Logger log4j)
                                       throws SQLException
Insert, update or delete record so that the table in the to connection becomes identical to the from connection

Parameters:
fromConnection -
fromTablename -
fromKeys -
fromFields -
toConnection -
toTablename -
toKeys -
toFields -
commitEvery -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay

public static void synchronizeTableFullOneWay(JdbcUtil.SyncDescriptor syncDescriptor,
                                              org.apache.log4j.Logger log4j)
                                       throws SQLException
Insert, update or delete record so that the table in the to connection becomes identical to the from connection

Parameters:
syncDescriptor -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_Insert

public static void synchronizeTableFullOneWay_Insert(Connection fromConnection,
                                                     Connection toConnection,
                                                     String tablename,
                                                     String[] keys,
                                                     String[] fields,
                                                     int commitEvery,
                                                     org.apache.log4j.Logger log4j)
                                              throws SQLException
Insert in the table in the to connection with any missing records in relation to the from connection

Parameters:
fromConnection -
toConnection -
tablename -
keys -
fields -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_Insert

public static void synchronizeTableFullOneWay_Insert(Connection fromConnection,
                                                     Connection toConnection,
                                                     String fromTablename,
                                                     String toTablename,
                                                     String[] keys,
                                                     String[] fields,
                                                     int commitEvery,
                                                     org.apache.log4j.Logger log4j)
                                              throws SQLException
Insert in the table in the to connection with any missing records in relation to the from connection

Parameters:
fromConnection -
toConnection -
fromTablename -
toTablename -
keys -
fields -
commitEvery -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_Copy

public static void synchronizeTableFullOneWay_Copy(Connection fromConnection,
                                                   Connection toConnection,
                                                   String fromTablename,
                                                   String toTablename,
                                                   String[] keys,
                                                   String[] fields,
                                                   int commitEvery,
                                                   org.apache.log4j.Logger log4j,
                                                   JdbcUtil.Preprocess preprocess)
                                            throws SQLException
Insert in the table in the to connection with any missing records in relation to the from connection

Parameters:
fromConnection -
toConnection -
fromTablename -
toTablename -
keys -
fields -
commitEvery -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_Insert

public static void synchronizeTableFullOneWay_Insert(Connection fromConnection,
                                                     Connection toConnection,
                                                     String fromTablename,
                                                     String toTablename,
                                                     String[] keys,
                                                     String[] fields,
                                                     int commitEvery,
                                                     org.apache.log4j.Logger log4j,
                                                     JdbcUtil.Preprocess preprocess)
                                              throws SQLException
Insert in the table in the to connection with any missing records in relation to the from connection

Parameters:
fromConnection -
toConnection -
fromTablename -
toTablename -
keys -
fields -
commitEvery -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_InsertUpdate

public static void synchronizeTableFullOneWay_InsertUpdate(Connection fromConnection,
                                                           Connection toConnection,
                                                           String tablename,
                                                           String[] keys,
                                                           String[] fields,
                                                           int commitEvery,
                                                           org.apache.log4j.Logger log4j)
                                                    throws SQLException
Update the table in the to connection with any missing records in relation to the from connection

Parameters:
fromConnection -
toConnection -
tablename -
keys -
fields -
log4j -
Throws:
SQLException - TODO: return a list of PK tuples to simplify delete

synchronizeTableFullOneWay_InsertUpdate

public static void synchronizeTableFullOneWay_InsertUpdate(Connection fromConnection,
                                                           String fromTablename,
                                                           String[] fromKeys,
                                                           String[] fromFields,
                                                           Connection toConnection,
                                                           String toTablename,
                                                           String[] toKeys,
                                                           String[] toFields,
                                                           int commitEvery,
                                                           boolean logExceptionButContinue,
                                                           org.apache.log4j.Logger log4j)
                                                    throws SQLException
Update the table in the to connection with any missing records in relation to the from connection

Parameters:
fromConnection -
fromTablename -
fromKeys -
fromFields -
toConnection -
toTablename -
toKeys -
toFields -
commitEvery -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_InsertUpdate

public static void synchronizeTableFullOneWay_InsertUpdate(JdbcUtil.SyncDescriptor syncDescriptor,
                                                           org.apache.log4j.Logger log4j)
                                                    throws SQLException
Update the table in the to connection with any missing records in relation to the from connection

Parameters:
syncDescriptor -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_Delete

public static void synchronizeTableFullOneWay_Delete(Connection fromConnection,
                                                     Connection toConnection,
                                                     String tablename,
                                                     String[] keys,
                                                     String[] fields,
                                                     int commitEvery,
                                                     org.apache.log4j.Logger log4j)
                                              throws SQLException
Remove any records in the to connection that do not exist in the form connection

Parameters:
fromConnection -
toConnection -
tablename -
keys -
fields -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_Delete

public static void synchronizeTableFullOneWay_Delete(Connection fromConnection,
                                                     String fromTablename,
                                                     String[] fromKeys,
                                                     String[] fromFields,
                                                     Connection toConnection,
                                                     String toTablename,
                                                     String[] toKeys,
                                                     String[] toFields,
                                                     int commitEvery,
                                                     boolean logExceptionButContinue,
                                                     org.apache.log4j.Logger log4j)
                                              throws SQLException
Remove any records in the to connection that do not exist in the form connection

Parameters:
fromConnection -
toConnection -
tablename -
keys -
fields -
commitEvery -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay_Delete

public static void synchronizeTableFullOneWay_Delete(JdbcUtil.SyncDescriptor syncDescriptor,
                                                     org.apache.log4j.Logger log4j)
                                              throws SQLException
Remove any records in the to connection that do not exist in the form connection

Parameters:
syncDescriptor -
log4j -
Throws:
SQLException

synchronizeTableFullOneWay

public static void synchronizeTableFullOneWay(Connection fromConnection,
                                              Connection toConnection,
                                              String fromSchemaName,
                                              String fromTablename,
                                              int commitEvery,
                                              org.apache.log4j.Logger log4j)
                                       throws SQLException
Sync a table with all fields

Parameters:
fromConnection -
toConnection -
fromSchemaName -
fromTablename -
commitEvery -
log4j -
Throws:
SQLException

getNextValueFromSequenceTable

public static Number getNextValueFromSequenceTable(Connection connection,
                                                   String tableName,
                                                   String idColumnName,
                                                   String id,
                                                   String valueColumnName,
                                                   int increment)
                                            throws SQLException
Get a value block from a sequence table

Parameters:
connection - this connection should have auto commit set to false, otherwise there is a small gap where things may go wrong
tableName -
idColumnName -
id -
valueColumnName -
increment -
Returns:
Number or null if nothing was allocated
Throws:
SQLException

getNextValueFromSequenceTable

public static Number getNextValueFromSequenceTable(Connection connection,
                                                   String tableName,
                                                   String idColumnName,
                                                   String id,
                                                   String valueColumnName)
                                            throws SQLException
Get a single value from a sequence table

Parameters:
connection -
tableName -
idColumnName -
id -
valueColumnName -
Returns:
Number or null if nothing was allocated
Throws:
SQLException

getNextValueFromSequenceTable

public static Number getNextValueFromSequenceTable(Connection connection,
                                                   String id)
                                            throws SQLException
Get a single value from the sequence table: create table seq(seq_id varchar(20), seq_val integer default 0, primary key(seq_id));

Parameters:
connection -
id -
Returns:
Number or null if nothing was allocated
Throws:
SQLException

getNextValueFromSequenceTableAndCheck

public static Number getNextValueFromSequenceTableAndCheck(Connection connection,
                                                           String tableName,
                                                           String idColumnName,
                                                           String id,
                                                           String valueColumnName,
                                                           String sql,
                                                           int maxTries)
                                                    throws SQLException
Get a single value from a sequence table

Parameters:
connection -
tableName - sequence table name
idColumnName - sequence table id column name
id - the id stored in the id column
valueColumnName - sequence table value column name
sql - the SQL that validates is the number already is used a a PK
maxTries - number of tries to get a PK
Returns:
Number or null if nothing was allocated
Throws:
SQLException

determineColumnType

public static int determineColumnType(Connection connection,
                                      String tableAndColumnName)
Return the java.sql.Types for a column NOTE: it may be smart to cache the results


determineColumnType

public static int determineColumnType(Connection connection,
                                      String tableName,
                                      String columnName)
Return the java.sql.Types for a column NOTE: it may be smart to cache the results


isColumnTypeNumber

public static boolean isColumnTypeNumber(int type)
Determine if the java.sql.Types is a number


isColumnTypeDateOrTime

public static boolean isColumnTypeDateOrTime(int type)
Determine if the java.sql.Types is a date


isColumnTypeString

public static boolean isColumnTypeString(int type)
Determine if the java.sql.Types is a date


getDefaultValueFor

public static Object getDefaultValueFor(int lType)
Return a default value for all java.sql.Types

Parameters:
lType -
Returns:

getFieldLength

public static int getFieldLength(Connection connection,
                                 String tablename,
                                 String columnname)
Determines the length of a field using metadata

Parameters:
connection -
tablename -
columnname -
Returns:
size or -1 when unknown

getFieldLength

public static int getFieldLength(Connection connection,
                                 String tablenamePlusColumnname)
Determines the length of a field using metadata

Parameters:
connection -
tablenamePlusColumnname - tablename.columnname
Returns:
size or -1 when unknown

main

public static void main(String[] args)
Some quick unit tests



Copyright © 2012 KnowledgePlaza. All Rights Reserved.