类 JdbcUtils


  • public abstract class JdbcUtils
    extends Object
    Generic utility methods for working with JDBC. Mainly for internal use within the framework, but also useful for custom JDBC access code.
    作者:
    Thomas Risberg, Juergen Hoeller
    • 字段详细资料

    • 构造器详细资料

    • 方法详细资料

      • closeConnection

        public static void closeConnection​(Connection con)
        Close the given JDBC Connection and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.
        参数:
        con - the JDBC Connection to close (may be null)
      • closeStatement

        public static void closeStatement​(Statement stmt)
        Close the given JDBC Statement and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.
        参数:
        stmt - the JDBC Statement to close (may be null)
      • closeResultSet

        public static void closeResultSet​(ResultSet rs)
        Close the given JDBC ResultSet and ignore any thrown exception. This is useful for typical finally blocks in manual JDBC code.
        参数:
        rs - the JDBC ResultSet to close (may be null)
      • getResultSetValue

        @UsesJava7
        public static Object getResultSetValue​(ResultSet rs,
                                               int index,
                                               Class<?> requiredType)
                                        throws SQLException
        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 (possibly not of the specified required type, with further conversion steps necessary)
        抛出:
        SQLException - if thrown by the JDBC API
        另请参阅:
        getResultSetValue(ResultSet, int)
      • getResultSetValue

        public static Object getResultSetValue​(ResultSet rs,
                                               int index)
                                        throws SQLException
        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 or String representation, respectively.

        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
        抛出:
        SQLException - if thrown by the JDBC API
        另请参阅:
        Blob, Clob, Timestamp
      • extractDatabaseMetaData

        public static Object extractDatabaseMetaData​(DataSource dataSource,
                                                     DatabaseMetaDataCallback action)
                                              throws MetaDataAccessException
        Extract database meta-data via the given DatabaseMetaDataCallback.

        This method will open a connection to the database and retrieve its meta-data. Since this method is called before the exception translation feature is configured for a DataSource, this method can not rely on SQLException translation itself.

        Any exceptions will be wrapped in a MetaDataAccessException. This is a checked exception and any calling code should catch and handle this exception. You can just log the error and hope for the best, but there is probably a more serious error that will reappear when you try to access the database again.

        参数:
        dataSource - the DataSource to extract meta-data for
        action - callback that will do the actual work
        返回:
        object containing the extracted information, as returned by the DatabaseMetaDataCallback's processMetaData method
        抛出:
        MetaDataAccessException - if meta-data access failed
        另请参阅:
        DatabaseMetaData
      • extractDatabaseMetaData

        public static Object extractDatabaseMetaData​(DataSource dataSource,
                                                     String metaDataMethodName)
                                              throws MetaDataAccessException
        Call the specified method on DatabaseMetaData for the given DataSource, and extract the invocation result.
        参数:
        dataSource - the DataSource to extract meta-data for
        metaDataMethodName - the name of the DatabaseMetaData method to call
        返回:
        the object returned by the specified DatabaseMetaData method
        抛出:
        MetaDataAccessException - if we couldn't access the DatabaseMetaData or failed to invoke the specified method
        另请参阅:
        DatabaseMetaData
      • supportsBatchUpdates

        public static boolean supportsBatchUpdates​(Connection con)
        Return whether the given JDBC driver supports JDBC 2.0 batch updates.

        Typically invoked right before execution of a given set of statements: to decide whether the set of SQL statements should be executed through the JDBC 2.0 batch mechanism or simply in a traditional one-by-one fashion.

        Logs a warning if the "supportsBatchUpdates" methods throws an exception and simply returns false in that case.

        参数:
        con - the Connection to check
        返回:
        whether JDBC 2.0 batch updates are supported
        另请参阅:
        DatabaseMetaData.supportsBatchUpdates()
      • commonDatabaseName

        public static String commonDatabaseName​(String source)
        Extract a common name for the target database in use even if various drivers/platforms provide varying names at runtime.
        参数:
        source - the name as provided in database meta-data
        返回:
        the common name to be used (e.g. "DB2" or "Sybase")
      • isNumeric

        public static boolean isNumeric​(int sqlType)
        Check whether the given SQL type is numeric.
        参数:
        sqlType - the SQL type to be checked
        返回:
        whether the type is numeric
      • lookupColumnName

        public static String lookupColumnName​(ResultSetMetaData resultSetMetaData,
                                              int columnIndex)
                                       throws SQLException
        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
        抛出:
        SQLException - in case of lookup failure
      • convertUnderscoreNameToPropertyName

        public static String convertUnderscoreNameToPropertyName​(String name)
        Convert a column name with underscores to the corresponding property name using "camel case". A name like "customer_number" would match a "customerNumber" property name.
        参数:
        name - the column name to be converted
        返回:
        the name using "camel case"