Two methods of using JDBC to connect to Oracle

Source: Internet
Author: User
Tags connection pooling

1. using thin Connection

Pros:The thin driver is pure Java code and uses TCP/IP technology to connect to the Oracle database through the socket of Java, so the thin drive is platform independent and requires no Oracle client installation. You only need to download a thin-driven jar package, and add the thin-driven path to the Class_path variable in the environment variable.

defect: It is thin performance, not up to the enterprise-level requirements such as the OCI method. In addition, if an Oracle database corresponds to a host, you can use thin connection, if an Oracle database corresponds to four or five hosts (cluster Server), when using thin, The entire connection string for the associated database in the Tnsnames.ora file needs to be copied so that the Oracle cluster database can be connected.

A fixed notation:

Class.forName ("Oracle.jdbc.driver.OracleDriver");

Connection Conn =drivermanager.getconnection ("JDBC:ORACLE:THIN:@192.168.75.14:1521:DB1", "Test", "test");

2. Use of OCI Connection (Oracle call Interface)

Advantage: using OCI to connect to a database is an enterprise-wide approach that adapts to a single database and a clustered database, with superior performance, especially with connection pooling, which greatly improves application performance and concurrency.

Disadvantage: If you want to use OCI, you must install the Oracle client.

After installing the Oracle client, there is a JDBC folder in which the OCI driver and the thin driver are included. So it is not necessary to download the Internet. This drive in the Jdbc/lib folder, mainly have Classes12.jar, Nls_charset12.jar and so on. The driver package ending with 12 is adapted to the version above jdk1.1. The version ending with 11 is adapted to the jdk1.1 below. The jar package containing the classes in the file name is the driver, and the jar package with the NLS in the file name is an internationalization-related class.

Once the file is found, it is necessary to add the absolute path of the class and NLS jar packages to the CLASS_PATH environment variable, otherwise the ClassNotFound exception will be reported.

After setting the environment variable, you can directly use the OCI driver, do not be "driven" two words frightened, (drive is an executable file and a connection string). It's simply a matter of connecting strings. The standard connection string is as follows:

Class. forname ("Oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager. getconnection ("Jdbc:oracle:oci: @xxzc", "Duansiyuan", "Oracle_password");

As long as these two lines, you can guarantee to connect to the database, regardless of whether it is a single database or cluster database. Where XXZC is the database name, Duansiyuan is the user name, Oralce_password is the password. This is similar to the way in C #.

Issues to be aware of:

1, the Oracle_home/jdbc folder has a large number of boilerplate code and help documents, inside how to install and use JDBC have detailed instructions, please give high attention and patience to read. There is a lot of knowledge only through their own repeated practice is not available.

2, if you want high performance, please use the OCI connection, if you do not want to install Oracle client, use thin connection.

Two methods of using JDBC to connect to Oracle

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.