/* ** +-----------------------------------------------+ ** | FILE : OracleConnection.java | ** | AUTHOR : Jeff Hunter | ** | DATE : 06-DEC-2001 | ** +-----------------------------------------------+ */ import java.sql.*; import java.util.*; import oracle.jdbc.driver.*; public class OracleConnection { /* ** +----------------------------------------+ ** | Return a JDBC connection appropiately | ** | either inside or outside the database. | ** +----------------------------------------+ */ public static Connection getConnection() throws SQLException { String username = "jdbc_demo"; String password = "jdbc_demo"; String driver_class = "oracle.jdbc.driver.OracleDriver"; // +-------------------------------------------------+ // | You can use either of the two URLs to obtain a | // | default connection. | // | "jdbc:default:connection:" | // | "jdbc:oracle:kprb:" | // +-------------------------------------------------+ String defaultConn = "jdbc:default:connection:"; String thinConn = "jdbc:oracle:thin:@cartman:1521:O901DB"; Connection conn = null; try { System.out.print("Loading JDBC Driver -> " + driver_class + "\n"); Class.forName (driver_class).newInstance(); //Driver d = new oracle.jdbc.driver.OracleDriver(); if (connectedToDatabase()) { System.out.print("Connecting to -> " + defaultConn + "\n"); System.out.print("Database Version -> " + System.getProperty("oracle.server.version") + "\n"); conn = DriverManager.getConnection(defaultConn); } else { System.out.print("Connecting to -> " + thinConn + ", " + password + ", " + password + "\n"); conn = DriverManager.getConnection(thinConn, username, password); } conn.setAutoCommit(false); return conn; } catch (Exception e) { throw new SQLException("Error loading JDBC Driver"); } } // METHOD: (getConnection) /* ** +--------------------------------------------------------+ ** | Method to determine if we are running in the database. | ** | If oracle.server.version is not null, we are running | ** | in the database. | ** +--------------------------------------------------------+ */ public static boolean connectedToDatabase() { String version = System.getProperty("oracle.server.version"); return (version != null && !version.equals("")); } // METHOD: (connectedToDatabase) } // CLASS: (OracleConnection)