Class JDBCUtils


  • public class JDBCUtils
    extends java.lang.Object
    JDBC-related utility methods.
    Author:
    Lukas Eder
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static SQLDialect dialect​(java.lang.String url)
      "Guess" the SQLDialect from a connection URL.
      static SQLDialect dialect​(java.sql.Connection connection)
      "Guess" the SQLDialect from a Connection instance.
      static java.lang.String driver​(java.lang.String url)
      "Guess" the JDBC driver from a connection URL.
      static void safeClose​(java.io.Closeable closeable)
      Safely close a closeable.
      static void safeClose​(java.lang.AutoCloseable closeable)
      Safely close a closeable.
      static void safeClose​(java.sql.Connection connection)
      Safely close a connection.
      static void safeClose​(java.sql.ResultSet resultSet)
      Safely close a result set.
      static void safeClose​(java.sql.ResultSet resultSet, java.sql.PreparedStatement statement)
      Safely close a result set and / or a statement.
      static void safeClose​(java.sql.Statement statement)
      Safely close a statement.
      static void safeFree​(java.sql.Array array)
      Safely free an Array object.
      static void safeFree​(java.sql.Blob blob)
      Safely free a blob.
      static void safeFree​(java.sql.Clob clob)
      Safely free a clob.
      static void safeFree​(java.sql.SQLXML xml)
      Safely free an XML object.
      static java.lang.Boolean wasNull​(java.sql.CallableStatement statement, java.lang.Boolean value)
      Convenient way to check if a JDBC-originated record was null.
      static <T> T wasNull​(java.sql.CallableStatement statement, T value)
      Convenient way to check if a JDBC-originated record was null.
      static java.lang.Boolean wasNull​(java.sql.ResultSet rs, java.lang.Boolean value)
      Convenient way to check if a JDBC-originated record was null.
      static <T> T wasNull​(java.sql.ResultSet rs, T value)
      Convenient way to check if a JDBC-originated record was null.
      static java.lang.Boolean wasNull​(java.sql.SQLInput stream, java.lang.Boolean value)
      Convenient way to check if a JDBC-originated record was null.
      static <T> T wasNull​(java.sql.SQLInput stream, T value)
      Convenient way to check if a JDBC-originated record was null.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • dialect

        public static final SQLDialect dialect​(java.sql.Connection connection)
        "Guess" the SQLDialect from a Connection instance.

        This method tries to guess the SQLDialect of a connection from the its connection URL as obtained by DatabaseMetaData.getURL(). If the dialect cannot be guessed from the URL (e.g. when using an JDBC-ODBC bridge), further actions may be implemented in the future.

        Returns:
        The appropriate SQLDialect or SQLDialect.DEFAULT if no dialect could be derived from the connection. Never null.
        See Also:
        dialect(String)
      • dialect

        public static final SQLDialect dialect​(java.lang.String url)
        "Guess" the SQLDialect from a connection URL.
        Returns:
        The appropriate SQLDialect or SQLDialect.DEFAULT if no dialect could be derived from the connection. Never null.
      • driver

        public static final java.lang.String driver​(java.lang.String url)
        "Guess" the JDBC driver from a connection URL.
        Returns:
        The appropriate JDBC driver class or "java.sql.Driver" if no driver class could be derived from the URL. Never null.
      • safeClose

        public static final void safeClose​(java.sql.Connection connection)
        Safely close a connection.

        This method will silently ignore if connection is null, or if Connection.close() throws an exception.

      • safeClose

        public static final void safeClose​(java.sql.Statement statement)
        Safely close a statement.

        This method will silently ignore if statement is null, or if Statement.close() throws an exception.

      • safeClose

        public static final void safeClose​(java.sql.ResultSet resultSet)
        Safely close a result set.

        This method will silently ignore if resultSet is null, or if ResultSet.close() throws an exception.

      • safeClose

        public static final void safeClose​(java.sql.ResultSet resultSet,
                                           java.sql.PreparedStatement statement)
        Safely close a result set and / or a statement.

        This method will silently ignore if resultSet or statement is null, or if ResultSet.close() or Statement.close() throws an exception.

      • safeClose

        public static final void safeClose​(java.io.Closeable closeable)
        Safely close a closeable.

        This method will silently ignore if closeable is null, or if Closeable.close() throws an exception.

      • safeClose

        public static final void safeClose​(java.lang.AutoCloseable closeable)
        Safely close a closeable.

        This method will silently ignore if closeable is null, or if AutoCloseable.close() throws an exception.

      • safeFree

        public static final void safeFree​(java.sql.Blob blob)
        Safely free a blob.

        This method will silently ignore if blob is null, or if Blob.free() throws an exception.

      • safeFree

        public static final void safeFree​(java.sql.Clob clob)
        Safely free a clob.

        This method will silently ignore if clob is null, or if Clob.free() throws an exception.

      • safeFree

        public static final void safeFree​(java.sql.SQLXML xml)
        Safely free an XML object.

        This method will silently ignore if xml is null, or if SQLXML.free() throws an exception.

      • safeFree

        public static final void safeFree​(java.sql.Array array)
        Safely free an Array object.

        This method will silently ignore if array is null, or if Array.free() throws an exception.

      • wasNull

        public static final <T> T wasNull​(java.sql.SQLInput stream,
                                          T value)
                                   throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        stream - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the SQLInput.wasNull() is true
        Throws:
        java.sql.SQLException
      • wasNull

        public static final <T extends java.lang.Number> T wasNull​(java.sql.SQLInput stream,
                                                                   T value)
                                                            throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        stream - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the SQLInput.wasNull() is true
        Throws:
        java.sql.SQLException
      • wasNull

        public static final java.lang.Boolean wasNull​(java.sql.SQLInput stream,
                                                      java.lang.Boolean value)
                                               throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        stream - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the SQLInput.wasNull() is true
        Throws:
        java.sql.SQLException
      • wasNull

        public static final <T> T wasNull​(java.sql.ResultSet rs,
                                          T value)
                                   throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        rs - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the ResultSet.wasNull() is true
        Throws:
        java.sql.SQLException
      • wasNull

        public static final <T extends java.lang.Number> T wasNull​(java.sql.ResultSet rs,
                                                                   T value)
                                                            throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        rs - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the ResultSet.wasNull() is true
        Throws:
        java.sql.SQLException
      • wasNull

        public static final java.lang.Boolean wasNull​(java.sql.ResultSet rs,
                                                      java.lang.Boolean value)
                                               throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        rs - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the ResultSet.wasNull() is true
        Throws:
        java.sql.SQLException
      • wasNull

        public static final <T> T wasNull​(java.sql.CallableStatement statement,
                                          T value)
                                   throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        statement - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the CallableStatement.wasNull() is true
        Throws:
        java.sql.SQLException
      • wasNull

        public static final <T extends java.lang.Number> T wasNull​(java.sql.CallableStatement statement,
                                                                   T value)
                                                            throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        statement - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the CallableStatement.wasNull() is true
        Throws:
        java.sql.SQLException
      • wasNull

        public static final java.lang.Boolean wasNull​(java.sql.CallableStatement statement,
                                                      java.lang.Boolean value)
                                               throws java.sql.SQLException
        Convenient way to check if a JDBC-originated record was null.

        This is useful to check if primitive types obtained from the JDBC API were actually SQL NULL values.

        Parameters:
        statement - The data source from which a value was read
        value - The value that was read
        Returns:
        The value or null if the CallableStatement.wasNull() is true
        Throws:
        java.sql.SQLException