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)
Gets the catalog.
|
static java.lang.String |
getCatalog(javax.sql.DataSource dataSource)
Gets the catalog.
|
static int |
getColumnIndex(java.sql.ResultSetMetaData metaData,
java.lang.String name)
Gets the column index.
|
static int |
getColumnIndex(java.sql.ResultSet rs,
java.lang.String name)
Gets the column index.
|
static java.lang.String |
getColumnName(java.sql.ResultSet rs,
int columnIndex)
Gets the column name.
|
static java.lang.String |
getColumnName(java.sql.ResultSetMetaData metaData,
int columnIndex)
Gets the column name.
|
static java.sql.Connection |
getConnection(javax.sql.DataSource dataSource)
Gets the connection.
|
static ConnectionWrapper |
getConnection(java.lang.String driverClassName,
java.lang.String uri)
Gets the connection.
|
static ConnectionWrapper |
getConnection(java.lang.String driverClassName,
java.lang.String uri,
java.lang.String username,
java.lang.String password)
Gets the connection.
|
static ConnectionWrapper |
getConnectionWrapper(javax.sql.DataSource dataSource)
Gets the connection wrapper.
|
static java.util.List<java.lang.Object[]> |
getResultSetArrays(java.sql.ResultSet rs)
Gets the result set arrays.
|
static java.util.List<java.lang.Object[]> |
getResultSetArrays(ResultSetWrapper rs)
Gets the result set arrays.
|
static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
getResultSetMaps(java.sql.ResultSet rs)
Gets the result set maps.
|
static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
getResultSetMaps(java.sql.ResultSet rs,
SqlTypeMappingManager manager)
Gets the result set maps.
|
static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
getResultSetMaps(ResultSetWrapper rs)
Gets the result set maps.
|
static <E> java.util.List<E> |
getResultSetObjects(java.sql.ResultSet rs,
cn.featherfly.common.repository.mapping.RowMapper<E> mapper)
Gets the result set objects.
|
static <E> java.util.List<E> |
getResultSetObjects(ResultSetWrapper rs,
cn.featherfly.common.repository.mapping.RowMapper<E> mapper)
Gets the result set objects.
|
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 java.sql.SQLType |
getResultSQLType(java.sql.ResultSet rs,
int index)
Gets the result SQL type.
|
static java.sql.SQLType |
getResultSQLType(ResultSetWrapper rs,
int index)
Gets the result SQL type.
|
static java.lang.String |
getTableName(java.sql.ResultSet rs,
int columnIndex)
Gets the table name.
|
static java.lang.String |
getTableName(java.sql.ResultSetMetaData metaData,
int columnIndex)
Gets the table name.
|
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(java.sql.PreparedStatement prep,
int position,
java.lang.Object value,
boolean enumWithOridinal)
设置参数
.
|
static void |
setParameter(PreparedStatementWrapper prep,
int position,
java.lang.Object value)
设置参数
.
|
static void |
setParameter(PreparedStatementWrapper prep,
int position,
java.lang.Object value,
boolean enumWithOridinal)
设置参数
.
|
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)
Warp connection.
|
static DataSourceWrapper |
warpDataSource(javax.sql.DataSource dataSource)
Warp data source.
|
public static ConnectionWrapper getConnection(java.lang.String driverClassName, java.lang.String uri, java.lang.String username, java.lang.String password)
driverClassName - the driver class nameuri - the uriusername - the usernamepassword - the passwordpublic static ConnectionWrapper getConnection(java.lang.String driverClassName, java.lang.String uri)
driverClassName - the driver class nameuri - the uripublic 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)
dataSource - the data sourcepublic static ConnectionWrapper warpConnection(java.sql.Connection connection)
connection - the connectionpublic static java.sql.Connection getConnection(javax.sql.DataSource dataSource)
dataSource - the data sourcepublic static ConnectionWrapper getConnectionWrapper(javax.sql.DataSource dataSource)
dataSource - the data sourcepublic static java.lang.String getCatalog(javax.sql.DataSource dataSource)
dataSource - the data sourcepublic static java.lang.String getCatalog(java.sql.Connection conn)
conn - the connpublic 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(PreparedStatementWrapper prep, int position, java.lang.Object value, boolean enumWithOridinal)
设置参数
.prep - PreparedStatementWrapperposition - 占位符位置value - 参数enumWithOridinal - enum with oridinalpublic static void setParameter(java.sql.PreparedStatement prep,
int position,
java.lang.Object value)
设置参数
.prep - PreparedStatementposition - 占位符位置value - 参数public static void setParameter(java.sql.PreparedStatement prep,
int position,
java.lang.Object value,
boolean enumWithOridinal)
设置参数
.prep - PreparedStatementposition - 占位符位置value - 参数enumWithOridinal - enum with oridinalpublic static java.lang.String getColumnName(java.sql.ResultSet rs,
int columnIndex)
rs - the rscolumnIndex - the column indexpublic static java.lang.String getColumnName(java.sql.ResultSetMetaData metaData,
int columnIndex)
metaData - the meta datacolumnIndex - the column indexpublic static java.lang.String getTableName(java.sql.ResultSet rs,
int columnIndex)
rs - the rscolumnIndex - the column indexpublic static java.lang.String getTableName(java.sql.ResultSetMetaData metaData,
int columnIndex)
metaData - the meta datacolumnIndex - the column indexpublic static int getColumnIndex(java.sql.ResultSet rs,
java.lang.String name)
rs - the rsname - the namepublic static int getColumnIndex(java.sql.ResultSetMetaData metaData,
java.lang.String name)
metaData - the meta dataname - the namepublic static java.sql.SQLType getResultSQLType(ResultSetWrapper rs, int index)
rs - the rsindex - the indexpublic static java.sql.SQLType getResultSQLType(java.sql.ResultSet rs,
int index)
rs - the rsindex - the indexpublic 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)
E - the element typers - the rsmapper - the mapperpublic static <E> java.util.List<E> getResultSetObjects(java.sql.ResultSet rs,
cn.featherfly.common.repository.mapping.RowMapper<E> mapper)
E - the element typers - the rsmapper - the mapperpublic static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> getResultSetMaps(ResultSetWrapper rs)
rs - the rspublic static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> getResultSetMaps(java.sql.ResultSet rs)
rs - the rspublic static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> getResultSetMaps(java.sql.ResultSet rs,
SqlTypeMappingManager manager)
rs - the rsmanager - the managerpublic static java.util.List<java.lang.Object[]> getResultSetArrays(ResultSetWrapper rs)
rs - the rspublic static java.util.List<java.lang.Object[]> getResultSetArrays(java.sql.ResultSet rs)
rs - the rspublic 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