在数据库中运行JAVA可以说是ORACLE8i的最令人激动的新特性。在你创建的使用ORACLE8i 数据库的应用程序中,你可以使用与JAVA有关的新特征,轻松的将程序发布到INTERNET或INTRANET上。 ADD1a+gR
y zlEpG;N
Methods for Using Java in ORACLE ojGAm@'s2
~GX* C{cq
大家都知道JAVA在跨平台开发与INTERNET开发中已经比较流行,ORACLE8i及以后的版本中都包含了对在数据库中运行JAVA的扩展支持,这里有两种方法可以使用: +brLiL}Y
wG}\$NM#
JDBC:与ODBC类似, JDBC 提供了一个驱动接口使你可以在JAVA程序中访问数据库。注:JDBC驱动内嵌在数据库中虚拟机中。 KgsV868
$$\Nc3
SQLJ:是一个JAVA预编译器,它可以将内嵌的SQL语句转化为JAVA语句.SQLJ的使用与运行机理与其它ORACLE的与编译器(如Pro*C,Pro*COBOL)类似。实际上,为了使我们形象的记住SQLJ提供的功能,我们也可以直接将SQLJ改名为Pro*Java。 p~a#RO#6
QG'6vbN
将JAVA集成到数据库中是双向的。也就是说你可以在JAVA中调用SQL与PL/SQL,也可以在SQL与PL/SQL中调用JAVA。JAVA程序可以直接通过JDBC驱动调用SQL与PL/SQL,反过来,你也可以在SQL与PL/SQL中直接调用JAVA。在数据库中,JAVA命名空间直接映射到数据库模式的命名空间中,这样可以方便JAVA的存取与调用。数据库同时提供扩展的DDL语句,通过这些语句,你可以象创建一个存储过程一样在数据中创建内嵌的JAVA程序。 nXL-_6p
mD#NK1. ~B
Features of ORACLE JDBC Drivers Ys%"h{z29
zvv+b>W"f
在ORACLE8i中有三种类型的JDBC驱动,他们都使用相同的 syntax, APIs, and Oracle extensions,以使JAVA代码在robust clients、Web-based Java applets, and Java stored procedures之间保持轻便灵活:三种类型如下: 2=&VKw&
1.JDBC OCI: 此驱动类似于传统的ODBC 驱动。因为它需要Oracle Call Interface and Net8,所以它需要在运行使用此驱动的JAVA程序的机器上安装客户端软件 A x.YJ/n-
2.JDBC Thin: 这种驱动一般用在运行在WEB浏览器中的JAVA程序。它不是通过OCI or Net8,而是通过Java sockets进行通信 ,因此不需要在使用JDBC Thin的客户端机器上安装客户端软件。 U3a,: Dn
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. J_\{fI]l
8w(CTv
如何配置使JAVA可以通过Oracle JDBC Drivers连接到数据库:1.安装Sun JDK. 7:a5grj 32
2. 修改PATH环境变量,使其指向JDK的bin目录 zPT* T-2
3. 设置CLASSPATH环境变量,使其指向正确的JDK的lib及oracle的JDBC接口。 HJ&"NgL9@
CLASSPATH = ".;????" bTpkUvam
3. 运行"java –version" ,验证java的版本。 }FHagj~y
:$E?HI 'E
如何在不同的操作系统上根据接口类型设置客户端: =BtC1wL
对JDBC THIN接口: 1ID!qz6W
在windows与unix下的设置方法一样: 9%7*~bW_
1.根据jdk的版本,只需要将classesxx.zip拷贝到指定的目录,不需要安装Oracle Client。在装完数据库后,该文件会在$ORACLE_HOME/jdbc/lib目录下。2.设置CLASSPATH,使其包含上面的classesxx.zip 3(3 EoUj
3.根据需要,拷贝oracle的其它zip文件并设置CLASSPATH pNQBz]G)
&c3'yu
对JDBC OCI接口: pc kI
Fow Windows: ^BHPb1oT
1.安装Oracle Client. b~KsX(]
2.根据jdk的版本,设置CLASSPATH,使其包含正确的classesxx.zip ygvFKneOdm
3.根据需要设置CLASSPATH,使其指向Oracle的其它zip文件 }dJ}@1
4.设置PATH,使其包含$ORACLE_HOME\bin目录 enq?Vx `
o" gw#w
For unix: ^FHD5.7)b
1.安装Oracle Client. uNvU)]t
2.根据jdk的版本,设置CLASSPATH,使其包含正确的classesxx.zip 5d[,?I8v
3.根据需要设置CLASSPATH,使其指向Oracle的其它zip文件 3bQE;
4.设置LD_LIBRARY_PATH,使其包含$ORACLE_HOME/lib目录 R$s( qz
M^ 7L
备注: `I A`' c
classesxx.zip一般在ORACLE_HOME\jdbc\lib目录下。 `p{d`j6
+p`j}AQ ]
在ORACLE_HOME\jdbc\lib目录下的与Oracle JDBC Drives驱动有关的文件的解释: vT/uE[}Kq
- classes12.zip !q6 fe:
Classes for use with JDK 1.2.x. It contains the JDBC driver r& j V!u
classes except classes necessary for NLS support in Object and uXJDF3jD}
Collection types. }ofbQ,
ZpI Bt_V
- nls_charset12.zip )jb 48E
NLS classes for use with JDK 1.2.x. It contains classes necessary 4l27guy
for NLS support in Object and Collection types. a+K%@Kh~
<`,: ," - classes12_g.zip R8cS NXf{ Same as classes12.zip, except that classes were compiled with <`bJM]D-/ "javac -g". }(H&>f1z
po<`Ev3C< JDBC连接数据库的语法: Pk C- P d] JDBC THIN: 5;lejPk hBq4EzK 0m!`ki% Code: [Copy to clipboard] TJOC !QC w Connection conn= *TZsN<>5
DriverManager.getConnection +5BE5[T
("jdbc:oracle:thin:@dlsun511:1521:ora1","scott","tiger"); [-h#Ygg
u(d;9"I
machine(ip@) : port# : sid t+&-k,)
UAM%.;w
JDBC OCI: lo/]0e.T
Bz&,r +/
Code: [Copy to clipboard] ;"L#5P}
Connection conn= 8Toz"^K]l
DriverManager.getConnection [Uuu7Pc%
("jdbc:oracle:oci8[9]:@RAC","scott","tiger"); S!j+Hk
}Q@?z>I(@
Net Service qt)mh0t9
#lJ^#
JDBC THIN与JDBC THIN对比: >S='*
相同之处: }(q+Z}T6X
The JDBC Thin, JDBC OCI, and JDBC Server drivers all provide the same functionality. They all support the following standards and features: !Wx)MK&
* JDBC 2.0 >5%rq2
* Partial JDBC 3.0 (in JDBC driver version 9.2) 'z9;qm.{
* the same syntax and APIs o{x,Znr9
* the same Oracle extensions ;V)qa:H%Q
主要是JDBC OCI 接口比JDBC THIN接口效率高! Ypjm, _rHV
>5^s_1#i
How does one connect with the JDBC Thin Driver? =C&iY!c8V
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. (J)BrD>
import java.sql.*; `kJOn}Y
]M ov% !
Code: [Copy to clipboard] . hr4 X/W
class dbAccess { {f*)pE
public static void main (String args []) throws SQLException =r$[>F7c
{ t^2"S`G< DriverManager.registerDriver ( F*qVzl}w new oracle.jdbc.driver.OracleDriver() &wpXXu)< ); S?[am8} cJFe=(@3 Connection conn = DriverManager.getConnection hUXA=s ("jdbc:oracle:thin:@dbhost:1521:ORA1", "scott", "tiger"); =,MJSpxt& // @machine:port:SID, userid, password M*/ 7fXg gTD`?*! Statement stmt = conn.createStatement(); %# Ju\ ResultSet rset = stmt.executeQuery ( ;j#HfsO "select BANNER from SYS.V_$VERSION" 4 v2)CfmKs ); CC~z4cm' while (rset.next()) STcpxdGK System.out.println (rset.getString(1)); // Print col 1 pl@x}U>de
stmt.close(); \qf$
} +tP5Rr~`
} VAjy_%fR;
How does one connect with the JDBC OCI Driver? B40=S~oon
One must have Net8 (SQL*Net) installed and working before attempting to use one of the OCI drivers. ;$]p&NG
[sl(,8!,
a #Qb,Tq-
Code: [Copy to clipboard] >Aqn">*
import java.sql.*; "'.fF e}C
class dbAccess { %eXb x+5f
public static void main (String args []) throws SQLException / 7[+,G
{ 4+Q<}ST try { gvbsgo6a Class.forName ("oracle.jdbc.driver.OracleDriver"); Iq!s5'8 } catch (ClassNotFoundException e) { n4>):n#
e.printStackTrace(); {Q=n@Z5
} 0e<4 conn =" DriverManager.getConnection">toS6DS
("jdbc:oracle:oci8:@ORA1", "scott", "tiger"); P8 /k*}
// or oci9 @Service, userid, password 1a(vH7
Statement stmt = conn.createStatement(); -g9'o!=A
ResultSet rset = stmt.executeQuery ( 1{y* U@
"select BANNER from SYS.V_$VERSION" !RS(F j =(
); FG{t/^Dr
while (rset.next()) CDKMW8\
System.out.println (rset.getString(1)); // Print col 1 Z+ai;, >
stmt.close(); 9%8, A'~
} frj! D0
} Ov"4^S
How does one connect with the JDBC KPRB Driver? ] CWg{&
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. Y@&O(Dg{
import java.sql.*; 9sK`vnE^
N.KaFe'Pa
public static void main (String args []) throws SQLException + 5d;vcB=I
{ ?#p[:[iq
Connection conn = (new )]}+tI+!
oracle.jdbc.driver.OracleDriver()).defaultConnection(); TplHvoi
f_a));@
Statement stmt = conn.createStatement(); 3E:d_ ]NM
ResultSet rset = stmt.executeQuery ( aC~hz.3$
"select BANNER from SYS.V_$VERSION" yh164M
);
没有评论:
发表评论