Package gov.usgs.util
Class JDBCUtils
java.lang.Object
gov.usgs.util.JDBCUtils
JDBC Connection and Statement utility functions.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ConnectiongetConnection(String driver, String url) Create a new JDBC Connection.static ConnectiongetMysqlConnection(String url) Get a mysql connection from a URL.static ConnectiongetSqliteConnection(File file) Get a sqlite connection from a file.static ConnectionGet a sqlite connection from a URL.static voidsetParameter(PreparedStatement statement, int index, Object object, int type) Set a JDBC prepared statement parameter.
-
Field Details
-
MYSQL_DRIVER_CLASSNAME
Mysql Driver.- See Also:
-
SQLITE_DRIVER_CLASSNAME
SQLite Driver.- See Also:
-
-
Constructor Details
-
JDBCUtils
public JDBCUtils()
-
-
Method Details
-
getConnection
public static Connection getConnection(String driver, String url) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, SQLException Create a new JDBC Connection.- Parameters:
driver- driver class name.url- driver specific url.- Returns:
- Connection to database.
- Throws:
ClassNotFoundException- if driver class is not found.IllegalAccessException- if driver empty constructor is not public.InstantiationException- if an exception occurs while instantiating driver.InvocationTargetException- if an exception occurs with invoked methodNoSuchMethodException- if method cannot be foundSQLException- if an error occurs while making connection.
-
setParameter
public static void setParameter(PreparedStatement statement, int index, Object object, int type) throws 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:
SQLException- if an error occurs while making connection.
-
getMysqlConnection
public static Connection getMysqlConnection(String url) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, 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:
SQLException- if an error occurs while making connection.ClassNotFoundException- if driver class is not found.IllegalAccessException- if driver empty constructor is not public.InstantiationException- if an exception occurs while instantiating driver.InvocationTargetException- if an exception occurs with invoked methodNoSuchMethodException- if method cannot be found
-
getSqliteConnection
public static Connection getSqliteConnection(File file) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, 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:
SQLException- if an error occurs while making connection.ClassNotFoundException- if driver class is not found.IllegalAccessException- if driver empty constructor is not public.InstantiationException- if an exception occurs while instantiating driver.InvocationTargetException- if an exception occurs with invoked methodNoSuchMethodException- if method cannot be found
-
getSqliteConnection
public static Connection getSqliteConnection(String url) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, 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:
SQLException- if an error occurs while making connection.ClassNotFoundException- if driver class is not found.IllegalAccessException- if driver empty constructor is not public.InstantiationException- if an exception occurs while instantiating driver.InvocationTargetException- if an exception occurs with invoked methodNoSuchMethodException- if method cannot be found
-