25package umontreal.ssj.util;
30import javax.sql.DataSource;
31import java.util.Properties;
91 Connection connection =
null;
94 if ((jndiName = prop.getProperty(
"jdbc.jndi-name")) !=
null) {
96 InitialContext context =
new InitialContext();
97 connection = ((DataSource) context.lookup(jndiName)).getConnection();
98 }
catch (NamingException e) {
99 throw new IllegalArgumentException(
"The jdbc.jndi-name property refers to the invalid name " + jndiName);
102 String driver = prop.getProperty(
"jdbc.driver");
103 String uri = prop.getProperty(
"jdbc.uri");
105 if (driver !=
null) {
107 Class driverClass = Class.forName(driver);
108 if (!Driver.class.isAssignableFrom(driverClass))
109 throw new IllegalArgumentException(
"The driver name " + driver
110 +
" does not correspond to a class implementing the java.sql.Driver interface");
112 driverClass.newInstance();
113 }
catch (ClassNotFoundException cnfe) {
114 throw new IllegalArgumentException(
"Could not find the driver class " + driver);
115 }
catch (IllegalAccessException iae) {
116 throw new IllegalArgumentException(
117 "An illegal access prevented the instantiation of driver class " + driver);
118 }
catch (InstantiationException ie) {
119 throw new IllegalArgumentException(
120 "An instantiation exception prevented the instantiation of driver class " + driver +
": "
124 connection = DriverManager.getConnection(uri);
126 throw new IllegalArgumentException(
127 "The jdbc.driver and jdbc.uri properties must be given if jdbc.jndi-name is not set");
148 Properties prop =
new Properties();
161 InputStream is = url.openStream();
175 FileInputStream is =
new FileInputStream(file);
189 FileInputStream is =
new FileInputStream(fileName);
205 InputStream is =
JDBCManager.class.getClassLoader().getResourceAsStream(resource);
227 public static double[]
readDoubleData(Statement stmt, String query)
throws SQLException {
228 ResultSet rs = stmt.executeQuery(query);
230 double[] res =
new double[rs.getRow()];
233 for (
int i = 0; i < res.length; i++) {
234 res[i] = rs.getDouble(1);
255 public static double[]
readDoubleData(Connection connection, String query)
throws SQLException {
256 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
269 public static double[]
readDoubleData(Statement stmt, String table, String column)
throws SQLException {
270 final String query =
"SELECT " + column +
" FROM " + table;
280 public static double[]
readDoubleData(Connection connection, String table, String column)
throws SQLException {
281 final String query =
"SELECT " + column +
" FROM " + table;
301 public static int[]
readIntData(Statement stmt, String query)
throws SQLException {
302 ResultSet rs = stmt.executeQuery(query);
304 int[] res =
new int[rs.getRow()];
307 for (
int i = 0; i < res.length; i++) {
308 res[i] = rs.getInt(1);
328 public static int[]
readIntData(Connection connection, String query)
throws SQLException {
329 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
342 public static int[]
readIntData(Statement stmt, String table, String column)
throws SQLException {
343 final String query =
"SELECT " + column +
" FROM " + table;
353 public static int[]
readIntData(Connection connection, String table, String column)
throws SQLException {
354 final String query =
"SELECT " + column +
" FROM " + table;
376 public static Object[]
readObjectData(Statement stmt, String query)
throws SQLException {
377 ResultSet rs = stmt.executeQuery(query);
379 Object[] res =
new Object[rs.getRow()];
382 for (
int i = 0; i < res.length; i++) {
383 res[i] = rs.getObject(1);
403 public static Object[]
readObjectData(Connection connection, String query)
throws SQLException {
404 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
417 public static Object[]
readObjectData(Statement stmt, String table, String column)
throws SQLException {
418 final String query =
"SELECT " + column +
" FROM " + table;
428 public static Object[]
readObjectData(Connection connection, String table, String column)
throws SQLException {
429 final String query =
"SELECT " + column +
" FROM " + table;
449 public static double[][]
readDoubleData2D(Statement stmt, String query)
throws SQLException {
450 ResultSet rs = stmt.executeQuery(query);
452 int c = rs.getMetaData().getColumnCount();
453 double[][] res =
new double[rs.getRow()][c];
456 for (
int i = 0; i < res.length; i++) {
457 for (
int j = 0; j < res[i].length; j++)
458 res[i][j] = rs.getDouble(1 + j);
479 public static double[][]
readDoubleData2D(Connection connection, String query)
throws SQLException {
480 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
494 final String query =
"SELECT * FROM " + table;
505 final String query =
"SELECT * FROM " + table;
525 public static int[][]
readIntData2D(Statement stmt, String query)
throws SQLException {
526 ResultSet rs = stmt.executeQuery(query);
528 int c = rs.getMetaData().getColumnCount();
529 int[][] res =
new int[rs.getRow()][c];
532 for (
int i = 0; i < res.length; i++) {
533 for (
int j = 0; j < res[i].length; j++)
534 res[i][j] = rs.getInt(1 + j);
554 public static int[][]
readIntData2D(Connection connection, String query)
throws SQLException {
555 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
569 final String query =
"SELECT * FROM " + table;
580 final String query =
"SELECT * FROM " + table;
602 public static Object[][]
readObjectData2D(Statement stmt, String query)
throws SQLException {
603 ResultSet rs = stmt.executeQuery(query);
605 int c = rs.getMetaData().getColumnCount();
606 Object[][] res =
new Object[rs.getRow()][c];
609 for (
int i = 0; i < res.length; i++) {
610 for (
int j = 0; j < res[i].length; j++)
611 res[i][j] = rs.getObject(1 + j);
631 public static Object[][]
readObjectData2D(Connection connection, String query)
throws SQLException {
632 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
646 final String query =
"SELECT * FROM " + table;
657 final String query =
"SELECT * FROM " + table;
This class provides some facilities to connect to a SQL database and to retrieve data stored in it.
static int[] readIntData(Connection connection, String query)
Copies the result of the SQL query query into an array of integers.
static Object[][] readObjectData2DTable(Statement stmt, String table)
Returns the values of the columns of the table table.
static int[] readIntData(Statement stmt, String table, String column)
Returns the values of the column column of the table table.
static Object[][] readObjectData2D(Connection connection, String query)
Copies the result of the SQL query query into a rectangular 2D array of integers.
static Object[] readObjectData(Statement stmt, String table, String column)
Returns the values of the column column of the table table.
static int[][] readIntData2DTable(Statement stmt, String table)
Returns the values of the columns of the table table.
static Connection connectToDatabase(Properties prop)
Connects to the database using the properties prop and returns the an object representing the connect...
static double[][] readDoubleData2D(Connection connection, String query)
Copies the result of the SQL query query into a rectangular 2D array of double-precision values.
static Connection connectToDatabase(String fileName)
Equivalent to connectToDatabase(newFileInputStream (fileName)).
static int[][] readIntData2D(Statement stmt, String query)
Copies the result of the SQL query query into a rectangular 2D array of integers.
static Connection connectToDatabase(File file)
Equivalent to connectToDatabase(newFileInputStream (file)).
static Object[][] readObjectData2D(Statement stmt, String query)
Copies the result of the SQL query query into a rectangular 2D array of objects.
static double[] readDoubleData(Connection connection, String query)
Copies the result of the SQL query query into an array of double-precision values.
static Object[] readObjectData(Connection connection, String query)
Copies the result of the SQL query query into an array of objects.
static Connection connectToDatabase(InputStream is)
Returns a connection to the database using the properties read from stream is.
static Connection connectToDatabaseFromResource(String resource)
Uses connectToDatabase(InputStream) with the stream obtained from the resource resource.
static double[][] readDoubleData2D(Statement stmt, String query)
Copies the result of the SQL query query into a rectangular 2D array of double-precision values.
static Object[][] readObjectData2DTable(Connection connection, String table)
Returns the values of the columns of the table table.
static double[] readDoubleData(Statement stmt, String query)
Copies the result of the SQL query query into an array of double-precision values.
static int[][] readIntData2D(Connection connection, String query)
Copies the result of the SQL query query into a rectangular 2D array of integers.
static Connection connectToDatabase(URL url)
Equivalent to connectToDatabase(url.openStream()).
static int[][] readIntData2DTable(Connection connection, String table)
Returns the values of the columns of the table table.
static int[] readIntData(Statement stmt, String query)
Copies the result of the SQL query query into an array of integers.
static double[][] readDoubleData2DTable(Connection connection, String table)
Returns the values of the columns of the table table.
static double[][] readDoubleData2DTable(Statement stmt, String table)
Returns the values of the columns of the table table.
static int[] readIntData(Connection connection, String table, String column)
Returns the values of the column column of the table table.
static Object[] readObjectData(Connection connection, String table, String column)
Returns the values of the column column of the table table.
static double[] readDoubleData(Statement stmt, String table, String column)
Returns the values of the column column of the table table.
static Object[] readObjectData(Statement stmt, String query)
Copies the result of the SQL query query into an array of objects.
static double[] readDoubleData(Connection connection, String table, String column)
Returns the values of the column column of the table table.