Package gov.usgs.util

Class JDBCUtils


  • public class JDBCUtils
    extends java.lang.Object
    JDBC Connection and Statement utility functions.
    • Constructor Summary

      Constructors 
      Constructor Description
      JDBCUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.sql.Connection getConnection​(java.lang.String driver, java.lang.String url)
      Create a new JDBC Connection.
      static java.sql.Connection getMysqlConnection​(java.lang.String url)
      Get a mysql connection from a URL.
      static java.sql.Connection getSqliteConnection​(java.io.File file)
      Get a sqlite connection from a file.
      static java.sql.Connection getSqliteConnection​(java.lang.String url)
      Get a sqlite connection from a URL.
      static void setParameter​(java.sql.PreparedStatement statement, int index, java.lang.Object object, int type)
      Set a JDBC prepared statement parameter.
      • Methods inherited from class java.lang.Object

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

      • MYSQL_DRIVER_CLASSNAME

        public static final java.lang.String MYSQL_DRIVER_CLASSNAME
        Mysql Driver.
        See Also:
        Constant Field Values
      • SQLITE_DRIVER_CLASSNAME

        public static final java.lang.String SQLITE_DRIVER_CLASSNAME
        SQLite Driver.
        See Also:
        Constant Field Values
    • Constructor Detail

      • JDBCUtils

        public JDBCUtils()
    • Method Detail

      • getConnection

        public static java.sql.Connection getConnection​(java.lang.String driver,
                                                        java.lang.String url)
                                                 throws java.lang.ClassNotFoundException,
                                                        java.lang.IllegalAccessException,
                                                        java.lang.InstantiationException,
                                                        java.lang.reflect.InvocationTargetException,
                                                        java.lang.NoSuchMethodException,
                                                        java.sql.SQLException
        Create a new JDBC Connection.
        Parameters:
        driver - driver class name.
        url - driver specific url.
        Returns:
        Connection to database.
        Throws:
        java.lang.ClassNotFoundException - if driver class is not found.
        java.lang.IllegalAccessException - if driver empty constructor is not public.
        java.lang.InstantiationException - if an exception occurs while instantiating driver.
        java.lang.reflect.InvocationTargetException - if an exception occurs with invoked method
        java.lang.NoSuchMethodException - if method cannot be found
        java.sql.SQLException - if an error occurs while making connection.
      • setParameter

        public static void setParameter​(java.sql.PreparedStatement statement,
                                        int index,
                                        java.lang.Object object,
                                        int type)
                                 throws java.sql.SQLException
        Set a JDBC prepared statement parameter. Either calls statement.setNull if object is null, or sets the appropriate type based on the object. If the object is not null, type is ignored.
        Parameters:
        statement - statement with parameters to set.
        index - index of parameter being set.
        object - value of parameter being set.
        type - java.sql.Types constant for column type.
        Throws:
        java.sql.SQLException - if an error occurs while making connection.
      • getMysqlConnection

        public static java.sql.Connection getMysqlConnection​(java.lang.String url)
                                                      throws java.sql.SQLException,
                                                             java.lang.ClassNotFoundException,
                                                             java.lang.IllegalAccessException,
                                                             java.lang.InstantiationException,
                                                             java.lang.reflect.InvocationTargetException,
                                                             java.lang.NoSuchMethodException
        Get a mysql connection from a URL. Calls getConnection(MYSQL_DRIVER_CLASSNAME, url).
        Parameters:
        url - a Mysql URL.
        Returns:
        a Connection to a Mysql database.
        Throws:
        java.sql.SQLException - if an error occurs while making connection.
        java.lang.ClassNotFoundException - if driver class is not found.
        java.lang.IllegalAccessException - if driver empty constructor is not public.
        java.lang.InstantiationException - if an exception occurs while instantiating driver.
        java.lang.reflect.InvocationTargetException - if an exception occurs with invoked method
        java.lang.NoSuchMethodException - if method cannot be found
      • getSqliteConnection

        public static java.sql.Connection getSqliteConnection​(java.io.File file)
                                                       throws java.sql.SQLException,
                                                              java.lang.ClassNotFoundException,
                                                              java.lang.IllegalAccessException,
                                                              java.lang.InstantiationException,
                                                              java.lang.reflect.InvocationTargetException,
                                                              java.lang.NoSuchMethodException
        Get a sqlite connection from a file. Builds a sqlite file url and calls getSqliteConnection(url).
        Parameters:
        file - sqlite database file.
        Returns:
        connection to sqlite database file.
        Throws:
        java.sql.SQLException - if an error occurs while making connection.
        java.lang.ClassNotFoundException - if driver class is not found.
        java.lang.IllegalAccessException - if driver empty constructor is not public.
        java.lang.InstantiationException - if an exception occurs while instantiating driver.
        java.lang.reflect.InvocationTargetException - if an exception occurs with invoked method
        java.lang.NoSuchMethodException - if method cannot be found
      • getSqliteConnection

        public static java.sql.Connection getSqliteConnection​(java.lang.String url)
                                                       throws java.sql.SQLException,
                                                              java.lang.ClassNotFoundException,
                                                              java.lang.IllegalAccessException,
                                                              java.lang.InstantiationException,
                                                              java.lang.reflect.InvocationTargetException,
                                                              java.lang.NoSuchMethodException
        Get a sqlite connection from a URL. Calls getConnection(SQLITE_DRIVER_CLASSNAME, url).
        Parameters:
        url - sqlite database URL.
        Returns:
        a Connection to a sqlite database.
        Throws:
        java.sql.SQLException - if an error occurs while making connection.
        java.lang.ClassNotFoundException - if driver class is not found.
        java.lang.IllegalAccessException - if driver empty constructor is not public.
        java.lang.InstantiationException - if an exception occurs while instantiating driver.
        java.lang.reflect.InvocationTargetException - if an exception occurs with invoked method
        java.lang.NoSuchMethodException - if method cannot be found