SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
JDBCManager.java
1/*
2 * Class: JDBCManager
3 * Description: Interface to access databases
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.util;
26
27import java.io.*;
28import java.net.URL;
29import java.sql.*;
30import javax.sql.DataSource;
31import java.util.Properties;
32import javax.naming.*;
33
64public class JDBCManager {
65
90 public static Connection connectToDatabase(Properties prop) throws SQLException {
91 Connection connection = null;
92 String jndiName;
93
94 if ((jndiName = prop.getProperty("jdbc.jndi-name")) != null) {
95 try {
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);
100 }
101 } else {
102 String driver = prop.getProperty("jdbc.driver");
103 String uri = prop.getProperty("jdbc.uri");
104 if (uri != null) {
105 if (driver != null) {
106 try {
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");
111 // Needed by some buggy drivers
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 + ": "
121 + ie.getMessage());
122 }
123 }
124 connection = DriverManager.getConnection(uri);
125 } else {
126 throw new IllegalArgumentException(
127 "The jdbc.driver and jdbc.uri properties must be given if jdbc.jndi-name is not set");
128 }
129 }
130
131 return connection;
132 }
133
147 public static Connection connectToDatabase(InputStream is) throws IOException, SQLException {
148 Properties prop = new Properties();
149
150 prop.load(is);
151
152 return connectToDatabase(prop);
153 }
154
159 public static Connection connectToDatabase(URL url) throws IOException, SQLException {
160
161 InputStream is = url.openStream();
162 try {
163 return connectToDatabase(is);
164 } finally {
165 is.close();
166 }
167 }
168
173 public static Connection connectToDatabase(File file) throws IOException, SQLException {
174
175 FileInputStream is = new FileInputStream(file);
176 try {
177 return connectToDatabase(is);
178 } finally {
179 is.close();
180 }
181 }
182
187 public static Connection connectToDatabase(String fileName) throws IOException, SQLException {
188
189 FileInputStream is = new FileInputStream(fileName);
190 try {
191 return connectToDatabase(is);
192 } finally {
193 is.close();
194 }
195 }
196
203 public static Connection connectToDatabaseFromResource(String resource) throws IOException, SQLException {
204
205 InputStream is = JDBCManager.class.getClassLoader().getResourceAsStream(resource);
206 try {
207 return connectToDatabase(is);
208 } finally {
209 is.close();
210 }
211 }
212
227 public static double[] readDoubleData(Statement stmt, String query) throws SQLException {
228 ResultSet rs = stmt.executeQuery(query);
229 rs.last();
230 double[] res = new double[rs.getRow()];
231 rs.first();
232
233 for (int i = 0; i < res.length; i++) {
234 res[i] = rs.getDouble(1);
235 rs.next();
236 }
237 rs.close();
238
239 return res;
240 }
241
255 public static double[] readDoubleData(Connection connection, String query) throws SQLException {
256 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
257 try {
258 return readDoubleData(stmt, query);
259 } finally {
260 stmt.close();
261 }
262 }
263
269 public static double[] readDoubleData(Statement stmt, String table, String column) throws SQLException {
270 final String query = "SELECT " + column + " FROM " + table;
271
272 return readDoubleData(stmt, query);
273 }
274
280 public static double[] readDoubleData(Connection connection, String table, String column) throws SQLException {
281 final String query = "SELECT " + column + " FROM " + table;
282
283 return readDoubleData(connection, query);
284 }
285
301 public static int[] readIntData(Statement stmt, String query) throws SQLException {
302 ResultSet rs = stmt.executeQuery(query);
303 rs.last();
304 int[] res = new int[rs.getRow()];
305 rs.first();
306
307 for (int i = 0; i < res.length; i++) {
308 res[i] = rs.getInt(1);
309 rs.next();
310 }
311 rs.close();
312
313 return res;
314 }
315
328 public static int[] readIntData(Connection connection, String query) throws SQLException {
329 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
330 try {
331 return readIntData(stmt, query);
332 } finally {
333 stmt.close();
334 }
335 }
336
342 public static int[] readIntData(Statement stmt, String table, String column) throws SQLException {
343 final String query = "SELECT " + column + " FROM " + table;
344
345 return readIntData(stmt, query);
346 }
347
353 public static int[] readIntData(Connection connection, String table, String column) throws SQLException {
354 final String query = "SELECT " + column + " FROM " + table;
355
356 return readIntData(connection, query);
357 }
358
376 public static Object[] readObjectData(Statement stmt, String query) throws SQLException {
377 ResultSet rs = stmt.executeQuery(query);
378 rs.last();
379 Object[] res = new Object[rs.getRow()];
380 rs.first();
381
382 for (int i = 0; i < res.length; i++) {
383 res[i] = rs.getObject(1);
384 rs.next();
385 }
386 rs.close();
387
388 return res;
389 }
390
403 public static Object[] readObjectData(Connection connection, String query) throws SQLException {
404 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
405 try {
406 return readObjectData(stmt, query);
407 } finally {
408 stmt.close();
409 }
410 }
411
417 public static Object[] readObjectData(Statement stmt, String table, String column) throws SQLException {
418 final String query = "SELECT " + column + " FROM " + table;
419
420 return readObjectData(stmt, query);
421 }
422
428 public static Object[] readObjectData(Connection connection, String table, String column) throws SQLException {
429 final String query = "SELECT " + column + " FROM " + table;
430
431 return readObjectData(connection, query);
432 }
433
449 public static double[][] readDoubleData2D(Statement stmt, String query) throws SQLException {
450 ResultSet rs = stmt.executeQuery(query);
451 rs.last();
452 int c = rs.getMetaData().getColumnCount();
453 double[][] res = new double[rs.getRow()][c];
454 rs.first();
455
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);
459 rs.next();
460 }
461 rs.close();
462
463 return res;
464 }
465
479 public static double[][] readDoubleData2D(Connection connection, String query) throws SQLException {
480 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
481 try {
482 return readDoubleData2D(stmt, query);
483 } finally {
484 stmt.close();
485 }
486 }
487
493 public static double[][] readDoubleData2DTable(Statement stmt, String table) throws SQLException {
494 final String query = "SELECT * FROM " + table;
495
496 return readDoubleData2D(stmt, query);
497 }
498
504 public static double[][] readDoubleData2DTable(Connection connection, String table) throws SQLException {
505 final String query = "SELECT * FROM " + table;
506
507 return readDoubleData2D(connection, query);
508 }
509
525 public static int[][] readIntData2D(Statement stmt, String query) throws SQLException {
526 ResultSet rs = stmt.executeQuery(query);
527 rs.last();
528 int c = rs.getMetaData().getColumnCount();
529 int[][] res = new int[rs.getRow()][c];
530 rs.first();
531
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);
535 rs.next();
536 }
537 rs.close();
538
539 return res;
540 }
541
554 public static int[][] readIntData2D(Connection connection, String query) throws SQLException {
555 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
556 try {
557 return readIntData2D(stmt, query);
558 } finally {
559 stmt.close();
560 }
561 }
562
568 public static int[][] readIntData2DTable(Statement stmt, String table) throws SQLException {
569 final String query = "SELECT * FROM " + table;
570
571 return readIntData2D(stmt, query);
572 }
573
579 public static int[][] readIntData2DTable(Connection connection, String table) throws SQLException {
580 final String query = "SELECT * FROM " + table;
581
582 return readIntData2D(connection, query);
583 }
584
602 public static Object[][] readObjectData2D(Statement stmt, String query) throws SQLException {
603 ResultSet rs = stmt.executeQuery(query);
604 rs.last();
605 int c = rs.getMetaData().getColumnCount();
606 Object[][] res = new Object[rs.getRow()][c];
607 rs.first();
608
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);
612 rs.next();
613 }
614 rs.close();
615
616 return res;
617 }
618
631 public static Object[][] readObjectData2D(Connection connection, String query) throws SQLException {
632 Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
633 try {
634 return readObjectData2D(stmt, query);
635 } finally {
636 stmt.close();
637 }
638 }
639
645 public static Object[][] readObjectData2DTable(Statement stmt, String table) throws SQLException {
646 final String query = "SELECT * FROM " + table;
647
648 return readObjectData2D(stmt, query);
649 }
650
656 public static Object[][] readObjectData2DTable(Connection connection, String table) throws SQLException {
657 final String query = "SELECT * FROM " + table;
658
659 return readObjectData2D(connection, query);
660 }
661
662}
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.