For unix: 1.安裝Oracle Client.
2.根據jdk的版本,設定CLASSPATH,使其包含正確的classesxx.zip
3.根據需要設定CLASSPATH,使其指向Oracle的其它zip檔案
4.設定LD_LIBRARY_PATH,使其包含$ORACLE_HOME/lib目錄
備忘: classesxx.zip一般在ORACLE_HOME\jdbc\lib目錄下。
在ORACLE_HOME\jdbc\lib目錄下的與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".
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.*;
class dbAccess {
public static void main (String args []) throws SQLException
{
DriverManager.registerDriver (
new oracle.jdbc.driver.OracleDriver()
);
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.
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.*;
class dbAccess {
public static void main (String args []) throws SQLException
{
Connection conn = (new
oracle.jdbc.driver.OracleDriver()).defaultConnection();