类 JdbcUtils

java.lang.Object
cn.featherfly.common.db.JdbcUtils

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, StatementResultSet.
    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 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 java.sql.Connection getConnection​(javax.sql.DataSource dataSource)
    Gets the connection.
    static ConnectionWrapper getConnectionWrapper​(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.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​(ResultSetWrapper rs, int index)
    Gets the result SQL type.
    static java.sql.SQLType getResultSQLType​(java.sql.ResultSet rs, int index)
    Gets the result SQL 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​(PreparedStatementWrapper prep, int position, java.lang.Object value)
    设置参数
    static void setParameter​(PreparedStatementWrapper prep, int position, java.lang.Object value, boolean enumWithOridinal)
    设置参数
    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 setParameters​(PreparedStatementWrapper prep, java.lang.Object... values)
    设置参数
    static void setParameters​(java.sql.PreparedStatement prep, java.lang.Object... values)
    设置参数
    static ConnectionWrapper warpConnection​(java.sql.Connection connection)
    Warp connection.
    static DataSourceWrapper warpDataSource​(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 name
      uri - the uri
      username - the username
      password - the password
      返回:
      the connection
    • getConnection

      public static ConnectionWrapper getConnection​(java.lang.String driverClassName, java.lang.String uri)
      Gets the connection.
      参数:
      driverClassName - the driver class name
      uri - 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, StatementResultSet. 忽略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 true if the driver was found, otherwise false
    • 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 of
      pw - 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 from
      pw - PrintWriter to print to
    • rollback

      public static void rollback​(java.sql.Connection conn) throws java.sql.SQLException
      Rollback 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.SQLException
      Performs a rollback on the 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 occurs
      从以下版本开始:
      DbUtils 1.1
    • rollbackAndCloseQuietly

      public 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.
      参数:
      conn - Connection to rollback. A null value is legal.
      从以下版本开始:
      DbUtils 1.1
    • warpDataSource

      public static DataSourceWrapper warpDataSource​(javax.sql.DataSource dataSource)
      Warp data source.
      参数:
      dataSource - the data source
      返回:
      the data source wrapper
    • warpConnection

      public static ConnectionWrapper warpConnection​(java.sql.Connection connection)
      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

      public static ConnectionWrapper getConnectionWrapper​(javax.sql.DataSource dataSource)
      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

      public static void setParameters​(PreparedStatementWrapper prep, java.lang.Object... values)

      设置参数

      .
      参数:
      prep - PreparedStatementWrapper
      values - 参数
    • setParameters

      public static void setParameters​(java.sql.PreparedStatement prep, java.lang.Object... values)

      设置参数

      .
      参数:
      prep - PreparedStatement
      values - 参数
    • setParameter

      public static void setParameter​(PreparedStatementWrapper prep, int position, java.lang.Object value)

      设置参数

      .
      参数:
      prep - PreparedStatementWrapper
      position - 占位符位置
      value - 参数
    • setParameter

      public static void setParameter​(PreparedStatementWrapper prep, int position, java.lang.Object value, boolean enumWithOridinal)

      设置参数

      .
      参数:
      prep - PreparedStatementWrapper
      position - 占位符位置
      value - 参数
      enumWithOridinal - enum with oridinal
    • setParameter

      public static void setParameter​(java.sql.PreparedStatement prep, int position, java.lang.Object value)

      设置参数

      .
      参数:
      prep - PreparedStatement
      position - 占位符位置
      value - 参数
    • setParameter

      public static void setParameter​(java.sql.PreparedStatement prep, int position, java.lang.Object value, boolean enumWithOridinal)

      设置参数

      .
      参数:
      prep - PreparedStatement
      position - 占位符位置
      value - 参数
      enumWithOridinal - enum with oridinal
    • getColumnIndex

      public static int getColumnIndex​(java.sql.ResultSet rs, java.lang.String name)
      Gets the column index.
      参数:
      rs - the rs
      name - 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 data
      name - the name
      返回:
      the column index
    • getResultSQLType

      public static java.sql.SQLType getResultSQLType​(ResultSetWrapper rs, int index)
      Gets the result SQL type.
      参数:
      rs - the rs
      index - 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 rs
      index - 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 data
      index - is the column index
      requiredType - the required value type (may be null)
      返回:
      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 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 data
      index - 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 rs
      mapper - 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 rs
      mapper - 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

      public static java.util.List<java.lang.Object[]> getResultSetArrays​(ResultSetWrapper rs)
      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 use
      columnIndex - the index of the column for the look up
      返回:
      the column name to use