public final class JdbcUtils
extends java.lang.Object
JDBC操作的工具类
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
close(java.sql.Connection conn)
关闭
Connection, 忽略null的情况. |
static void |
close(java.sql.ResultSet rs)
关闭
ResultSet, 忽略null的情况. |
static void |
close(java.sql.Statement stmt)
关闭
Statement, 忽略null的情况. |
static void |
closeQuietly(java.sql.Connection conn)
关闭
Connection, 忽略null的情况以及隐藏掉所有发生的SQL异常. |
static void |
closeQuietly(java.sql.Connection conn,
java.sql.Statement stmt,
java.sql.ResultSet rs)
关闭
Connection, Statement 和
ResultSet. |
static void |
closeQuietly(java.sql.ResultSet rs)
关闭
ResultSet, 忽略null的情况以及隐藏掉所有发生的SQL异常. |
static void |
closeQuietly(java.sql.Statement stmt)
关闭
Statement, 忽略null的情况以及隐藏掉所有发生的SQL异常. |
static void |
commitAndClose(java.sql.Connection conn)
提交
Connection以后关闭, 忽略null的情况. |
static void |
commitAndCloseQuietly(java.sql.Connection conn)
提交
Connection以后关闭, 忽略null的情况以及隐藏掉所有发生的SQL异常. |
static java.lang.String |
getCatalog(java.sql.Connection conn) |
static java.lang.String |
getCatalog(javax.sql.DataSource dataSource) |
static java.sql.Connection |
getConnection(javax.sql.DataSource dataSource) |
static ConnectionWrapper |
getConnectionWrapper(javax.sql.DataSource dataSource) |
static java.util.List<java.lang.Object[]> |
getResultSetArrays(java.sql.ResultSet rs) |
static java.util.List<java.lang.Object[]> |
getResultSetArrays(ResultSetWrapper rs) |
static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
getResultSetMaps(java.sql.ResultSet rs) |
static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
getResultSetMaps(ResultSetWrapper rs) |
static <E> java.util.List<E> |
getResultSetObjects(java.sql.ResultSet rs,
cn.featherfly.common.repository.mapping.RowMapper<E> mapper) |
static <E> java.util.List<E> |
getResultSetObjects(ResultSetWrapper rs,
cn.featherfly.common.repository.mapping.RowMapper<E> mapper) |
static java.lang.Object |
getResultSetValue(java.sql.ResultSet rs,
int index)
Retrieve a JDBC column value from a ResultSet, using the most appropriate
value type.
|
static java.lang.Object |
getResultSetValue(java.sql.ResultSet rs,
int index,
java.lang.Class<?> requiredType)
Retrieve a JDBC column value from a ResultSet, using the specified value
type.
|
static boolean |
loadDriver(java.lang.String driverClassName)
Loads and registers a database driver class.
|
static java.lang.String |
lookupColumnName(java.sql.ResultSetMetaData resultSetMetaData,
int columnIndex)
Determine the column name to use.
|
static void |
printStackTrace(java.sql.SQLException e)
Print the stack trace for a SQLException to STDERR.
|
static void |
printStackTrace(java.sql.SQLException e,
java.io.PrintWriter pw)
Print the stack trace for a SQLException to a specified PrintWriter.
|
static void |
printWarnings(java.sql.Connection conn)
Print warnings on a Connection to STDERR.
|
static void |
printWarnings(java.sql.Connection conn,
java.io.PrintWriter pw)
Print warnings on a Connection to a specified PrintWriter.
|
static void |
rollback(java.sql.Connection conn)
Rollback any changes made on the given connection.
|
static void |
rollbackAndClose(java.sql.Connection conn)
Performs a rollback on the
Connection then closes it, avoid
closing if null. |
static void |
rollbackAndCloseQuietly(java.sql.Connection conn)
Performs a rollback on the
Connection then closes it, avoid
closing if null and hide any SQLExceptions that occur. |
static void |
setParameter(java.sql.PreparedStatement prep,
int position,
java.lang.Object value)
设置参数
|
static void |
setParameter(PreparedStatementWrapper prep,
int position,
java.lang.Object value)
设置参数
|
static void |
setParameters(java.sql.PreparedStatement prep,
java.lang.Object... values)
设置参数
|
static void |
setParameters(PreparedStatementWrapper prep,
java.lang.Object... values)
设置参数
|
static ConnectionWrapper |
warpConnection(java.sql.Connection connection) |
static DataSourceWrapper |
warpDataSource(javax.sql.DataSource dataSource) |
public static void close(java.sql.Connection conn)
Connection, 忽略null的情况.conn - 需要关闭的连接.public static void close(java.sql.ResultSet rs)
ResultSet, 忽略null的情况.rs - 需要关闭的结果集.public static void close(java.sql.Statement stmt)
Statement, 忽略null的情况.stmt - 需要关闭的Statement.public static void closeQuietly(java.sql.Connection conn,
java.sql.Statement stmt,
java.sql.ResultSet rs)
Connection, Statement 和
ResultSet. 忽略null的情况以及隐藏掉所有发生的SQL异常.conn - Connection.stmt - Statement.rs - ResultSet.public static void closeQuietly(java.sql.Connection conn)
Connection, 忽略null的情况以及隐藏掉所有发生的SQL异常.conn - 要关闭的数据库连接.public static void closeQuietly(java.sql.ResultSet rs)
ResultSet, 忽略null的情况以及隐藏掉所有发生的SQL异常.rs - ResultSet.public static void closeQuietly(java.sql.Statement stmt)
Statement, 忽略null的情况以及隐藏掉所有发生的SQL异常.stmt - Statement.public static void commitAndClose(java.sql.Connection conn)
Connection以后关闭, 忽略null的情况.conn - Connectionpublic static void commitAndCloseQuietly(java.sql.Connection conn)
Connection以后关闭, 忽略null的情况以及隐藏掉所有发生的SQL异常.conn - Connection to close.public static boolean loadDriver(java.lang.String driverClassName)
driverClassName - of driver to loadtrue if the driver was found, otherwise
falsepublic static void printStackTrace(java.sql.SQLException e)
e - SQLException to print stack trace ofpublic static void printStackTrace(java.sql.SQLException e,
java.io.PrintWriter pw)
e - SQLException to print stack trace ofpw - PrintWriter to print topublic static void printWarnings(java.sql.Connection conn)
conn - Connection to print warnings frompublic static void printWarnings(java.sql.Connection conn,
java.io.PrintWriter pw)
conn - Connection to print warnings frompw - PrintWriter to print topublic static void rollback(java.sql.Connection conn)
throws java.sql.SQLException
conn - Connection to rollback. A null value is legal.java.sql.SQLException - if a database access error occurspublic static void rollbackAndClose(java.sql.Connection conn)
throws java.sql.SQLException
Connection then closes it, avoid
closing if null.conn - Connection to rollback. A null value is legal.java.sql.SQLException - if a database access error occurspublic static void rollbackAndCloseQuietly(java.sql.Connection conn)
Connection then closes it, avoid
closing if null and hide any SQLExceptions that occur.conn - Connection to rollback. A null value is legal.public static DataSourceWrapper warpDataSource(javax.sql.DataSource dataSource)
public static ConnectionWrapper warpConnection(java.sql.Connection connection)
public static java.sql.Connection getConnection(javax.sql.DataSource dataSource)
public static ConnectionWrapper getConnectionWrapper(javax.sql.DataSource dataSource)
public static java.lang.String getCatalog(javax.sql.DataSource dataSource)
public static java.lang.String getCatalog(java.sql.Connection conn)
public static void setParameters(PreparedStatementWrapper prep, java.lang.Object... values)
设置参数
prep - PreparedStatementWrappervalues - 参数public static void setParameters(java.sql.PreparedStatement prep,
java.lang.Object... values)
设置参数
prep - PreparedStatementvalues - 参数public static void setParameter(PreparedStatementWrapper prep, int position, java.lang.Object value)
设置参数
prep - PreparedStatementWrapperposition - 占位符位置value - 参数public static void setParameter(java.sql.PreparedStatement prep,
int position,
java.lang.Object value)
设置参数
prep - PreparedStatementposition - 占位符位置value - 参数public static java.lang.Object getResultSetValue(java.sql.ResultSet rs,
int index,
java.lang.Class<?> requiredType)
Uses the specifically typed ResultSet accessor methods, falling back to
getResultSetValue(java.sql.ResultSet, int) for unknown types.
Note that the returned value may not be assignable to the specified required type, in case of an unknown type. Calling code needs to deal with this case appropriately, e.g. throwing a corresponding exception.
rs - is the ResultSet holding the dataindex - is the column indexrequiredType - the required value type (may be null)public static java.lang.Object getResultSetValue(java.sql.ResultSet rs,
int index)
Uses the getObject(index) method, but includes additional
"hacks" to get around Oracle 10g returning a non-standard object for its
TIMESTAMP datatype and a java.sql.Date for DATE columns
leaving out the time portion: These columns will explicitly be extracted
as standard java.sql.Timestamp object.
rs - is the ResultSet holding the dataindex - is the column indexBlob,
Clob,
Timestamppublic static <E> java.util.List<E> getResultSetObjects(ResultSetWrapper rs, cn.featherfly.common.repository.mapping.RowMapper<E> mapper)
public static <E> java.util.List<E> getResultSetObjects(java.sql.ResultSet rs,
cn.featherfly.common.repository.mapping.RowMapper<E> mapper)
public static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> getResultSetMaps(ResultSetWrapper rs)
public static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> getResultSetMaps(java.sql.ResultSet rs)
public static java.util.List<java.lang.Object[]> getResultSetArrays(ResultSetWrapper rs)
public static java.util.List<java.lang.Object[]> getResultSetArrays(java.sql.ResultSet rs)
public static java.lang.String lookupColumnName(java.sql.ResultSetMetaData resultSetMetaData,
int columnIndex)
This method implementation takes into account recent clarifications expressed in the JDBC 4.0 specification:
columnLabel - the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column.
resultSetMetaData - the current meta data to usecolumnIndex - the index of the column for the look up