java 连接远程oracle_Java远程访问Oracle教程

Table of content

This article provides examples of how to establish database connection with an Oracle database server – the first step to have Java applications working with one of the most popular database systems. Suppose you already had a version of Oracle database installed, such as Oracle Database 11g Express Edition.

1.    Download JDBC driver library for Oracle database

Click here to visit Oracle’s JDBC driver download page:

0818b9ca8b590ca3270a3433284dd417.png

Select the JDBC driver version that matches Oracle database engine installed on your computer. Here we select Oracle Database 11g Release 2 (11.2.0.3) which brings us to the jar download page:

0818b9ca8b590ca3270a3433284dd417.png

As you can see, there are several jar files which may cause confusion. However, the main jar file is ojdbc5.jar(for Java 5 or later) or ojdbc6.jar (for Java 6 or later). Here we select the version for Java 6. Remember to select Accept License Agreement at the top, and click on the link ojdbc6.jar to download.

NOTE:Oracle requires users to have an Oracle account for downloading, so you may have to register an account if you don’t have one.

Place the ojdbc6.jarfile under your project’s classpath as usual as using any jar file.

2.    JDBC database URL for Oracle database

The syntax of database URL for Oracle database is as follows:

jdbc:oracle::@

jdbc:oracle::/@

Where:

drivertype can be thin, oci or kprb.

database can be in the form of hostname:port:SID or a TNSNAMES entry listed in the file tnsnames.ora reside on the client computer. The default port is 1521.

Oracle categorizes their JDBC driver into four different types, as described in the following table:

Driver type

Usage

drivertype

Thin Driver

For client-side use without an Oracle installation

thin

OCI Driver

For client-side use with an Oracle installation

oci

Server-Side Thin Driver

Same as Thin Driver, but runs inside an Oracle server to access a remote server

thin

Server-Side Internal Driver

Runs inside the target server

kprb

According to Oracle, if your JDBC client and Oracle database server are running on the same machine, you should use the OCI Driver because it is much faster than the Thin Driver (The OCI Driver can use Inter Process Communication – IPC, whereas the Thin Driver can use only network connection).

For example, if you want to connect user tiger with password scott to an Oracle database with SID productDBthrough default port on host dbHost using the Thin Driver, you can construct the URL as follows:

If using the OCI Driver:

If you have a TNSNAMES entry productionDB in the tnsnames.ora file, you can construct the URL as follows:

For the Server-Side Thin Driver, use the same URL as the Thin Driver.

For the Server-Side Internal Driver, use the following URLs:

Because in that environment, the driver actually runs within a default session, and the client is always connected so the connection should never be closed.

3.    Register Oracle JDBC driver

The Oracle JDBC driver class name is oracle.jdbc.OracleDriver. You can register this driver as follows:

or:

NOTE:Since Java 6 (JDBC 4.0), registering the driver explicitly as above becomes optional. As long as we put theojdbc6.jar file in the classpath, JDBC driver manager can detect and load the driver automatically.

4.    Establish connection

With JDBC, we can establish a database connection by calling the method getConnection() of the DriverManager class. There are three versions of this method:

static Connection getConnection(String url)

static Connection getConnection(String url, Properties info)

static Connection getConnection(String url, String user, String password)

So we can have three ways for making a connection as follows:

Using only database URL for everything

In this method, we specify all connection properties in a single URL string, for example:

That uses the Thin Driver to connect the user tiger with password scott to the database SID productDB running on the same machine through the default port 1521.

Using database URL, username and password

In this method, we pass the username and password as additional arguments to the method getConnetion(), for example:

Using database URL and Properties object

In this method, we use a java.util.Properties object to hold username, password and other additional properties. For example:

In this example, we are using the OCI Driver with a TNSNAMES entry ProductDB, and specifying an additional propertydefaultRowPrefetch which is the number of rows to prefetch from the server.

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

5.    Example program

To demonstrate, we create a small example program below that establishes three different connections in 3 ways mentioned above, and finally close all the connections:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值