public class JDBCManager
extends java.lang.Object
Connection object
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 a DataSource obtained
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).
The connection is established using the
connectToDatabase
method. 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 and Description |
|---|
JDBCManager() |
| Modifier and Type | Method and Description |
|---|---|
static java.sql.Connection |
connectToDatabase(java.io.File file)
Equivalent to
connectToDatabase (new FileInputStream (file)). |
static java.sql.Connection |
connectToDatabase(java.io.InputStream is)
Returns a connection to the database using the properties read from stream is.
|
static java.sql.Connection |
connectToDatabase(java.util.Properties prop)
Connects to the database using the properties prop and returns the
an object representing the connection.
|
static java.sql.Connection |
connectToDatabase(java.lang.String fileName)
Equivalent to
connectToDatabase (new FileInputStream (fileName)). |
static java.sql.Connection |
connectToDatabase(java.net.URL url)
Equivalent to
connectToDatabase (url.openStream()). |
static java.sql.Connection |
connectToDatabaseFromResource(java.lang.String resource)
Uses
connectToDatabase with 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.
|
public static java.sql.Connection connectToDatabase(java.util.Properties prop)
throws java.sql.SQLException
InitialContext,
uses the context to get a DataSource object,
and uses the data source to obtain a connection.
This method assumes that JNDI is configured correctly;
see the class InitialContext for 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 JDBC DriverManager.
The driver manager is then used to obtain the connection using the URI.
This method throws
an SQLException if the connection failed and an
IllegalArgumentException
if the properties do not contain the required values.prop - the properties to connect to the database.java.sql.SQLException - if the connection failed.java.lang.IllegalArgumentException - if the properties do not contain the require values.public static java.sql.Connection connectToDatabase(java.io.InputStream is)
throws java.io.IOException,
java.sql.SQLException
connectToDatabase to establish the connection.is - the stream to read for the properties.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.public static java.sql.Connection connectToDatabase(java.net.URL url)
throws java.io.IOException,
java.sql.SQLException
connectToDatabase (url.openStream()).java.io.IOExceptionjava.sql.SQLExceptionpublic static java.sql.Connection connectToDatabase(java.io.File file)
throws java.io.IOException,
java.sql.SQLException
connectToDatabase (new FileInputStream (file)).java.io.IOExceptionjava.sql.SQLExceptionpublic static java.sql.Connection connectToDatabase(java.lang.String fileName)
throws java.io.IOException,
java.sql.SQLException
connectToDatabase (new FileInputStream (fileName)).java.io.IOExceptionjava.sql.SQLExceptionpublic static java.sql.Connection connectToDatabaseFromResource(java.lang.String resource)
throws java.io.IOException,
java.sql.SQLException
connectToDatabase with 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 uses connectToDatabase
to establish the connection.java.io.IOExceptionjava.sql.SQLExceptionpublic static double[] readDoubleData(java.sql.Statement stmt,
java.lang.String query)
throws java.sql.SQLException
SQLException if the query is not valid.stmt - the statement used to make the query.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static double[] readDoubleData(java.sql.Connection connection,
java.lang.String query)
throws java.sql.SQLException
readDoubleData, which returns an
array of double-precision values.connection - the active connection to the database.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static double[] readDoubleData(java.sql.Statement stmt,
java.lang.String table,
java.lang.String column)
throws java.sql.SQLException
readDoubleData
(stmt, "SELECT column FROM table").java.sql.SQLExceptionpublic static double[] readDoubleData(java.sql.Connection connection,
java.lang.String table,
java.lang.String column)
throws java.sql.SQLException
readDoubleData
(connection, "SELECT column FROM table").java.sql.SQLExceptionpublic static int[] readIntData(java.sql.Statement stmt,
java.lang.String query)
throws java.sql.SQLException
SQLException if the query is not valid.
The given statement stmt must not be set up to
produce forward-only result sets.stmt - the statement used to make the query.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static int[] readIntData(java.sql.Connection connection,
java.lang.String query)
throws java.sql.SQLException
readIntData, which returns an
array of integers.connection - the active connection to the database.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static int[] readIntData(java.sql.Statement stmt,
java.lang.String table,
java.lang.String column)
throws java.sql.SQLException
readIntData
(stmt, "SELECT column FROM table").java.sql.SQLExceptionpublic static int[] readIntData(java.sql.Connection connection,
java.lang.String table,
java.lang.String column)
throws java.sql.SQLException
readIntData
(connection, "SELECT column FROM table").java.sql.SQLExceptionpublic static java.lang.Object[] readObjectData(java.sql.Statement stmt,
java.lang.String query)
throws java.sql.SQLException
SQLException if the query is not valid.
The given statement stmt must not be set up to
produce forward-only result sets.stmt - the statement used to make the query.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static java.lang.Object[] readObjectData(java.sql.Connection connection,
java.lang.String query)
throws java.sql.SQLException
readObjectData, which returns an
array of integers.connection - the active connection to the database.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static java.lang.Object[] readObjectData(java.sql.Statement stmt,
java.lang.String table,
java.lang.String column)
throws java.sql.SQLException
readObjectData
(stmt, "SELECT column FROM table").java.sql.SQLExceptionpublic static java.lang.Object[] readObjectData(java.sql.Connection connection,
java.lang.String table,
java.lang.String column)
throws java.sql.SQLException
readObjectData
(connection, "SELECT column FROM table").java.sql.SQLExceptionpublic static double[][] readDoubleData2D(java.sql.Statement stmt,
java.lang.String query)
throws java.sql.SQLException
SQLException if the query is not valid.
The given statement stmt must not be set up to
produce forward-only result sets.stmt - the statement used to make the query.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static double[][] readDoubleData2D(java.sql.Connection connection,
java.lang.String query)
throws java.sql.SQLException
readDoubleData2D, which returns a 2D
array of double-precision values.connection - the active connection to the database.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static double[][] readDoubleData2DTable(java.sql.Statement stmt,
java.lang.String table)
throws java.sql.SQLException
readDoubleData2D
(stmt, "SELECT * FROM table").java.sql.SQLExceptionpublic static double[][] readDoubleData2DTable(java.sql.Connection connection,
java.lang.String table)
throws java.sql.SQLException
readDoubleData2D
(connection, "SELECT * FROM table").java.sql.SQLExceptionpublic static int[][] readIntData2D(java.sql.Statement stmt,
java.lang.String query)
throws java.sql.SQLException
SQLException if the query is not valid.
The given statement stmt must not be set up to
produce forward-only result sets.stmt - the statement used to make the query.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static int[][] readIntData2D(java.sql.Connection connection,
java.lang.String query)
throws java.sql.SQLException
readIntData2D, which returns a 2D
array of integers.connection - the active connection to the database.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static int[][] readIntData2DTable(java.sql.Statement stmt,
java.lang.String table)
throws java.sql.SQLException
readIntData2D
(stmt, "SELECT * FROM table").java.sql.SQLExceptionpublic static int[][] readIntData2DTable(java.sql.Connection connection,
java.lang.String table)
throws java.sql.SQLException
readIntData2D
(connection, "SELECT * FROM table").java.sql.SQLExceptionpublic static java.lang.Object[][] readObjectData2D(java.sql.Statement stmt,
java.lang.String query)
throws java.sql.SQLException
SQLException if the query is not valid.
The given statement stmt must not be set up to
produce forward-only result sets.stmt - the statement used to make the query.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static java.lang.Object[][] readObjectData2D(java.sql.Connection connection,
java.lang.String query)
throws java.sql.SQLException
readObjectData2D, which returns a 2D
array of integers.connection - the active connection to the database.query - the SQL query to execute.java.sql.SQLException - if the query is not valid.public static java.lang.Object[][] readObjectData2DTable(java.sql.Statement stmt,
java.lang.String table)
throws java.sql.SQLException
readObjectData2D
(stmt, "SELECT * FROM table").java.sql.SQLExceptionpublic static java.lang.Object[][] readObjectData2DTable(java.sql.Connection connection,
java.lang.String table)
throws java.sql.SQLException
readObjectData2D
(connection, "SELECT * FROM table").java.sql.SQLExceptionTo submit a bug or ask questions, send an e-mail to Pierre L'Ecuyer.