public class JDBCUtil extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
JDBCUtil.BatchProcessor
|
Constructor and Description |
---|
JDBCUtil() |
Modifier and Type | Method and Description |
---|---|
static boolean |
addColumn(Connection conn,
String table,
String cname,
String cdef,
String afterCname)
Adds a column (with name 'cname' and definition 'cdef') to the specified table.
|
static boolean |
addIndexToTable(Connection conn,
String table,
String cname,
String iname)
Adds an index on the specified column (cname) to the specified table.
|
static void |
batchQuery(Connection conn,
String query,
Collection<?> keys,
boolean escapeKeys,
int batchSize,
JDBCUtil.BatchProcessor processor)
Issues a query with a potentially large number of keys in batches.
|
static void |
changeColumn(Connection conn,
String table,
String cname,
String cdef)
Changes a column's definition.
|
static void |
checkedUpdate(PreparedStatement stmt,
int expectedCount)
Calls
stmt.executeUpdate() on the supplied statement, checking to see that it
returns the expected update count and throwing a persistence exception if it does not. |
static void |
checkedUpdate(Statement stmt,
String query,
int expectedCount)
Calls
stmt.executeUpdate() on the supplied statement with the supplied query,
checking to see that it returns the expected update count and throwing a persistence
exception if it does not. |
static void |
close(Connection conn)
Closes the supplied JDBC connection and gracefully handles being passed null (by doing
nothing).
|
static void |
close(Statement stmt)
Closes the supplied JDBC statement and gracefully handles being passed null (by doing
nothing).
|
static boolean |
createTableIfMissing(Connection conn,
String table,
String[] definition,
String postamble)
Used to programatically create a database table.
|
static boolean |
dropColumn(Connection conn,
String table,
String cname)
Removes a column from the specified table.
|
static boolean |
dropIndex(Connection conn,
String table,
String cname,
String iname)
Removes a named index from the specified table.
|
static void |
dropPrimaryKey(Connection conn,
String table)
Removes the primary key from the specified table.
|
static String |
escape(Object[] values)
Escapes a list of values, separating the escaped values by commas.
|
static String |
escape(String text)
Escapes any single quotes in the supplied text and wraps it in single quotes to make it safe
for embedding into a database query.
|
static String |
getColumnDefaultValue(Connection conn,
String table,
String column)
Returns a string representation of the default value for the specified column in the
specified table.
|
static int |
getColumnSize(Connection conn,
String table,
String column)
Returns the size for the specified column in the specified table.
|
static int |
getColumnType(Connection conn,
String table,
String column)
Returns the type (as specified in
Types for the specified column in the
specified table. |
static String |
getIndexName(Connection conn,
String table,
String column)
Returns the name of the index for the specified column in the specified table.
|
static boolean |
isColumnNullable(Connection conn,
String table,
String column)
Determines whether or not the specified column accepts null values.
|
static String |
jigger(String text)
Many databases simply fail to handle Unicode text properly and this routine provides a
common workaround which is to represent a UTF-8 string as an ISO-8859-1 string.
|
static Connection |
makeCollector(Connection conn,
List<Statement> stmts)
Wraps the given connection in a proxied instance that will add all statements returned by
methods called on the proxy (such as
Connection.createStatement() ) to the supplied
list. |
String |
quote(Date date)
Converts the date to a string and surrounds it in single-quotes via the escape method.
|
static String |
safeJigger(String text)
Utility method to jigger the specified string so that it's safe to use in a regular
Statement.
|
static boolean |
tableContainsColumn(Connection conn,
String table,
String column)
Returns true if the table with the specified name exists and contains a column with the
specified name, false if either condition does not hold true.
|
static boolean |
tableContainsIndex(Connection conn,
String table,
String column,
String index)
Returns true if the index on the specified column exists for the specified table, false if
it does not.
|
static boolean |
tableContainsPrimaryKey(Connection conn,
String table,
String column)
Returns true if the specified table contains a primary key on the specified column.
|
static boolean |
tableExists(Connection conn,
String name)
Returns true if the table with the specified name exists, false if it does
not.
|
static String |
unjigger(String text)
Reverses
jigger(java.lang.String) . |
static void |
warnedUpdate(PreparedStatement stmt,
int expectedCount)
Calls
stmt.executeUpdate() on the supplied statement, checking to see that it
returns the expected update count and logging a warning if it does not. |
static void |
warnedUpdate(Statement stmt,
String query,
int expectedCount)
Calls
stmt.executeUpdate() on the supplied statement with the supplied query,
checking to see that it returns the expected update count and logging a warning if it does
not. |
public static void close(Statement stmt) throws SQLException
SQLException
public static void close(Connection conn) throws SQLException
SQLException
public static Connection makeCollector(Connection conn, List<Statement> stmts)
Connection.createStatement()
) to the supplied
list. Thus you can create the proxy, pass the proxy to code that creates and uses statements
and then close any statements created by the code that operated on that Connection before
returning it to a pool, for example.public static void checkedUpdate(PreparedStatement stmt, int expectedCount) throws SQLException, PersistenceException
stmt.executeUpdate()
on the supplied statement, checking to see that it
returns the expected update count and throwing a persistence exception if it does not.SQLException
PersistenceException
public static void checkedUpdate(Statement stmt, String query, int expectedCount) throws SQLException, PersistenceException
stmt.executeUpdate()
on the supplied statement with the supplied query,
checking to see that it returns the expected update count and throwing a persistence
exception if it does not.SQLException
PersistenceException
public static void warnedUpdate(PreparedStatement stmt, int expectedCount) throws SQLException
stmt.executeUpdate()
on the supplied statement, checking to see that it
returns the expected update count and logging a warning if it does not.SQLException
public static void batchQuery(Connection conn, String query, Collection<?> keys, boolean escapeKeys, int batchSize, JDBCUtil.BatchProcessor processor) throws SQLException
Collection<Integer> keys = ...;
String query = "select NAME from USERS where USER_ID in (#KEYS)";
JDBCUtil.BatchProcessor proc = new JDBCUtil.BatchProcessor() {
public void process (ResultSet row) {
String name = rs.getString(1);
// do whatever with name
}
};
JDBCUtil.batchQuery(conn, query, keys, false, 500, proc);
query
- the SQL query to run for each batch with the string #KEYS#
in the
place where the batch of keys should be substituted.escapeKeys
- if true, escape(java.lang.String)
will be called on each key to escape any
dangerous characters and wrap the key in quotes.batchSize
- the number of keys at a time to substitute in for #KEYS#
.SQLException
public static void warnedUpdate(Statement stmt, String query, int expectedCount) throws SQLException
stmt.executeUpdate()
on the supplied statement with the supplied query,
checking to see that it returns the expected update count and logging a warning if it does
not.SQLException
public String quote(Date date)
public static String escape(String text)
public static String escape(Object[] values)
escape(String)
.public static String jigger(String text)
public static String unjigger(String text)
jigger(java.lang.String)
.public static String safeJigger(String text)
public static boolean createTableIfMissing(Connection conn, String table, String[] definition, String postamble) throws SQLException
SQLException
public static boolean tableExists(Connection conn, String name) throws SQLException
SQLException
public static boolean tableContainsColumn(Connection conn, String table, String column) throws SQLException
SQLException
public static boolean tableContainsIndex(Connection conn, String table, String column, String index) throws SQLException
SQLException
public static boolean tableContainsPrimaryKey(Connection conn, String table, String column) throws SQLException
SQLException
public static String getIndexName(Connection conn, String table, String column) throws SQLException
SQLException
public static int getColumnType(Connection conn, String table, String column) throws SQLException
Types
for the specified column in the
specified table.SQLException
public static boolean isColumnNullable(Connection conn, String table, String column) throws SQLException
SQLException
public static int getColumnSize(Connection conn, String table, String column) throws SQLException
SQLException
public static String getColumnDefaultValue(Connection conn, String table, String column) throws SQLException
SQLException
public static boolean addColumn(Connection conn, String table, String cname, String cdef, String afterCname) throws SQLException
afterCname
- (optional) the name of the column after which to add the new column.SQLException
public static void changeColumn(Connection conn, String table, String cname, String cdef) throws SQLException
SQLException
public static boolean dropColumn(Connection conn, String table, String cname) throws SQLException
SQLException
public static boolean dropIndex(Connection conn, String table, String cname, String iname) throws SQLException
SQLException
public static void dropPrimaryKey(Connection conn, String table) throws SQLException
SQLException
public static boolean addIndexToTable(Connection conn, String table, String cname, String iname) throws SQLException
SQLException
Copyright © 2015. All rights reserved.