Instance code to explain various methods of Java connecting to Oracle database

Source: Internet
Author: User
Tags contains include connect odbc stmt version variable oracle database
oracle| Data | database Java interface with Oracle:
Running Java in a database can be said to be the most exciting new feature of Oracle8i. In the application you created to use the Oracle8i database, you can use new Java-related features to easily publish programs to the Internet or intranet.
  
   Methods for Using Java in ORACLE
  
As you all know, Java has become more popular in cross-platform development and Internet development, and oracle8i and later versions contain extended support for running Java in the database, and there are two ways to use it:
  
JDBC: Like ODBC, JDBC provides a driver interface that allows you to access a database in a Java program. Note: The JDBC driver is embedded in the virtual machine in the database.
  
SQLJ: Is a Java precompiled compiler that converts embedded SQL statements into Java statements. The use and operating mechanism of SQLJ is similar to that of other Oracle compilers (such as Pro*c,pro*cobol). In fact, in order to make our image remember the functions provided by SQLJ, we can also directly rename Sqlj to Pro*java.
  
The integration of Java into the database is bidirectional. That is, you can invoke SQL and Pl/sql in Java, or you can invoke Java in SQL and Pl/sql. Java programs can invoke SQL and pl/sql directly through the JDBC driver, and in turn, you can invoke Java directly in SQL and Pl/sql. In a database, the Java namespace is mapped directly into the database schema's namespace, which facilitates Java access and invocation. The database also provides extended DDL statements that allow you to create embedded Java programs in your data just as you would create a stored procedure.
  
   Features of ORACLE JDBC Drivers
  
There are three types of JDBC drivers in oracle8i that use the same syntax, APIs, and Oracle extensions to make Java code in robust clients, web-based Java applets, and Lightweight and flexible between Java stored procedures: Three types are 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 the client software on the machine running the Java program using this driver
2. JDBC Thin: This drive is typically used in Java programs that run in a Web browser. It communicates through the Java sockets instead of OCI or NET8, so you do not need to install client software on a client machine that uses JDBC thin.
3. JDBC KPRB: This drive is used by Java programs that are stored directly in the database, such as the Java Stored procedures, triggers, DB 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 a database via Oracle JDBC drivers: 1. Install the Sun JDK.
2. Modify the PATH environment variable so that it points to the JDK bin directory
3. Set the CLASSPATH environment variable so that it points to the correct JDK's lib and Oracle's JDBC interface.
CLASSPATH = ".;????"
3. Run "java–version" to verify the Java version.
  
How to set up a client based on the interface type on a different operating system:
For JDBC Thin interfaces:
This is the same as the Setup method under Windows and UNIX:
1. Depending on the version of the JDK, you only need to copy the Classesxx.zip to the specified directory without having to install Oracle Client. After the database is loaded, the file is in the $oracle_home/jdbc/lib directory. 2. Set the classpath to include the classesxx.zip above
3. Copy Oracle's other ZIP files and set Classpath as needed
  
   for JDBC OCI interfaces:
FOW Windows:
1. Install Oracle Client.
2. According to the JDK version, set the classpath to include the correct classesxx.zip
3. Set the classpath to point to other Oracle Zip files as needed
4. Set path so that it contains the $oracle_home\bin directory
  
   For Unix:
1. Install Oracle Client.
2. According to the JDK version, set the classpath to include the correct classesxx.zip
3. Set the classpath to point to other Oracle Zip files as needed
4. Set Ld_library_path to include the $oracle_home/lib directory
  
   Note:
Classesxx.zip is generally in the Oracle_home\jdbc\lib directory.
  
The files that are related to the Oracle JDBC drives drive in the Oracle_home\jdbc\lib directory Explanation:
-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 the NLS support in Object and Collection types.
  
-Classes12_g.zip
Same as Classes12.zip, except that classes were compiled with
"Javac-g".
  
   JDBC Connection syntax for database:
JDBC THIN:
  
  
Connection conn=
Drivermanager.getconnection
("Jdbc:oracle:thin: @dlsun511:1521:ora1", "Scott", "Tiger");
|   | |
Machine (ip@): port#: Sid
  
JDBC OCI:
  
Connection conn=
Drivermanager.getconnection
("jdbc:oracle:oci8[9]: @RAC", "Scott", "Tiger");
|
Net Service
  
JDBC Thin vs. JDBC Thin:
In the same place:
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
The main JDBC OCI interface is more efficient than the JDBC thin interface!
  
How do does one connect with the JDBC Thin Driver?
The "The JDBC thin driver provides" 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 ()
);
  
Connection conn = drivermanager.getconnection
("Jdbc:oracle:thin: @dbhost: 1521:ora1", "Scott", "Tiger");
@machine:p ort: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 do 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 do 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 don't need to specify a database URL, username or password as you are already connected to a Databas E session. Remember not to close the default connection. Closing The default connection might throw a 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 ();
  
Statement stmt = Conn.createstatement ();
ResultSet RSet = Stmt.executequery (
"Select BANNER from SYS. V_$version "
);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.