Oracle Jdbc Version Compatibility
Oracle Jdbc Version Compatibility
YuanyuanL 发表于3年前
Oracle Jdbc Version Compatibility
  • 发表于 3年前
  • 阅读 12
  • 收藏 0
  • 点赞 0
  • 评论 0




本文出自oracle 11.1

Version Compatibility for Oracle JDBC Drivers

This section discusses the general JDBC version compatibility issues.

Backward Compatibility

The JDBC drivers are certified to work with the currently supported versions of Oracle Database. For example, the JDBC Thin drivers in Oracle Database 11g Release 1 (11.1) are certified to work with the 10.2.x, 10.1.x, 9.2.x, and 9.0.1.x Oracle Database releases. However, they are not certified to work with older, unsupported database releases, such as 8.0.x and 7.x.

Forward Compatibility

Existing and supported JDBC drivers are certified to work with Oracle Database 11g Release 1 (11.1).


Determine the Version of the JDBC Driver

You can determine the version of the JDBC driver that you installed, by calling the getDriverVersion method of the OracleDatabaseMetaData class.

The following sample code shows how to determine the driver version:

import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;

class JDBCVersion
  public static void main (String args[]) throws SQLException
    OracleDataSource ods = new OracleDataSource();
    Connection conn = ods.getConnection();

    // Create Oracle DatabaseMetaData object
    DatabaseMetaData meta = conn.getMetaData();

    // gets driver info:
    System.out.println("JDBC driver version is " + meta.getDriverVersion());

You can also determine the version of the JDBC driver by executing the following commands:

  • java -jar ojdbc5.jar

  • java -jar ojdbc6.jar

Test JDBC and the Database Connection

The samples directory contains sample programs for a particular Oracle JDBC driver. One of the programs,, is designed to test JDBC and the database connection. The program queries for the user name, password, and the name of the database to which you want to connect. The program connects to the database, queries for the string "Hello World", and prints it to the screen.

Go to the samples directory, and compile and run the program. If the results of the query print without error, then your Java and JDBC installations are correct.

Although is a simple program, it demonstrates several important functions by performing the following:

  • Imports the necessary Java classes, including JDBC classes

  • Creates a DataSource instance

  • Connects to the database

  • Runs a simple query

  • Prints the query results to your screen

The program, which uses the JDBC OCI driver, is as follows:

 * This sample can be used to check the JDBC installation.
 * Just run it and provide the connect information. It will select
 * "Hello World" from the database.

// You need to import the java.sql and JDBC packages to use JDBC
import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;

// We import to be able to read from the command line

class JdbcCheckup
  public static void main(String args[]) throws SQLException, IOException

    // Prompt the user for connect information
    System.out.println("Please enter information to test connection to 
                          the database");
    String user;
    String password;
    String database;

    user = readEntry("user: ");
    int slash_index = user.indexOf('/');
    if (slash_index != -1)
      password = user.substring(slash_index + 1);
      user = user.substring(0, slash_index);
      password = readEntry("password: ");
    database = readEntry("database(a TNSNAME entry): ");

    System.out.print("Connecting to the database...");
    // Open an OracleDataSource and get a connection
    OracleDataSource ods = new OracleDataSource();
    ods.setURL("jdbc:oracle:oci:@" + database);
    Connection conn = ods.getConnection();

    // Create a statement
    Statement stmt = conn.createStatement();

    // Do the SQL "Hello World" thing
    ResultSet rset = stmt.executeQuery("select 'Hello World' from dual");

    while (
    // close the result set, the statement and the connection
    System.out.println("Your JDBC installation is correct.");

  // Utility function to read a line from standard input
  static String readEntry(String prompt)
      StringBuffer buffer = new StringBuffer();
      int c =;
      while (c != '\n' && c != -1)
        c =;
      return buffer.toString().trim();
    catch(IOException e)
      return "";

Opening a Connection to a Database

First, you must create an OracleDataSource instance. Then, open a connection to the database using theOracleDataSource.getConnection method. The properties of the retrieved connection are derived from theOracleDataSource instance. If you set the URL connection property, then all other properties, includingTNSEntryNameDatabaseNameServiceNameServerNamePortNumberNetwork Protocol, and driver type are ignored.

Specifying a Database URL, User Name, and Password

The following code sets the URL, user name, and password for a data source:

OracleDataSource ods = new OracleDataSource();

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

OracleDataSource ods = new OracleDataSource();
String url = "jdbc:oracle:thin:@//myhost:1521/orcl",//orcl is instance name
Connection conn = ods.getConnection();


The user name and password specified in the arguments override any user name and password specified in the URL.

Specifying a Database URL that Includes User Name and Password

The following example connects user scott with password tiger to a database host whose Transparent Network Substrate (TNS) entry is myTNSEntry, using the JDBC Oracle Call Interface (OCI) driver. In this case, the URL includes the user name and password and is the only input parameter.

String url = "jdbc:oracle:oci:scott/tiger@myTNSEntry");
Connection conn = ods.getConnection();

If you want to connect using the Thin driver, then you must specify the port number. For example, if you want to connect to the database on the host myhost that has a TCP/IP listener on port 1521 and the service identifier isorcl, then provide the following code:

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

See Also:

Chapter 8, "Data Sources and URLs"

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 148
博文 220
码字总数 186331
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
* 支付类型