java与oracle的接口:
在数据库中运行JAVA可以说是ORACLE8i的最令人激动的新特性。在你创建的使用ORACLE8i 数据库的应用程序中,你可以使用与JAVA有关的新特征,轻松的将程序发布到INTERNET或INTRANET上。
Methods for Using Java in ORACLE
大家都知道JAVA在跨平台开发与INTERNET开发中已经比较流行,ORACLE8i及以后的版本中都包含了对在数据库中运行JAVA的扩展支持,这里有两种方法可以使用:
JDBC:与ODBC类似, JDBC 提供了一个驱动接口使你可以在JAVA程序中访问数据库。注:JDBC驱动内嵌在数据库中虚拟机中。
SQLJ:是一个JAVA预编译器,它可以将内嵌的SQL语句转化为JAVA语句.SQLJ的使用与运行机理与其它ORACLE的与编译器(如Pro*C,Pro*COBOL)类似。实际上,为了使我们形象的记住SQLJ提供的功能,我们也可以直接将SQLJ改名为Pro*Java。
将JAVA集成到数据库中是双向的。也就是说你可以在JAVA中调用SQL与PL/SQL,也可以在SQL与PL/SQL中调用JAVA。JAVA程序可以直接通过JDBC驱动调用SQL与PL/SQL,反过来,你也可以在SQL与PL/SQL中直接调用JAVA。在数据库中,JAVA命名空间直接映射到数据库模式的命名空间中,这样可以方便JAVA的存取与调用。数据库同时提供扩展的DDL语句,通过这些语句,你可以象创建一个存储过程一样在数据中创建内嵌的JAVA程序。
Features of ORACLE JDBC Drivers
在ORACLE8i中有三种类型的JDBC驱动,他们都使用相同的 syntax, APIs, and Oracle extensions,以使JAVA代码在robust clients、Web-based Java applets, and Java stored procedures之间保持轻便灵活:三种类型如下:
1.JDBC
OCI: 此驱动类似于传统的ODBC 驱动。因为它需要Oracle Call Interface and Net8,所以它需要在运行使用此驱动的JAVA程序的机器上安装客户端软件
2.JDBC Thin: 这种驱动一般用在运行在WEB浏览器中的JAVA程序。它不是通过OCI or Net8,而是通过Java sockets进行通信 ,因此不需要在使用JDBC Thin的客户端机器上安装客户端软件。
3.JDBC KPRB: 这种驱动由直接存储在数据库中的JAVA程序使用,如Java Stored Procedures 、triggers、Database JSP's。It uses the default/ current database session and thus requires no additional database username, password or URL.
如何配置使JAVA可以通过Oracle JDBC Drivers连接到数据库:1.安装Sun JDK.
2. 修改PATH环境变量,使其指向JDK的bin目录
3. 设置CLASSPATH环境变量,使其指向正确的JDK的lib及oracle的JDBC接口。
CLASSPATH = ".;????"
3. 运行"java ?version" ,验证java的版本。
如何在不同的操作系统上根据接口类型设置客户端:
对JDBC THIN接口:
在windows与unix下的设置方法一样:
1.根据jdk的版本,只需要将classesxx.zip拷贝到指定的目录,不需要安装Oracle Client。在装完数据库后,该文件会在$ORACLE_HOME/jdbc/lib目录下。2.设置CLASSPATH,使其包含上面的classesxx.zip
3.根据需要,拷贝oracle的其它zip文件并设置CLASSPATH
对JDBC OCI接口:
Fow Windows:
1.安装Oracle Client.
2.根据jdk的版本,设置CLASSPATH,使其包含正确的classesxx.zip
3.根据需要设置CLASSPATH,使其指向Oracle的其它zip文件
4.设置PATH,使其包含$ORACLE_HOME\bin目录
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".
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"
);