(Strongly recommended) Java connection Oracle database and the meaning of Java in the database (Sunnyxu original)

xiaoxiao2021-03-06  42

Java and Oracle interface:

Running Java in the database can be said to be the most exciting new feature of Oracle8i. In applications you created using Oracle8i database, you can use new features related to Java, easy to publish the program to the Internet or intranet.

Methods for using java in oracle ===================================

Everyone knows that Java has been popular in cross-platform development and Internet development, and Oracle8i and later versions contain extension support to run Java in the database. There are two ways to use:

JDBC: Similar to ODBC, JDBC provides a driver interface to enable you to access the database in the Java program. Note: The JDBC driver is embedded in the virtual machine in the database.

SQLJ: is a Java Preciator that converts the embedded SQL statement to Java statement. SQLJ use and run mechanism similar to other Oracle (such as Pro * C, Pro * COBOL). In fact, in order to keep our image remember the function of SQLJ, we can also rename SQLJ directly to Pro * Java.

It is two-way integrating Java into the database. That is, you can call SQL and PL / SQL in Java, or you can call Java in SQL and PL / SQL. The Java program can call SQL and PL / SQL directly through the JDBC driver, but you can also call Java directly in SQL and PL / SQL. In the database, the Java namespace is directly mapped to the namespace of the database mode, which can be convenient for Java access and calls. The database also provides an extended DDL statement, through these statements, you can create an embedded Java program like creating a stored procedure.

Features of Oracle JDBC Drivers

==========================================================================================================================================================

In Oracle8i, there are three types of JDBC drivers, they use the same syntax, apis, and Oracle Extensions to keep Java code in Robust Clients, Web-Based Java Applets, and Java Stored Procedures to maintain light and flexible: three types as follows:

1. JDBC OCI: This drive is similar to the traditional ODBC driver. Because it requires Oracle Call Interface and Net8, it needs to install client software on the machine running using this driver Java program.

2. JDBC Thin: This drive is typically used in the Java program running in a web browser. It is not via OCI or Net8, but communicates with Java Sockets, so it is not necessary to install client software on a client machine using JDBC Thin.

3. JDBC KPRB: This driver is used by Java programs stored directly in the database, such as Java Stored Procedures, Triggers, Database JSP's. IT Uses The Default / Current Database SESSION AND THUS Requires No Additional Database Username, Password or URL. How to configure Java to connect to the database via Oracle JDBC Drivers: 1. Install Sun JDK.

2. Modify the PATH environment variable to point to the bin directory of JDK

3. Set the CLASSPATH environment variable to point to the correct JDK's lib and Oracle JDBC interface.

Classpath = ".; ????"

3. Run "java -version" to verify the version of Java.

How to set the client according to the interface type on a different operating system:

For JDBC Thin interface:

Like the setting method under Windows with UNIX:

1. According to the JDK version, you only need to copy ClassSxx.zip to the specified directory, and do not need to install Oracle Client. After installed the database, the file will be in the $ ORACLE_HOME / JDBC / LIB directory. 2. Set ClassPath to enable it contains Classesxx.zip

3. Copy Oracle's other ZIP files and set ClassPath as needed

For JDBC OCI interfaces:

Fow Windows:

1. Install Oracle Client.

2. Depending on the JDK version, set the classpath to make it contain the correct classesxx.zip

3. Set ClassPath as needed to point to Oracle's other ZIP files

4. Set PATH to include $ oracle_home / bin directory

For UNIX:

1. Install Oracle Client.

2. Depending on the JDK version, set the classpath to make it contain the correct classesxx.zip

3. Set ClassPath as needed to point to Oracle's other ZIP files

4. Set ld_library_path so that it contains $ Oracle_Home / Lib Directory

Remarks:

Classesxx.zip is typically in the Oracle_Home / JDBC / LIB directory.

Explanation of files related to Oracle JDBC DRIVES drivers in Oracle_Home / JDBC / LIB directory:

- 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 Connection Database Syntax: 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 compared to JDBC Thin:

Same:

THE JDBC Thin, JDBC OCI, And JDBC Server Drivers All Provide The Same FunctionAry. The Same FunctionAlity.

* JDBC 2.0

* Partial JDBC 3.0 (in JDBC Driver Version 9.2)

* The Same Syntax and Apis

* The Same Oracle Extensions

As for the difference, it is a form, not uploaded, everyone summarizes it! !

Mainly the JDBC OCI interface is higher than JDBC Thin interface!

How does One Connect with the JDBC Thin Driver?

...........................

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 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 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 (); "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"); while (RSET.NEXT ()) System.out.println (RSET.GETSTRING (1)); //print col 1 stmt.close ();}}

Initialization parameters related to 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 SGA in 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 whiveded, 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..

If you put the Java program in the database and run the Java program stored in the database, Java virtual machines are enabled in the database, in order to ensure the effective run of the Java virtual machine, you need to set the parameters described above.

How to load a Java program to the database and release it out? ====================================

Just as above, the Java program or class can be stored in the database as a replacement or supplement of PL / SQL. Java can be used as a trigger, stored procedure, function, and object of the database. After the Java stored procedure is developed in accordance with the following procedure, you can call the Java stored procedure from SQL or PL / SQL, just like calling a normal PL / SQL process. The following code describes how to develop and use a Java program that outputs "Hello, World" in SQL * PLUS:

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 loadingjava 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 indeicating

Parameter-Passing Between Java and PL / SQL OR SQL.

(1) Write a Java program

--- You can create Java source files directly in SQL * Plus. Of course, if you have compiled Java Class, you can skip this step directly, directly to the Java program.

SQL> - First, Create The Java Source Code

SQL> Create or Replace Java Source Named "Hello" AS

Public class hello {

Static Public String Message (String Name) {Return "Hello," Name

}

}

/

Java created.

(2) Release Java program

SQL> - Now, Publish it to SQL

SQL> Create or Replace Function Hello (Name Varchar2) Return Varchar2

As Language Java Name

'Hello.Message (java.lang.string) returnjava.lang.string';

Function created.

(3) Using the published Java program

SQL> - Now, you can use the java procedure from a SQL Statement

SQL> SELECT HELLO ('World!') From Dual;

Hello ('World!')

---------------

Hello World!

--- Hello function does not support Chinese, 9i support in 8i. Such as:

SQL> SELECT HELLO ('Hello!') From Dual;

Hello ('Hello!')

------------------

Hello, hello!

As for other examples, you can see $ Oracle_home / JDBC / DEMO.ZIP file, which has an example of using JDBC OCI and JDBC Thin interfaces.

转载请注明原文地址:https://www.9cbs.com/read-60872.html

New Post(0)