SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
umontreal.ssj.util.JDBCManager Class Reference

This class provides some facilities to connect to a SQL database and to retrieve data stored in it. More...

Static Public Member Functions

static Connection connectToDatabase (Properties prop) throws SQLException
 Connects to the database using the properties prop and returns the an object representing the connection.
static Connection connectToDatabase (InputStream is) throws IOException, SQLException
 Returns a connection to the database using the properties read from stream is.
static Connection connectToDatabase (URL url) throws IOException, SQLException
 Equivalent to connectToDatabase(url.openStream()).
static Connection connectToDatabase (File file) throws IOException, SQLException
 Equivalent to connectToDatabase(new FileInputStream (file)).
static Connection connectToDatabase (String fileName) throws IOException, SQLException
 Equivalent to connectToDatabase(new FileInputStream (fileName)).
static Connection connectToDatabaseFromResource (String resource) throws IOException, SQLException
 Uses connectToDatabase(InputStream) with the stream obtained from the resource resource.
static double[] readDoubleData (Statement stmt, String query) throws SQLException
 Copies the result of the SQL query query into an array of double-precision values.
static double[] readDoubleData (Connection connection, String query) throws SQLException
 Copies the result of the SQL query query into an array of double-precision values.
static double[] readDoubleData (Statement stmt, String table, String column) throws SQLException
 Returns the values of the column column of the table table.
static double[] readDoubleData (Connection connection, String table, String column) throws SQLException
 Returns the values of the column column of the table table.
static int[] readIntData (Statement stmt, String query) throws SQLException
 Copies the result of the SQL query query into an array of integers.
static int[] readIntData (Connection connection, String query) throws SQLException
 Copies the result of the SQL query query into an array of integers.
static int[] readIntData (Statement stmt, String table, String column) throws SQLException
 Returns the values of the column column of the table table.
static int[] readIntData (Connection connection, String table, String column) throws SQLException
 Returns the values of the column column of the table table.
static Object[] readObjectData (Statement stmt, String query) throws SQLException
 Copies the result of the SQL query query into an array of objects.
static Object[] readObjectData (Connection connection, String query) throws SQLException
 Copies the result of the SQL query query into an array of objects.
static Object[] readObjectData (Statement stmt, String table, String column) throws SQLException
 Returns the values of the column column of the table table.
static Object[] readObjectData (Connection connection, String table, String column) throws SQLException
 Returns the values of the column column of the table table.
static double[][] readDoubleData2D (Statement stmt, String query) throws SQLException
 Copies the result of the SQL query query into a rectangular 2D array of double-precision values.
static double[][] readDoubleData2D (Connection connection, String query) throws SQLException
 Copies the result of the SQL query query into a rectangular 2D array of double-precision values.
static double[][] readDoubleData2DTable (Statement stmt, String table) throws SQLException
 Returns the values of the columns of the table table.
static double[][] readDoubleData2DTable (Connection connection, String table) throws SQLException
 Returns the values of the columns of the table table.
static int[][] readIntData2D (Statement stmt, String query) throws SQLException
 Copies the result of the SQL query query into a rectangular 2D array of integers.
static int[][] readIntData2D (Connection connection, String query) throws SQLException
 Copies the result of the SQL query query into a rectangular 2D array of integers.
static int[][] readIntData2DTable (Statement stmt, String table) throws SQLException
 Returns the values of the columns of the table table.
static int[][] readIntData2DTable (Connection connection, String table) throws SQLException
 Returns the values of the columns of the table table.
static Object[][] readObjectData2D (Statement stmt, String query) throws SQLException
 Copies the result of the SQL query query into a rectangular 2D array of objects.
static Object[][] readObjectData2D (Connection connection, String query) throws SQLException
 Copies the result of the SQL query query into a rectangular 2D array of integers.
static Object[][] readObjectData2DTable (Statement stmt, String table) throws SQLException
 Returns the values of the columns of the table table.
static Object[][] readObjectData2DTable (Connection connection, String table) throws SQLException
 Returns the values of the columns of the table table.

Detailed Description

This 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 a 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).

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 connectToDatabase(Properties) 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.

Definition at line 64 of file JDBCManager.java.

Member Function Documentation

◆ connectToDatabase() [1/5]

Connection umontreal.ssj.util.JDBCManager.connectToDatabase ( File file) throws IOException, SQLException
static

Equivalent to connectToDatabase(new FileInputStream (file)).

Definition at line 173 of file JDBCManager.java.

◆ connectToDatabase() [2/5]

Connection umontreal.ssj.util.JDBCManager.connectToDatabase ( InputStream is) throws IOException, SQLException
static

Returns a connection to the database using the properties read from stream is.

This method loads the properties from the given stream, and calls connectToDatabase(Properties) to establish the connection.

Parameters
isthe stream to read for the properties.
Returns
the connection to the database.
Exceptions
SQLExceptionif the connection failed.
IOExceptionif the stream can not be read correctly.
IllegalArgumentExceptionif the properties do not contain the require values.

Definition at line 147 of file JDBCManager.java.

◆ connectToDatabase() [3/5]

Connection umontreal.ssj.util.JDBCManager.connectToDatabase ( Properties prop) throws SQLException
static

Connects 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 of 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.

Parameters
propthe properties to connect to the database.
Returns
the connection to the database.
Exceptions
SQLExceptionif the connection failed.
IllegalArgumentExceptionif the properties do not contain the require values.

Definition at line 90 of file JDBCManager.java.

◆ connectToDatabase() [4/5]

Connection umontreal.ssj.util.JDBCManager.connectToDatabase ( String fileName) throws IOException, SQLException
static

Equivalent to connectToDatabase(new FileInputStream (fileName)).

Definition at line 187 of file JDBCManager.java.

◆ connectToDatabase() [5/5]

Connection umontreal.ssj.util.JDBCManager.connectToDatabase ( URL url) throws IOException, SQLException
static

Equivalent to connectToDatabase(url.openStream()).

Definition at line 159 of file JDBCManager.java.

◆ connectToDatabaseFromResource()

Connection umontreal.ssj.util.JDBCManager.connectToDatabaseFromResource ( String resource) throws IOException, SQLException
static

Uses connectToDatabase(InputStream) 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(Properties) to establish the connection.

Definition at line 203 of file JDBCManager.java.

◆ readDoubleData() [1/4]

double[] umontreal.ssj.util.JDBCManager.readDoubleData ( Connection connection,
String query ) throws SQLException
static

Copies 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, to readDoubleData(Statement,String), which returns an array of double-precision values.

Parameters
connectionthe active connection to the database.
querythe SQL query to execute.
Returns
the first column of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 255 of file JDBCManager.java.

◆ readDoubleData() [2/4]

double[] umontreal.ssj.util.JDBCManager.readDoubleData ( Connection connection,
String table,
String column ) throws SQLException
static

Returns the values of the column column of the table table.

This method is equivalent to readDoubleData(connection, "SELECT column FROM table").

Definition at line 280 of file JDBCManager.java.

◆ readDoubleData() [3/4]

double[] umontreal.ssj.util.JDBCManager.readDoubleData ( Statement stmt,
String query ) throws SQLException
static

Copies 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 an SQLException if the query is not valid.

Parameters
stmtthe statement used to make the query.
querythe SQL query to execute.
Returns
the first column of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 227 of file JDBCManager.java.

◆ readDoubleData() [4/4]

double[] umontreal.ssj.util.JDBCManager.readDoubleData ( Statement stmt,
String table,
String column ) throws SQLException
static

Returns the values of the column column of the table table.

This method is equivalent to readDoubleData(stmt, "SELECT column FROM table").

Definition at line 269 of file JDBCManager.java.

◆ readDoubleData2D() [1/2]

double[][] umontreal.ssj.util.JDBCManager.readDoubleData2D ( Connection connection,
String query ) throws SQLException
static

Copies 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, to readDoubleData2D(Statement,String), which returns a 2D array of double-precision values.

Parameters
connectionthe active connection to the database.
querythe SQL query to execute.
Returns
the columns of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 479 of file JDBCManager.java.

◆ readDoubleData2D() [2/2]

double[][] umontreal.ssj.util.JDBCManager.readDoubleData2D ( Statement stmt,
String query ) throws SQLException
static

Copies 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 an SQLException if the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.

Parameters
stmtthe statement used to make the query.
querythe SQL query to execute.
Returns
the columns of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 449 of file JDBCManager.java.

◆ readDoubleData2DTable() [1/2]

double[][] umontreal.ssj.util.JDBCManager.readDoubleData2DTable ( Connection connection,
String table ) throws SQLException
static

Returns the values of the columns of the table table.

This method is equivalent to readDoubleData2D(connection, "SELECT * FROM table").

Definition at line 504 of file JDBCManager.java.

◆ readDoubleData2DTable() [2/2]

double[][] umontreal.ssj.util.JDBCManager.readDoubleData2DTable ( Statement stmt,
String table ) throws SQLException
static

Returns the values of the columns of the table table.

This method is equivalent to readDoubleData2D(stmt, "SELECT * FROM table").

Definition at line 493 of file JDBCManager.java.

◆ readIntData() [1/4]

int[] umontreal.ssj.util.JDBCManager.readIntData ( Connection connection,
String query ) throws SQLException
static

Copies 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, to readIntData(Statement,String), which returns an array of integers.

Parameters
connectionthe active connection to the database.
querythe SQL query to execute.
Returns
the first column of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 328 of file JDBCManager.java.

◆ readIntData() [2/4]

int[] umontreal.ssj.util.JDBCManager.readIntData ( Connection connection,
String table,
String column ) throws SQLException
static

Returns the values of the column column of the table table.

This method is equivalent to readIntData(connection, "SELECT column FROM table").

Definition at line 353 of file JDBCManager.java.

◆ readIntData() [3/4]

int[] umontreal.ssj.util.JDBCManager.readIntData ( Statement stmt,
String query ) throws SQLException
static

Copies 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 an SQLException if the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.

Parameters
stmtthe statement used to make the query.
querythe SQL query to execute.
Returns
the first column of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 301 of file JDBCManager.java.

◆ readIntData() [4/4]

int[] umontreal.ssj.util.JDBCManager.readIntData ( Statement stmt,
String table,
String column ) throws SQLException
static

Returns the values of the column column of the table table.

This method is equivalent to readIntData(stmt, "SELECT column FROM table").

Definition at line 342 of file JDBCManager.java.

◆ readIntData2D() [1/2]

int[][] umontreal.ssj.util.JDBCManager.readIntData2D ( Connection connection,
String query ) throws SQLException
static

Copies 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, to readIntData2D(Statement,String), which returns a 2D array of integers.

Parameters
connectionthe active connection to the database.
querythe SQL query to execute.
Returns
the columns of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 554 of file JDBCManager.java.

◆ readIntData2D() [2/2]

int[][] umontreal.ssj.util.JDBCManager.readIntData2D ( Statement stmt,
String query ) throws SQLException
static

Copies 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 an SQLException if the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.

Parameters
stmtthe statement used to make the query.
querythe SQL query to execute.
Returns
the columns of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 525 of file JDBCManager.java.

◆ readIntData2DTable() [1/2]

int[][] umontreal.ssj.util.JDBCManager.readIntData2DTable ( Connection connection,
String table ) throws SQLException
static

Returns the values of the columns of the table table.

This method is equivalent to readIntData2D(connection, "SELECT * FROM table").

Definition at line 579 of file JDBCManager.java.

◆ readIntData2DTable() [2/2]

int[][] umontreal.ssj.util.JDBCManager.readIntData2DTable ( Statement stmt,
String table ) throws SQLException
static

Returns the values of the columns of the table table.

This method is equivalent to readIntData2D(stmt, "SELECT * FROM table").

Definition at line 568 of file JDBCManager.java.

◆ readObjectData() [1/4]

Object[] umontreal.ssj.util.JDBCManager.readObjectData ( Connection connection,
String query ) throws SQLException
static

Copies 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, to readObjectData(Statement,String), which returns an array of integers.

Parameters
connectionthe active connection to the database.
querythe SQL query to execute.
Returns
the first column of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 403 of file JDBCManager.java.

◆ readObjectData() [2/4]

Object[] umontreal.ssj.util.JDBCManager.readObjectData ( Connection connection,
String table,
String column ) throws SQLException
static

Returns the values of the column column of the table table.

This method is equivalent to readObjectData(connection, "SELECT column FROM table").

Definition at line 428 of file JDBCManager.java.

◆ readObjectData() [3/4]

Object[] umontreal.ssj.util.JDBCManager.readObjectData ( Statement stmt,
String query ) throws SQLException
static

Copies 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 an SQLException if the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.

Parameters
stmtthe statement used to make the query.
querythe SQL query to execute.
Returns
the first column of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 376 of file JDBCManager.java.

◆ readObjectData() [4/4]

Object[] umontreal.ssj.util.JDBCManager.readObjectData ( Statement stmt,
String table,
String column ) throws SQLException
static

Returns the values of the column column of the table table.

This method is equivalent to readObjectData(stmt, "SELECT column FROM table").

Definition at line 417 of file JDBCManager.java.

◆ readObjectData2D() [1/2]

Object[][] umontreal.ssj.util.JDBCManager.readObjectData2D ( Connection connection,
String query ) throws SQLException
static

Copies 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, to readObjectData2D(Statement,String), which returns a 2D array of integers.

Parameters
connectionthe active connection to the database.
querythe SQL query to execute.
Returns
the columns of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 631 of file JDBCManager.java.

◆ readObjectData2D() [2/2]

Object[][] umontreal.ssj.util.JDBCManager.readObjectData2D ( Statement stmt,
String query ) throws SQLException
static

Copies 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 an SQLException if the query is not valid. The given statement stmt must not be set up to produce forward-only result sets.

Parameters
stmtthe statement used to make the query.
querythe SQL query to execute.
Returns
the columns of the result set.
Exceptions
SQLExceptionif the query is not valid.

Definition at line 602 of file JDBCManager.java.

◆ readObjectData2DTable() [1/2]

Object[][] umontreal.ssj.util.JDBCManager.readObjectData2DTable ( Connection connection,
String table ) throws SQLException
static

Returns the values of the columns of the table table.

This method is equivalent to readObjectData2D(connection, "SELECT * FROM table").

Definition at line 656 of file JDBCManager.java.

◆ readObjectData2DTable() [2/2]

Object[][] umontreal.ssj.util.JDBCManager.readObjectData2DTable ( Statement stmt,
String table ) throws SQLException
static

Returns the values of the columns of the table table.

This method is equivalent to readObjectData2D(stmt, "SELECT * FROM table").

Definition at line 645 of file JDBCManager.java.


The documentation for this class was generated from the following file: