Package gov.usgs.util
Class JDBCUtils
- java.lang.Object
-
- gov.usgs.util.JDBCUtils
-
public class JDBCUtils extends java.lang.ObjectJDBC Connection and Statement utility functions.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringMYSQL_DRIVER_CLASSNAMEMysql Driver.static java.lang.StringSQLITE_DRIVER_CLASSNAMESQLite Driver.
-
Constructor Summary
Constructors Constructor Description JDBCUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.sql.ConnectiongetConnection(java.lang.String driver, java.lang.String url)Create a new JDBC Connection.static java.sql.ConnectiongetMysqlConnection(java.lang.String url)Get a mysql connection from a URL.static java.sql.ConnectiongetSqliteConnection(java.io.File file)Get a sqlite connection from a file.static java.sql.ConnectiongetSqliteConnection(java.lang.String url)Get a sqlite connection from a URL.static voidsetParameter(java.sql.PreparedStatement statement, int index, java.lang.Object object, int type)Set a JDBC prepared statement parameter.
-
-
-
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
-
-
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.SQLExceptionCreate 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 methodjava.lang.NoSuchMethodException- if method cannot be foundjava.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.SQLExceptionSet 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.NoSuchMethodExceptionGet 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 methodjava.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.NoSuchMethodExceptionGet 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 methodjava.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.NoSuchMethodExceptionGet 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 methodjava.lang.NoSuchMethodException- if method cannot be found
-
-