oracle.jdbc Class OracleDriver说说吧

oracle.jdbc
Class OracleDriver

java.lang.Object
  extended byoracle.jdbc.driver.OracleDriver
      extended byoracle.jdbc.OracleDriver

  • All Implemented Interfaces:

  • java.sql.Driver


  • public class OracleDriver
  • extends oracle.jdbc.driver.OracleDriver

The Oracle JDBC driver class that implements the java.sql.Driver interface.

Register the JDBC drivers

To access a database from a Java application, you must first provide the code to register your installed driver with your program. You do this with the static registerDriver() method of the java.sql.DriverManager class. This class provides a basic service for managing a set of JDBC drivers. The registerDriver()method takes as input a "driver" class, that is, a class that implements the java.sql.Driver interface, as is the case with OracleDriver.

Note: Alternatively, you can use the forName() method of the java.lang.Class class to load the JDBC drivers directly. For example: Class.forName ("oracle.jdbc.OracleDriver");. However, this method is valid only for JDK-compliant Java virtual machines. It is not valid for Microsoft Java virtual machines.

You register the driver only once in your Java application.

  DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

Open a Connection to a database

Once you have registered the driver, you can open a connection to the database with the static getConnection() method of the java.sql.DriverManager class. The type of the object returned is java.sql.Connection.

Understanding the Forms of getConnection()

Specifying a Databse URL, User Name, and Password

The following signature takes the URL, user name, and password as separate parameters:

getConnection(String URL, String user, String password);

Where the URL is of the form:
  jdbc:oracle:<drivertype>:@<database>

The following example connects user scott with password tiger to a database with SID orcl through port 1521 of host myhost, using the Thin driver.

Connection conn = DriverManager.getConnection
  ("jdbc:oracle:thin:@myhost :1521:orcl", "scott", "tiger");

Specifying a Databse URL That Includes User Name and Password

The following signature takes the URL, user name, and password all as part of a URL parameter:

getConnection(String URL);

Where the URL is of the form:
  jdbc:oracle:<drivertype>:<user>/<password>@<database>

The following example connects user scott with password tiger to a database on host myhost using the OCI driver. In this case, however, the URL includes the userid and password, and is the only input parameter.

Connection conn = DriverManager.getConnection
   ("jdbc:oracle:oci8:scott/tiger@myhost);

If you want to connect with the Thin driver, you must specify the port number and SID. For example, if you want to connect to the database on host myhostthat has a TCP/IP listener up on port 1521, and the SID (system identifier) is orcl:

Connection conn = DriverManager.getConnection
   ("jdbc:oracle:thin:scott/tiger@myhost :1521:orcl);

Specifying a Database URL and Properties Object

The following signature takes a URL, together with a properties object that specifies user name and password (perhaps among other things):

getConnection(String URL, Properties info);

Where the URL is of the form:
  jdbc:oracle:<drivertype>:@<database>

In addition to the URL, use an object of the standard Java Properties class as input. For example:

java.util.Properties info = new java.util.Properties();
  info.put ("user", "scott");
  info.put (
"password","tiger");
  info.put ("defaultRowPrefetch","15");
  getConnection ("jdbc:oracle:oci8:@",info);

The table below lists the connection properties that Oracle JDBC drivers support.

Connection Properties Recognized by Oracle JDBC Drivers



Select your driver type : thin, oci, kprb...

Oralce provides four types of JDBC driver.

  • Thin Driver, a 100% Java driver for client-side use without an Oracle installation, particularly with applets. The Thin driver type is thin. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the Thin driver, you would write :

      Connection conn = DriverManager.getConnection
      ("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");
  • OCI Driver for client-side use with an Oracle client installation. The OCI driver type is oci. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the OCI driver, you would write :

      Connection conn = DriverManager.getConnection
      ("jdbc:oracle:oci:@myhost:1521:orcl", "scott", "tiger");

    Note that you can also specify the database by a TNSNAMES entry. You can find the available TNSNAMES entries listed in the file tnsnames.ora on the client computer from which you are connecting. For example, if you want to connect to the database on host myhost as user scott with password tiger that has aTNSNAMES entry of MyHostString, enter:

      Connection conn = DriverManager.getConnection
      ("jdbc:oracle:oci8:@MyHostString","scott","tiger");

    If your JDBC client and Oracle server are running on the same machine, the OCI driver can use IPC (InterProcess Communication) to connect to the database instead of a network connection. An IPC connection is much faster than a network connection.

      Connection conn = DriverManager.getConnection
      ("jdbc:oracle:oci8:@","scott","tiger");
  • Server-Side Thin Driver, which is functionally the same as the client-side Thin driver, but is for code that runs inside an Oracle server and needs to access a remote server, including middle-tier scenarios. The Server-Side Thin driver type is thin and there is no difference in your code between using the Thin driver from a client application or from inside a server.

  • Server-Side Internal Driver for code that runs inside the target server, that is, inside the Oracle server that it must access. The Server-Side Internal driver type is kprb and it actually runs within a default session. You are already "connected". Therefore the connection should never be closed.
    To access the default connection, write:

      DriverManager.getConnection("jdbc:oracle:kprb:");
      or:  DriverManager.getConnection("jdbc:default:connection:");

    You can also use the Oracle-specific defaultConnection() method of the OracleDriver class which is generally recommended:

      OracleDriver ora = new OracleDriver();
      Connection conn = ora.defaultConnection();

    Note: You are no longer required to register the OracleDriver class for connecting with the Server-Side Internal driver, although there is no harm in doing so. This is true whether you are using getConnection() or defaultConnection() to make the connection.
    Any user name or password you include in the URL string is ignored in connecting to the server default connection. The DriverManager.getConnection() method returns a new Java Connection object every time you call it. Note that although the method is not creating a new physical connection (only a single implicit connection is used), it is returning a new object.
    Again, when JDBC code is running inside the target server, the connection is an implicit data channel, not an explicit connection instance as from a client. It should never be closed.


转载于:https://my.oschina.net/u/2308739/blog/628532

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值