javaee连接Oracle数据库的各种方法.docx_第1页
javaee连接Oracle数据库的各种方法.docx_第2页
javaee连接Oracle数据库的各种方法.docx_第3页
javaee连接Oracle数据库的各种方法.docx_第4页
javaee连接Oracle数据库的各种方法.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java连接oracle数据库的各种方法及java在数据库中的含义 - 又是我的总结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之间保持轻便灵活:三种类型如下:1JDBCOCI: 此驱动类似于传统的ODBC 驱动。因为它需要Oracle Call Interface and Net8,所以它需要在运行使用此驱动的JAVA程序的机器上安装客户端软件2JDBC Thin: 这种驱动一般用在运行在WEB浏览器中的JAVA程序。它不是通过OCI or Net8,而是通过Java sockets进行通信 ,因此不需要在使用JDBC Thin的客户端机器上安装客户端软件。 3JDBC KPRB: 这种驱动由直接存储在数据库中的JAVA程序使用,如Java Stored Procedures 、triggers、Database JSPs。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.zip3根据需要,拷贝oracle的其它zip文件并设置CLASSPATH对JDBC OCI接口:Fow Windows:1安装Oracle Client.2根据jdk的版本,设置CLASSPATH,使其包含正确的classesxx.zip3根据需要设置CLASSPATH,使其指向Oracle的其它zip文件4设置PATH,使其包含$ORACLE_HOMEbin目录For unix:1安装Oracle Client.2根据jdk的版本,设置CLASSPATH,使其包含正确的classesxx.zip3根据需要设置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: 1. Connection conn= 2. DriverManager.getConnection 3. (jdbc:oracle:thin:dlsun511:1521:ora1,scott,tiger); 4. | | | 5. machine(ip) : port# : sid 复制代码JDBC OCI: 1. Connection conn= 2. DriverManager.getConnection 3. (jdbc:oracle:oci89:RAC,scott,tiger); 4. | 5. 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.*; 1. class dbAccess 2. public static void main (String args ) throws SQLException3. 4. DriverManager.registerDriver (5. new oracle.jdbc.driver.OracleDriver()6. );7.8. Connection conn = DriverManager.getConnection9. (jdbc:oracle:thin:dbhost:1521:ORA1, scott, tiger);10. / machine:port:SID, userid,password11.12. Statement stmt = conn.createStatement();13. ResultSet rset = stmt.executeQuery (14. select BANNER from SYS.V_$VERSION15. );16. while (rset.next()17. System.out.println (rset.getString(1); / Print col 118. stmt.close();19. 20. 复制代码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. 1. import java.sql.*;2. class dbAccess 3. public static void main (String args ) throws SQLException4. 5. try 6. Class.forName (oracle.jdbc.driver.OracleDriver);7. catch (ClassNotFoundException e) 8. e.printStackTrace();9. 10.11. Connection conn = DriverManager.getConnection12. (jdbc:oracle:oci8:ORA1, scott, tiger);13. / or oci9 Service, userid,password14. Statement stmt = conn.createStatement();15. ResultSet rset = stmt.executeQuery (16. select BANNER from SYS.V_$VERSION17. );18. while (rset.next()19. System.out.println (rset.getString(1); / Print col 120. stmt.close();21. 22. 复制代码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.*; 1. class dbAccess 2. public static void main (String args ) throws SQLException3. 4. Connection conn = (new5. oracle.jdbc.driver.OracleDriver().defaultConnection();6.7. Statement stmt = conn.createStatement();8. ResultSet rset = stmt.executeQuery (9. select BANNER from SYS.V_$VERSION10. );11. while (rset.next()12. System.out.println (rset.getString(1); / Print col 113. stmt.close();14. 15. 复制代码与JAVA有关的初始化参数:=Executing initjvm.sql also highlights some new initsid.ora parameters that are used to support Java in your Oracle8i database. These parameters, their descriptions, and the settings required for running initjvm.sql, are all shown in the following list:? SHARED_POOL_SIZE? Defines the size of your shared pool in bytes. This should be set to at least 50MB to run initjvm.sql. ? JAVA_POOL_SIZE? Defines the size of the Java pool, a new area of the SGAin Oracle8i used to store shared Java objects. This should be set to 50MB when running initjvm.sql, but can be as low as 20MB for normal use of Java stored procedures. ? JAVA_SOFT_SESSIONSPACE_LIMIT? Identifies a soft limit on memory used by Java in a session. The default is 1MB. If this limit is exceeded, a warning is written to the ALERT log. ? JAVA_MAX_SESSIONSPACE_SIZE? Identifies the maximum amount of memory that can be used by a Java procedure; the default is 4GB. When the limit set by this parameter is exceeded, the executing Java procedure is killed by Oracle8i automatically. 如果将JAVA程序存放在数据库中,并运行存储在数据库中的JAVA程序,则数据库中会启用JAVA的虚拟机,为了保证JAVA虚拟机有效的运行,你需要设置上面介绍的参数。如何将一个JAVA程序装载到数据库并且发布出去?= 就像前面说得,java程序或类可以被存储到数据库中,作为PL/SQL的替换或补充。Java可以被用来作为数据库的触发器、存储过程、函数、对象的成员函数。在按照下面的过程开发完java存储过程后,就可以从SQL或PL/SQL中调用JAVA存储过程,就像调用普通的PL/SQL过程一样。下面的代码描述了如何在SQL*PLUS中开发和使用一个 输出Hello, World 的JAVA程序的例子:1. Write the Java program using a Java development environment like Jdeveloper or JBuilder.2. Load the Java program into Oracle8i using either the create or replace java source command, or with the LOADJAVA utility.3. Publish your Java procedure to SQL. This step identifies your Java procedure to SQL and PL/SQL by exposing the procedure entry point, mapping datatypes in Java to PL/SQL or SQL, and indicating parameter-passing b

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论