分类: Oracle
2008-05-22 17:29:38
来源: |
|
对JDBC OCI接口: Fow Windows: 1.安装Oracle Client. 2.根据jdk的版本,设置CLASSPATH,使其包含正确的classesxx.zip 3.根据需要设置CLASSPATH,使其指向Oracle的其它zip文件 4.设置PATH,使其包含 $ORACLE_HOMEbin目录 For unix: 1.安装Oracle Client. 2.根据jdk的版本,设置CLASSPATH,使其包含正确的classesxx.zip 3.根据需要设置CLASSPATH,使其指向Oracle的其它zip文件 4.设置LD_LIBRARY_PATH,使其包含 $ORACLE_HOME/lib目录 备注: classesxx.zip一般在ORACLE_HOMEjdbclib目录下。 在ORACLE_HOMEjdbclib目录下的与Oracle JDBC Drives驱动有关的文件的解释: - classes12.zip Classes for use with JDK 1.2.x. It contains the JDBC driver classes except classes necessary for NLS support in Object and Collection types. - nls_charset12.zip NLS classes for use with JDK 1.2.x. It contains classes necessary for NLS support in Object and Collection types. - classes12_g.zip Same as classes12.zip, except that classes were compiled with "javac -g". JDBC数据库的语法: JDBC THIN: Code: [Copy to clipboard] Connection conn= DriverManager.getConnection ("jdbc:oracle:thin:@dlsun511:1521:ora1","scott","tiger"); | | | machine(ip@) : port# : sid JDBC OCI: Code: [Copy to clipboard] Connection conn= DriverManager.getConnection ("jdbc:oracle:oci8[9]:@RAC","scott","tiger"); | Net Service JDBC THIN与JDBC THIN对比: 相同之处: The JDBC Thin, JDBC OCI, and JDBC Server drivers all provide the same functionality. They all support the following standards and features: * JDBC 2.0 * Partial JDBC 3.0 (in JDBC driver version 9.2) * the same syntax and APIs * the same Oracle extensions 主要是JDBC OCI 接口比JDBC THIN接口效率高! How does one connect with the JDBC Thin Driver? The the JDBC thin driver provides the only way to access Oracle from the Web (applets). It is smaller and slower than the OCI drivers. import java.sql.*; Code: [Copy to clipboard] class dbAccess { public static void main (String args []) throws SQLException { DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver() ); Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@dbhost:1521:ORA1", "scott", "tiger"); // @machine:port:SID, userid, password Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery ( "select BANNER from SYS.V_ $VERSION" ); while (rset.next()) System.out.println (rset.getString(1)); // Print col 1 stmt.close(); } } How does one connect with the JDBC OCI Driver? One must have Net8 (SQL*Net) installed and working before attempting to use one of the OCI drivers. Code: [Copy to clipboard] import java.sql.*; class dbAccess { public static void main (String args []) throws SQLException { try { Class.forName ("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } Connection conn = DriverManager.getConnection ("jdbc:oracle:oci8:@ORA1", "scott", "tiger"); // or oci9 @Service, userid, password Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery ( "select BANNER from SYS.V_ $VERSION" ); while (rset.next()) System.out.println (rset.getString(1)); // Print col 1 stmt.close(); } } How does one connect with the JDBC KPRB Driver? One can obtain a handle to the default or current connection (KPRB driver) by calling the OracleDriver.defaultConenction() method. Please note that you do not need to specify a database URL, username or password as you are already connected to a database session. Remember not to close the default connection. Closing the default connection might throw an exception in future releases of Oracle. import java.sql.*; Code: [Copy to clipboard] class dbAccess { public static void main (String args []) throws SQLException { Connection conn = (new oracle.jdbc.driver.OracleDriver()).defaultConnection(); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery ( "select BANNER from SYS.V_ $VERSION" ); |