umontreal.iro.lecuyer.util
Class JDBCManager
- java.lang.Object
-
- umontreal.iro.lecuyer.util.JDBCManager
-
public class JDBCManager extends java.lang.ObjectThis class provides some facilities to connect to a SQL database and to retrieve data stored in it. JDBC provides a standardized interface for accessing a database independently of a specific database management system (DBMS). The user of JDBC must create aConnectionobject used to send SQL queries to the underlying DBMS, but the creation of the connection adds a DBMS-specific portion in the application. This class helps the developer in moving the DBMS-specific information out of the source code by storing it in a properties file. The methods in this class can read such a properties file and establish the JDBC connection. The connection can be made by using aDataSourceobtained through a JNDI server, or by a JDBC URI associated with a driver class. Therefore, the properties used to connect to the database must be a JNDI name (jdbc.jndi-name), or a driver to load (jdbc.driver) with the URI of a database (jdbc.uri).
jdbc.driver=com.mysql.jdbc.Driver
jdbc.uri=jdbc:mysql://mysql.iro.umontreal.ca/database?user=foo&password=bar
The connection is established using the
connectToDatabasemethod. Shortcut methods are also available to read the properties from a file or a resource before establishing the connection. This class also provides shortcut methods to read data from a database and to copy the data into Java arrays.
-
-
Constructor Summary
Constructors Constructor and Description JDBCManager()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static java.sql.ConnectionconnectToDatabase(java.io.File file)Equivalent toconnectToDatabase(new FileInputStream (file)).static java.sql.ConnectionconnectToDatabase(java.io.InputStream is)Returns a connection to the database using the properties read from stream is.static java.sql.ConnectionconnectToDatabase(java.util.Properties prop)Connects to the database using the properties prop and returns the an object representing the connection.static java.sql.ConnectionconnectToDatabase(java.lang.String fileName)Equivalent toconnectToDatabase(new FileInputStream (fileName)).static java.sql.ConnectionconnectToDatabase(java.net.URL url)Equivalent toconnectToDatabase(url.openStream()).static java.sql.ConnectionconnectToDatabaseFromResource(java.lang.String resource)UsesconnectToDatabasewith the stream obtained from the resource resource.static double[]readDoubleData(java.sql.Connection connection, java.lang.String query)Copies the result of the SQL query query into an array of double-precision values.static double[]readDoubleData(java.sql.Connection connection, java.lang.String table, java.lang.String column)Returns the values of the column column of the table table.static double[]readDoubleData(java.sql.Statement stmt, java.lang.String query)Copies the result of the SQL query query into an array of double-precision values.static double[]readDoubleData(java.sql.Statement stmt, java.lang.String table, java.lang.String column)Returns the values of the column column of the table table.static double[][]readDoubleData2D(java.sql.Connection connection, java.lang.String query)Copies the result of the SQL query query into a rectangular 2D array of double-precision values.static double[][]readDoubleData2D(java.sql.Statement stmt, java.lang.String query)Copies the result of the SQL query query into a rectangular 2D array of double-precision values.static double[][]readDoubleData2DTable(java.sql.Connection connection, java.lang.String table)Returns the values of the columns of the table table.static double[][]readDoubleData2DTable(java.sql.Statement stmt, java.lang.String table)Returns the values of the columns of the table table.static int[]readIntData(java.sql.Connection connection, java.lang.String query)Copies the result of the SQL query query into an array of integers.static int[]readIntData(java.sql.Connection connection, java.lang.String table, java.lang.String column)Returns the values of the column column of the table table.static int[]readIntData(java.sql.Statement stmt, java.lang.String query)Copies the result of the SQL query query into an array of integers.static int[]readIntData(java.sql.Statement stmt, java.lang.String table, java.lang.String column)Returns the values of the column column of the table table.static int[][]readIntData2D(java.sql.Connection connection, java.lang.String query)Copies the result of the SQL query query into a rectangular 2D array of integers.static int[][]readIntData2D(java.sql.Statement stmt, java.lang.String query)Copies the result of the SQL query query into a rectangular 2D array of integers.static int[][]readIntData2DTable(java.sql.Connection connection, java.lang.String table)Returns the values of the columns of the table table.static int[][]readIntData2DTable(java.sql.Statement stmt, java.lang.String table)Returns the values of the columns of the table table.static java.lang.Object[]readObjectData(java.sql.Connection connection, java.lang.String query)Copies the result of the SQL query query into an array of objects.static java.lang.Object[]readObjectData(java.sql.Connection connection, java.lang.String table, java.lang.String column)Returns the values of the column column of the table table.static java.lang.Object[]readObjectData(java.sql.Statement stmt, java.lang.String query)Copies the result of the SQL query query into an array of objects.static java.lang.Object[]readObjectData(java.sql.Statement stmt, java.lang.String table, java.lang.String column)Returns the values of the column column of the table table.static java.lang.Object[][]readObjectData2D(java.sql.Connection connection, java.lang.String query)Copies the result of the SQL query query into a rectangular 2D array of integers.static java.lang.Object[][]readObjectData2D(java.sql.Statement stmt, java.lang.String query)Copies the result of the SQL query query into a rectangular 2D array of objects.static java.lang.Object[][]readObjectData2DTable(java.sql.Connection connection, java.lang.String table)Returns the values of the columns of the table table.static java.lang.Object[][]readObjectData2DTable(java.sql.Statement stmt, java.lang.String table)Returns the values of the columns of the table table.
-
-
-
Method Detail
-
connectToDatabase
public static java.sql.Connection connectToDatabase(java.util.Properties prop) throws java.sql.SQLExceptionConnects to the database using the properties prop and returns the an object representing the connection. The properties stored in prop must be a JNDI name (jdbc.jndi-name), or the name of a driver (jdbc.driver) to load and the URI of the database (jdbc.uri). When a JNDI name is given, this method constructs a context using the nullary constructor ofInitialContext, uses the context to get aDataSourceobject, and uses the data source to obtain a connection. This method assumes that JNDI is configured correctly; see the classInitialContextfor more information about configuring JNDI. If no JNDI name is specified, the method looks for a JDBC URI. If a driver class name is specified along with the URI, the corresponding driver is loaded and registered with the JDBCDriverManager. The driver manager is then used to obtain the connection using the URI. This method throws anSQLExceptionif the connection failed and anIllegalArgumentExceptionif the properties do not contain the required values.- Parameters:
prop- the properties to connect to the database.- Returns:
- the connection to the database.
- Throws:
java.sql.SQLException- if the connection failed.java.lang.IllegalArgumentException- if the properties do not contain the require values.
-
connectToDatabase
public static java.sql.Connection connectToDatabase(java.io.InputStream is) throws java.io.IOException, java.sql.SQLExceptionReturns a connection to the database using the properties read from stream is. This method loads the properties from the given stream, and callsconnectToDatabaseto establish the connection.- Parameters:
is- the stream to read for the properties.- Returns:
- the connection to the database.
- Throws:
java.sql.SQLException- if the connection failed.java.io.IOException- if the stream can not be read correctly.java.lang.IllegalArgumentException- if the properties do not contain the require values.
-
connectToDatabase
public static java.sql.Connection connectToDatabase(java.net.URL url) throws java.io.IOException, java.sql.SQLExceptionEquivalent toconnectToDatabase(url.openStream()).- Throws:
java.io.IOExceptionjava.sql.SQLException
-
connectToDatabase
public static java.sql.Connection connectToDatabase(java.io.File file) throws java.io.IOException, java.sql.SQLExceptionEquivalent toconnectToDatabase(new FileInputStream (file)).- Throws:
java.io.IOExceptionjava.sql.SQLException
-
connectToDatabase
public static java.sql.Connection connectToDatabase(java.lang.String fileName) throws java.io.IOException, java.sql.SQLExceptionEquivalent toconnectToDatabase(new FileInputStream (fileName)).- Throws:
java.io.IOExceptionjava.sql.SQLException
-
connectToDatabaseFromResource
public static java.sql.Connection connectToDatabaseFromResource(java.lang.String resource) throws java.io.IOException, java.sql.SQLExceptionUsesconnectToDatabasewith the stream obtained from the resource resource. This method searches the file resource on the class path, opens the first resource found, and extracts properties from it. It then usesconnectToDatabaseto establish the connection.- Throws:
java.io.IOExceptionjava.sql.SQLException
-
readDoubleData
public static double[] readDoubleData(java.sql.Statement stmt, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into an array of double-precision values. This method uses the statement stmt to execute the given query, and assumes that the first column of the result set contains double-precision values. Each row of the result set then becomes an element of an array of double-precision values which is returned by this method. This method throws anSQLExceptionif the query is not valid.- Parameters:
stmt- the statement used to make the query.query- the SQL query to execute.- Returns:
- the first column of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readDoubleData
public static double[] readDoubleData(java.sql.Connection connection, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into an array of double-precision values. This method uses the active connection connection to create a statement, and passes this statement, with the query, toreadDoubleData, which returns an array of double-precision values.- Parameters:
connection- the active connection to the database.query- the SQL query to execute.- Returns:
- the first column of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readDoubleData
public static double[] readDoubleData(java.sql.Statement stmt, java.lang.String table, java.lang.String column) throws java.sql.SQLExceptionReturns the values of the column column of the table table. This method is equivalent toreadDoubleData(stmt, "SELECT column FROM table").- Throws:
java.sql.SQLException
-
readDoubleData
public static double[] readDoubleData(java.sql.Connection connection, java.lang.String table, java.lang.String column) throws java.sql.SQLExceptionReturns the values of the column column of the table table. This method is equivalent toreadDoubleData(connection, "SELECT column FROM table").- Throws:
java.sql.SQLException
-
readIntData
public static int[] readIntData(java.sql.Statement stmt, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into an array of integers. This method uses the statement stmt to execute the given query, and assumes that the first column of the result set contains integer values. Each row of the result set then becomes an element of an array of integers which is returned by this method. This method throws anSQLExceptionif the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.- Parameters:
stmt- the statement used to make the query.query- the SQL query to execute.- Returns:
- the first column of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readIntData
public static int[] readIntData(java.sql.Connection connection, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into an array of integers. This method uses the active connection connection to create a statement, and passes this statement, with the query, toreadIntData, which returns an array of integers.- Parameters:
connection- the active connection to the database.query- the SQL query to execute.- Returns:
- the first column of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readIntData
public static int[] readIntData(java.sql.Statement stmt, java.lang.String table, java.lang.String column) throws java.sql.SQLExceptionReturns the values of the column column of the table table. This method is equivalent toreadIntData(stmt, "SELECT column FROM table").- Throws:
java.sql.SQLException
-
readIntData
public static int[] readIntData(java.sql.Connection connection, java.lang.String table, java.lang.String column) throws java.sql.SQLExceptionReturns the values of the column column of the table table. This method is equivalent toreadIntData(connection, "SELECT column FROM table").- Throws:
java.sql.SQLException
-
readObjectData
public static java.lang.Object[] readObjectData(java.sql.Statement stmt, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into an array of objects. This method uses the statement stmt to execute the given query, and extracts values from the first column of the obtained result set by using the getObject method. Each row of the result set then becomes an element of an array of objects which is returned by this method. The type of the objects in the array depends on the column type of the result set, which depends on the database and query. This method throws anSQLExceptionif the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.- Parameters:
stmt- the statement used to make the query.query- the SQL query to execute.- Returns:
- the first column of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readObjectData
public static java.lang.Object[] readObjectData(java.sql.Connection connection, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into an array of objects. This method uses the active connection connection to create a statement, and passes this statement, with the query, toreadObjectData, which returns an array of integers.- Parameters:
connection- the active connection to the database.query- the SQL query to execute.- Returns:
- the first column of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readObjectData
public static java.lang.Object[] readObjectData(java.sql.Statement stmt, java.lang.String table, java.lang.String column) throws java.sql.SQLExceptionReturns the values of the column column of the table table. This method is equivalent toreadObjectData(stmt, "SELECT column FROM table").- Throws:
java.sql.SQLException
-
readObjectData
public static java.lang.Object[] readObjectData(java.sql.Connection connection, java.lang.String table, java.lang.String column) throws java.sql.SQLExceptionReturns the values of the column column of the table table. This method is equivalent toreadObjectData(connection, "SELECT column FROM table").- Throws:
java.sql.SQLException
-
readDoubleData2D
public static double[][] readDoubleData2D(java.sql.Statement stmt, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into a rectangular 2D array of double-precision values. This method uses the statement stmt to execute the given query, and assumes that the columns of the result set contain double-precision values. Each row of the result set then becomes a row of a 2D array of double-precision values which is returned by this method. This method throws anSQLExceptionif the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.- Parameters:
stmt- the statement used to make the query.query- the SQL query to execute.- Returns:
- the columns of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readDoubleData2D
public static double[][] readDoubleData2D(java.sql.Connection connection, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into a rectangular 2D array of double-precision values. This method uses the active connection connection to create a statement, and passes this statement, with the query, toreadDoubleData2D, which returns a 2D array of double-precision values.- Parameters:
connection- the active connection to the database.query- the SQL query to execute.- Returns:
- the columns of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readDoubleData2DTable
public static double[][] readDoubleData2DTable(java.sql.Statement stmt, java.lang.String table) throws java.sql.SQLExceptionReturns the values of the columns of the table table. This method is equivalent toreadDoubleData2D(stmt, "SELECT * FROM table").- Throws:
java.sql.SQLException
-
readDoubleData2DTable
public static double[][] readDoubleData2DTable(java.sql.Connection connection, java.lang.String table) throws java.sql.SQLExceptionReturns the values of the columns of the table table. This method is equivalent toreadDoubleData2D(connection, "SELECT * FROM table").- Throws:
java.sql.SQLException
-
readIntData2D
public static int[][] readIntData2D(java.sql.Statement stmt, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into a rectangular 2D array of integers. This method uses the statement stmt to execute the given query, and assumes that the columns of the result set contain integers. Each row of the result set then becomes a row of a 2D array of integers which is returned by this method. This method throws anSQLExceptionif the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.- Parameters:
stmt- the statement used to make the query.query- the SQL query to execute.- Returns:
- the columns of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readIntData2D
public static int[][] readIntData2D(java.sql.Connection connection, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into a rectangular 2D array of integers. This method uses the active connection connection to create a statement, and passes this statement, with the query, toreadIntData2D, which returns a 2D array of integers.- Parameters:
connection- the active connection to the database.query- the SQL query to execute.- Returns:
- the columns of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readIntData2DTable
public static int[][] readIntData2DTable(java.sql.Statement stmt, java.lang.String table) throws java.sql.SQLExceptionReturns the values of the columns of the table table. This method is equivalent toreadIntData2D(stmt, "SELECT * FROM table").- Throws:
java.sql.SQLException
-
readIntData2DTable
public static int[][] readIntData2DTable(java.sql.Connection connection, java.lang.String table) throws java.sql.SQLExceptionReturns the values of the columns of the table table. This method is equivalent toreadIntData2D(connection, "SELECT * FROM table").- Throws:
java.sql.SQLException
-
readObjectData2D
public static java.lang.Object[][] readObjectData2D(java.sql.Statement stmt, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into a rectangular 2D array of objects. This method uses the statement stmt to execute the given query, and extracts values from the obtained result set by using the getObject method. Each row of the result set then becomes a row of a 2D array of objects which is returned by this method. The type of the objects in the 2D array depends on the column types of the result set, which depend on the database and query. This method throws anSQLExceptionif the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.- Parameters:
stmt- the statement used to make the query.query- the SQL query to execute.- Returns:
- the columns of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readObjectData2D
public static java.lang.Object[][] readObjectData2D(java.sql.Connection connection, java.lang.String query) throws java.sql.SQLExceptionCopies the result of the SQL query query into a rectangular 2D array of integers. This method uses the active connection connection to create a statement, and passes this statement, with the query, toreadObjectData2D, which returns a 2D array of integers.- Parameters:
connection- the active connection to the database.query- the SQL query to execute.- Returns:
- the columns of the result set.
- Throws:
java.sql.SQLException- if the query is not valid.
-
readObjectData2DTable
public static java.lang.Object[][] readObjectData2DTable(java.sql.Statement stmt, java.lang.String table) throws java.sql.SQLExceptionReturns the values of the columns of the table table. This method is equivalent toreadObjectData2D(stmt, "SELECT * FROM table").- Throws:
java.sql.SQLException
-
readObjectData2DTable
public static java.lang.Object[][] readObjectData2DTable(java.sql.Connection connection, java.lang.String table) throws java.sql.SQLExceptionReturns the values of the columns of the table table. This method is equivalent toreadObjectData2D(connection, "SELECT * FROM table").- Throws:
java.sql.SQLException
-
-
DMelt 3.0 © DataMelt by jWork.ORG