类 JdbcUtils
public final class JdbcUtils
extends java.lang.Object
JDBC操作的工具类
.-
方法概要
修饰符和类型 方法 说明 static voidclose(java.sql.Connection conn)关闭Connection, 忽略null的情况.static voidclose(java.sql.ResultSet rs)关闭ResultSet, 忽略null的情况.static voidclose(java.sql.Statement stmt)关闭Statement, 忽略null的情况.static voidcloseQuietly(java.sql.Connection conn)关闭Connection, 忽略null的情况以及隐藏掉所有发生的SQL异常.static voidcloseQuietly(java.sql.Connection conn, java.sql.Statement stmt, java.sql.ResultSet rs)关闭Connection,Statement和ResultSet.static voidcloseQuietly(java.sql.ResultSet rs)关闭ResultSet, 忽略null的情况以及隐藏掉所有发生的SQL异常.static voidcloseQuietly(java.sql.Statement stmt)关闭Statement, 忽略null的情况以及隐藏掉所有发生的SQL异常.static voidcommitAndClose(java.sql.Connection conn)提交Connection以后关闭, 忽略null的情况.static voidcommitAndCloseQuietly(java.sql.Connection conn)提交Connection以后关闭, 忽略null的情况以及隐藏掉所有发生的SQL异常.static java.lang.StringgetCatalog(java.sql.Connection conn)Gets the catalog.static java.lang.StringgetCatalog(javax.sql.DataSource dataSource)Gets the catalog.static intgetColumnIndex(java.sql.ResultSetMetaData metaData, java.lang.String name)Gets the column index.static intgetColumnIndex(java.sql.ResultSet rs, java.lang.String name)Gets the column index.static ConnectionWrappergetConnection(java.lang.String driverClassName, java.lang.String uri)Gets the connection.static ConnectionWrappergetConnection(java.lang.String driverClassName, java.lang.String uri, java.lang.String username, java.lang.String password)Gets the connection.static java.sql.ConnectiongetConnection(javax.sql.DataSource dataSource)Gets the connection.static ConnectionWrappergetConnectionWrapper(javax.sql.DataSource dataSource)Gets the connection wrapper.static java.util.List<java.lang.Object[]>getResultSetArrays(ResultSetWrapper rs)Gets the result set arrays.static java.util.List<java.lang.Object[]>getResultSetArrays(java.sql.ResultSet rs)Gets the result set arrays.static java.util.List<java.util.Map<java.lang.String,java.lang.Object>>getResultSetMaps(ResultSetWrapper rs)Gets the result set maps.static java.util.List<java.util.Map<java.lang.String,java.lang.Object>>getResultSetMaps(java.sql.ResultSet rs)Gets the result set maps.static <E> java.util.List<E>getResultSetObjects(ResultSetWrapper rs, cn.featherfly.common.repository.mapping.RowMapper<E> mapper)Gets the result set objects.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 java.lang.ObjectgetResultSetValue(java.sql.ResultSet rs, int index)Retrieve a JDBC column value from a ResultSet, using the most appropriate value type.static java.lang.ObjectgetResultSetValue(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.SQLTypegetResultSQLType(ResultSetWrapper rs, int index)Gets the result SQL type.static java.sql.SQLTypegetResultSQLType(java.sql.ResultSet rs, int index)Gets the result SQL type.static booleanloadDriver(java.lang.String driverClassName)Loads and registers a database driver class.static java.lang.StringlookupColumnName(java.sql.ResultSetMetaData resultSetMetaData, int columnIndex)Determine the column name to use.static voidprintStackTrace(java.sql.SQLException e)Print the stack trace for a SQLException to STDERR.static voidprintStackTrace(java.sql.SQLException e, java.io.PrintWriter pw)Print the stack trace for a SQLException to a specified PrintWriter.static voidprintWarnings(java.sql.Connection conn)Print warnings on a Connection to STDERR.static voidprintWarnings(java.sql.Connection conn, java.io.PrintWriter pw)Print warnings on a Connection to a specified PrintWriter.static voidrollback(java.sql.Connection conn)Rollback any changes made on the given connection.static voidrollbackAndClose(java.sql.Connection conn)Performs a rollback on theConnectionthen closes it, avoid closing if null.static voidrollbackAndCloseQuietly(java.sql.Connection conn)Performs a rollback on theConnectionthen closes it, avoid closing if null and hide any SQLExceptions that occur.static voidsetParameter(PreparedStatementWrapper prep, int position, java.lang.Object value)设置参数static voidsetParameter(PreparedStatementWrapper prep, int position, java.lang.Object value, boolean enumWithOridinal)设置参数static voidsetParameter(java.sql.PreparedStatement prep, int position, java.lang.Object value)设置参数static voidsetParameter(java.sql.PreparedStatement prep, int position, java.lang.Object value, boolean enumWithOridinal)设置参数static voidsetParameters(PreparedStatementWrapper prep, java.lang.Object... values)设置参数static voidsetParameters(java.sql.PreparedStatement prep, java.lang.Object... values)设置参数static ConnectionWrapperwarpConnection(java.sql.Connection connection)Warp connection.static DataSourceWrapperwarpDataSource(javax.sql.DataSource dataSource)Warp data source.从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
方法详细资料
-
getConnection
public static ConnectionWrapper getConnection(java.lang.String driverClassName, java.lang.String uri, java.lang.String username, java.lang.String password)Gets the connection.- 参数:
driverClassName- the driver class nameuri- the uriusername- the usernamepassword- the password- 返回:
- the connection
-
getConnection
public static ConnectionWrapper getConnection(java.lang.String driverClassName, java.lang.String uri)Gets the connection.- 参数:
driverClassName- the driver class nameuri- the uri- 返回:
- the connection
-
close
public static void close(java.sql.Connection conn)关闭Connection, 忽略null的情况.- 参数:
conn- 需要关闭的连接.
-
close
public static void close(java.sql.ResultSet rs)关闭ResultSet, 忽略null的情况.- 参数:
rs- 需要关闭的结果集.
-
close
public static void close(java.sql.Statement stmt)关闭Statement, 忽略null的情况.- 参数:
stmt- 需要关闭的Statement.
-
closeQuietly
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.
-
closeQuietly
public static void closeQuietly(java.sql.Connection conn)关闭Connection, 忽略null的情况以及隐藏掉所有发生的SQL异常.- 参数:
conn- 要关闭的数据库连接.
-
closeQuietly
public static void closeQuietly(java.sql.ResultSet rs)关闭ResultSet, 忽略null的情况以及隐藏掉所有发生的SQL异常.- 参数:
rs- ResultSet.
-
closeQuietly
public static void closeQuietly(java.sql.Statement stmt)关闭Statement, 忽略null的情况以及隐藏掉所有发生的SQL异常.- 参数:
stmt- Statement.
-
commitAndClose
public static void commitAndClose(java.sql.Connection conn)提交Connection以后关闭, 忽略null的情况.- 参数:
conn- Connection
-
commitAndCloseQuietly
public static void commitAndCloseQuietly(java.sql.Connection conn)提交Connection以后关闭, 忽略null的情况以及隐藏掉所有发生的SQL异常.- 参数:
conn- Connection to close.
-
loadDriver
public static boolean loadDriver(java.lang.String driverClassName)Loads and registers a database driver class. If this succeeds, it returns true, else it returns false.- 参数:
driverClassName- of driver to load- 返回:
- boolean
trueif the driver was found, otherwisefalse
-
printStackTrace
public static void printStackTrace(java.sql.SQLException e)Print the stack trace for a SQLException to STDERR.- 参数:
e- SQLException to print stack trace of
-
printStackTrace
public static void printStackTrace(java.sql.SQLException e, java.io.PrintWriter pw)Print the stack trace for a SQLException to a specified PrintWriter.- 参数:
e- SQLException to print stack trace ofpw- PrintWriter to print to
-
printWarnings
public static void printWarnings(java.sql.Connection conn)Print warnings on a Connection to STDERR.- 参数:
conn- Connection to print warnings from
-
printWarnings
public static void printWarnings(java.sql.Connection conn, java.io.PrintWriter pw)Print warnings on a Connection to a specified PrintWriter.- 参数:
conn- Connection to print warnings frompw- PrintWriter to print to
-
rollback
public static void rollback(java.sql.Connection conn) throws java.sql.SQLExceptionRollback any changes made on the given connection.- 参数:
conn- Connection to rollback. A null value is legal.- 抛出:
java.sql.SQLException- if a database access error occurs
-
rollbackAndClose
public static void rollbackAndClose(java.sql.Connection conn) throws java.sql.SQLExceptionPerforms a rollback on theConnectionthen closes it, avoid closing if null.- 参数:
conn- Connection to rollback. A null value is legal.- 抛出:
java.sql.SQLException- if a database access error occurs- 从以下版本开始:
- DbUtils 1.1
-
rollbackAndCloseQuietly
public static void rollbackAndCloseQuietly(java.sql.Connection conn)Performs a rollback on theConnectionthen closes it, avoid closing if null and hide any SQLExceptions that occur.- 参数:
conn- Connection to rollback. A null value is legal.- 从以下版本开始:
- DbUtils 1.1
-
warpDataSource
Warp data source.- 参数:
dataSource- the data source- 返回:
- the data source wrapper
-
warpConnection
Warp connection.- 参数:
connection- the connection- 返回:
- the connection wrapper
-
getConnection
public static java.sql.Connection getConnection(javax.sql.DataSource dataSource)Gets the connection.- 参数:
dataSource- the data source- 返回:
- the connection
-
getConnectionWrapper
Gets the connection wrapper.- 参数:
dataSource- the data source- 返回:
- the connection wrapper
-
getCatalog
public static java.lang.String getCatalog(javax.sql.DataSource dataSource)Gets the catalog.- 参数:
dataSource- the data source- 返回:
- the catalog
-
getCatalog
public static java.lang.String getCatalog(java.sql.Connection conn)Gets the catalog.- 参数:
conn- the conn- 返回:
- the catalog
-
setParameters
设置参数
.- 参数:
prep- PreparedStatementWrappervalues- 参数
-
setParameters
public static void setParameters(java.sql.PreparedStatement prep, java.lang.Object... values)设置参数
.- 参数:
prep- PreparedStatementvalues- 参数
-
setParameter
public static void setParameter(PreparedStatementWrapper prep, int position, java.lang.Object value)设置参数
.- 参数:
prep- PreparedStatementWrapperposition- 占位符位置value- 参数
-
setParameter
public static void setParameter(PreparedStatementWrapper prep, int position, java.lang.Object value, boolean enumWithOridinal)设置参数
.- 参数:
prep- PreparedStatementWrapperposition- 占位符位置value- 参数enumWithOridinal- enum with oridinal
-
setParameter
public static void setParameter(java.sql.PreparedStatement prep, int position, java.lang.Object value)设置参数
.- 参数:
prep- PreparedStatementposition- 占位符位置value- 参数
-
setParameter
public static void setParameter(java.sql.PreparedStatement prep, int position, java.lang.Object value, boolean enumWithOridinal)设置参数
.- 参数:
prep- PreparedStatementposition- 占位符位置value- 参数enumWithOridinal- enum with oridinal
-
getColumnIndex
public static int getColumnIndex(java.sql.ResultSet rs, java.lang.String name)Gets the column index.- 参数:
rs- the rsname- the name- 返回:
- the column index
-
getColumnIndex
public static int getColumnIndex(java.sql.ResultSetMetaData metaData, java.lang.String name)Gets the column index.- 参数:
metaData- the meta dataname- the name- 返回:
- the column index
-
getResultSQLType
Gets the result SQL type.- 参数:
rs- the rsindex- the index- 返回:
- the result SQL type
-
getResultSQLType
public static java.sql.SQLType getResultSQLType(java.sql.ResultSet rs, int index)Gets the result SQL type.- 参数:
rs- the rsindex- the index- 返回:
- the result SQL type
-
getResultSetValue
public 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.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 benull)- 返回:
- the value object
-
getResultSetValue
public 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. The returned value should be a detached value object, not having any ties to the active ResultSet: in particular, it should not be a Blob or Clob object but rather a byte array respectively String representation.Uses the
getObject(index)method, but includes additional "hacks" to get around Oracle 10g returning a non-standard object for its TIMESTAMP datatype and ajava.sql.Datefor DATE columns leaving out the time portion: These columns will explicitly be extracted as standardjava.sql.Timestampobject.- 参数:
rs- is the ResultSet holding the dataindex- is the column index- 返回:
- the value object
- 另请参阅:
Blob,Clob,Timestamp
-
getResultSetObjects
public static <E> java.util.List<E> getResultSetObjects(ResultSetWrapper rs, cn.featherfly.common.repository.mapping.RowMapper<E> mapper)Gets the result set objects.- 类型参数:
E- the element type- 参数:
rs- the rsmapper- the mapper- 返回:
- the result set objects
-
getResultSetObjects
public static <E> java.util.List<E> getResultSetObjects(java.sql.ResultSet rs, cn.featherfly.common.repository.mapping.RowMapper<E> mapper)Gets the result set objects.- 类型参数:
E- the element type- 参数:
rs- the rsmapper- the mapper- 返回:
- the result set objects
-
getResultSetMaps
public static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> getResultSetMaps(ResultSetWrapper rs)Gets the result set maps.- 参数:
rs- the rs- 返回:
- the result set maps
-
getResultSetMaps
public static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> getResultSetMaps(java.sql.ResultSet rs)Gets the result set maps.- 参数:
rs- the rs- 返回:
- the result set maps
-
getResultSetArrays
Gets the result set arrays.- 参数:
rs- the rs- 返回:
- the result set arrays
-
getResultSetArrays
public static java.util.List<java.lang.Object[]> getResultSetArrays(java.sql.ResultSet rs)Gets the result set arrays.- 参数:
rs- the rs- 返回:
- the result set arrays
-
lookupColumnName
public static java.lang.String lookupColumnName(java.sql.ResultSetMetaData resultSetMetaData, int columnIndex)Determine the column name to use. The column name is determined based on a lookup using ResultSetMetaData.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- 返回:
- the column name to use
-