org.eclipse.net4j.db
Class DBUtil

java.lang.Object
  extended by org.eclipse.net4j.db.DBUtil

public final class DBUtil
extends Object

A utility class with various static factory and convenience methods.


Nested Class Summary
static interface DBUtil.DeserializeRowHandler
          A row handler with a method that is called once per row deserialized within DBUtil.deserializeTable().
static interface DBUtil.RowHandler
          Call-back interface with a method that is called after a number of table rows have been handled by one of the subtypes of this interface.
static interface DBUtil.SerializeRowHandler
          A row handler with a method that is called once per row serialized within DBUtil.serializeTable().
 
Method Summary
static int clearTable(Connection connection, IDBTable table)
           
static int clearTable(Connection connection, String tableName)
           
static Exception close(Connection connection)
           
static Exception close(ResultSet resultSet)
           
static Exception close(Statement statement)
           
static IDBConnectionProvider createConnectionProvider(DataSource dataSource)
           
static DataSource createDataSource(Map<Object,Object> properties)
           
static DataSource createDataSource(Map<Object,Object> properties, String namespace)
           
static DataSource createDataSource(Map<Object,Object> properties, String namespace, String driverClassKey)
           
static IDBSchema createSchema(String name)
           
static void deserializeTable(ExtendedDataInput in, Connection connection, IDBTable table, OMMonitor monitor)
           
static void deserializeTable(ExtendedDataInput in, Connection connection, IDBTable table, OMMonitor monitor, DBUtil.DeserializeRowHandler handler)
           
static List<Exception> dropAllTables(Connection connection, String dbName)
           
static void executeBatch(PreparedStatement stmt, int counter)
           
static void executeBatch(PreparedStatement stmt, int counter, boolean checkExactlyOne)
           
static List<String> getAllSchemaTableNames(Connection connection)
           
static List<String> getAllSchemaTableNames(DatabaseMetaData metaData)
           
static List<String> getAllTableNames(Connection connection, String dbName)
           
static IDBAdapter getDBAdapter(String adapterName)
          Can only be used when Eclipse is running.
static int getRowCount(ResultSet resultSet)
          Returns the number of rows contained in the given result set.
static int select(Connection connection, IDBRowHandler rowHandler, IDBField... fields)
           
static int select(Connection connection, IDBRowHandler rowHandler, String where, IDBField... fields)
           
static Object[] select(Connection connection, String where, IDBField... fields)
           
static int selectMaximumInt(Connection connection, IDBField field, String... where)
           
static long selectMaximumLong(Connection connection, IDBField field, String... where)
           
static int selectMinimumInt(Connection connection, IDBField field, String... where)
           
static long selectMinimumLong(Connection connection, IDBField field, String... where)
           
static void serializeTable(ExtendedDataOutput out, Connection connection, IDBTable table, String tableAlias, String sqlSuffix)
           
static void serializeTable(ExtendedDataOutput out, Connection connection, IDBTable table, String tableAlias, String sqlSuffix, DBUtil.SerializeRowHandler handler)
           
static void sqlDump(Connection conn, String sql)
          Deprecated. Should only be used when debugging.
static void sqlDump(IDBConnectionProvider connectionProvider, String sql)
          Deprecated. Should only be used when debugging.
static String trace(String sql)
           
static int update(Connection connection, String sql)
           
static int update(PreparedStatement stmt, boolean exactlyOne)
          Execute update on the given prepared statement and handle common cases of return values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

sqlDump

@Deprecated
public static void sqlDump(Connection conn,
                                      String sql)
Deprecated. Should only be used when debugging.

For debugging purposes ONLY!

Since:
3.0

sqlDump

@Deprecated
public static void sqlDump(IDBConnectionProvider connectionProvider,
                                      String sql)
Deprecated. Should only be used when debugging.

For debugging purposes ONLY!

Since:
3.0

createSchema

public static IDBSchema createSchema(String name)

createDataSource

public static DataSource createDataSource(Map<Object,Object> properties)

createDataSource

public static DataSource createDataSource(Map<Object,Object> properties,
                                          String namespace)

createDataSource

public static DataSource createDataSource(Map<Object,Object> properties,
                                          String namespace,
                                          String driverClassKey)

createConnectionProvider

public static IDBConnectionProvider createConnectionProvider(DataSource dataSource)

getDBAdapter

public static IDBAdapter getDBAdapter(String adapterName)
Can only be used when Eclipse is running. In standalone scenarios create the adapter instance by directly calling the constructor of the adapter class.


close

public static Exception close(Connection connection)

close

public static Exception close(Statement statement)

close

public static Exception close(ResultSet resultSet)

getAllSchemaTableNames

public static List<String> getAllSchemaTableNames(Connection connection)
Since:
3.0

getAllSchemaTableNames

public static List<String> getAllSchemaTableNames(DatabaseMetaData metaData)
Since:
3.0

getAllTableNames

public static List<String> getAllTableNames(Connection connection,
                                            String dbName)

dropAllTables

public static List<Exception> dropAllTables(Connection connection,
                                            String dbName)
Since:
4.0

selectMinimumInt

public static int selectMinimumInt(Connection connection,
                                   IDBField field,
                                   String... where)
                            throws DBException
Throws:
DBException
Since:
3.0

selectMinimumLong

public static long selectMinimumLong(Connection connection,
                                     IDBField field,
                                     String... where)
                              throws DBException
Throws:
DBException
Since:
3.0

selectMaximumInt

public static int selectMaximumInt(Connection connection,
                                   IDBField field,
                                   String... where)
                            throws DBException
Throws:
DBException
Since:
3.0

selectMaximumLong

public static long selectMaximumLong(Connection connection,
                                     IDBField field,
                                     String... where)
                              throws DBException
Throws:
DBException
Since:
3.0

executeBatch

public static void executeBatch(PreparedStatement stmt,
                                int counter)
Since:
4.1

executeBatch

public static void executeBatch(PreparedStatement stmt,
                                int counter,
                                boolean checkExactlyOne)
Since:
4.1

update

public static int update(Connection connection,
                         String sql)

update

public static int update(PreparedStatement stmt,
                         boolean exactlyOne)
                  throws SQLException
Execute update on the given prepared statement and handle common cases of return values.

Parameters:
stmt - the prepared statement
exactlyOne - if true, the update count is checked to be 1. Else the update result is only checked so that the update was successful (i.e. result code != Statement.EXECUTE_FAILED).
Returns:
the update count / execution result as returned by PreparedStatement.executeUpdate(). Can be used by the caller to perform more advanced checks.
Throws:
SQLException - if PreparedStatement.executeUpdate() throws it.
IllegalStateException - if the check indicated by excatlyOne indicates an error.
Since:
4.0

clearTable

public static int clearTable(Connection connection,
                             IDBTable table)
Since:
4.1

clearTable

public static int clearTable(Connection connection,
                             String tableName)
Since:
4.1

select

public static int select(Connection connection,
                         IDBRowHandler rowHandler,
                         String where,
                         IDBField... fields)
                  throws DBException
Throws:
DBException

select

public static int select(Connection connection,
                         IDBRowHandler rowHandler,
                         IDBField... fields)
                  throws DBException
Throws:
DBException

select

public static Object[] select(Connection connection,
                              String where,
                              IDBField... fields)
                       throws DBException
Throws:
DBException

getRowCount

public static int getRowCount(ResultSet resultSet)
                       throws DBException
Returns the number of rows contained in the given result set.

The statement of the result set must have been created with TYPE_SCROLL_INSENSITIVE.

Throws:
DBException
Since:
4.0

serializeTable

public static void serializeTable(ExtendedDataOutput out,
                                  Connection connection,
                                  IDBTable table,
                                  String tableAlias,
                                  String sqlSuffix)
                           throws DBException,
                                  IOException
Throws:
DBException
IOException
Since:
3.0

serializeTable

public static void serializeTable(ExtendedDataOutput out,
                                  Connection connection,
                                  IDBTable table,
                                  String tableAlias,
                                  String sqlSuffix,
                                  DBUtil.SerializeRowHandler handler)
                           throws DBException,
                                  IOException
Throws:
DBException
IOException
Since:
4.1

deserializeTable

public static void deserializeTable(ExtendedDataInput in,
                                    Connection connection,
                                    IDBTable table,
                                    OMMonitor monitor)
                             throws IOException
Throws:
IOException
Since:
4.0

deserializeTable

public static void deserializeTable(ExtendedDataInput in,
                                    Connection connection,
                                    IDBTable table,
                                    OMMonitor monitor,
                                    DBUtil.DeserializeRowHandler handler)
                             throws IOException
Throws:
IOException
Since:
4.1

trace

public static String trace(String sql)
Since:
3.0


Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.