文档章节

Oracle Jdbc Version Compatibility

YuanyuanL
 YuanyuanL
发布于 2015/04/28 15:31
字数 978
阅读 21
收藏 0

oracle11.2官方文档:http://docs.oracle.com/cd/E11882_01/java.112/e16548/getsta.htm#JJDBC28049

oracle11.1官方文档: http://docs.oracle.com/cd/B28359_01/java.111/b31224/getsta.htm

本文出自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).

Note:

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();
    ods.setURL("jdbc:oracle:thin:scott/tiger@host:port:service");
    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,JdbcCheckup.java, 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 JdbcCheckup.java program. If the results of the query print without error, then your Java and JDBC installations are correct.

Although JdbcCheckup.java 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 JdbcCheckup.java 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 java.io to be able to read from the command line
import java.io.*;

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);
    }
    else
      password = readEntry("password: ");
    database = readEntry("database(a TNSNAME entry): ");

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

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

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

    while (rset.next())
      System.out.println(rset.getString(1));
    // close the result set, the statement and the connection
    rset.close();
    stmt.close();
    conn.close();
    System.out.println("Your JDBC installation is correct.");
  }

  // Utility function to read a line from standard input
  static String readEntry(String prompt)
  {
    try
    {
      StringBuffer buffer = new StringBuffer();
      System.out.print(prompt);
      System.out.flush();
      int c = System.in.read();
      while (c != '\n' && c != -1)
      {
        buffer.append((char)c);
        c = System.in.read();
      }
      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();
ods.setURL(url);
ods.setUser(user);
ods.setPassword(password);

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
ods.setURL(url);
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();

Note:

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");
ods.setURL(url);
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");
ods.setURL(URL);
Connection conn = ods.getConnection();

See Also:

Chapter 8, "Data Sources and URLs"


本文转载自:http://docs.oracle.com/cd/B28359_01/java.111/b31224/getsta.htm

共有 人打赏支持
YuanyuanL

YuanyuanL

粉丝 152
博文 322
码字总数 188376
作品 0
济南
部门经理
私信 提问
由 Eclipse 基金会接手的 Java EE 正在发生改变

Java EE 的新东家开始对 Java EE 的开发工作和支持进行更改。 自 Java EE 移交给 Eclipse 基金会以来,它的开发工作和管理方式正在开始改变。 一方面,Oracle 正在制作 Java EE 技术兼容性工...

局长
2017/10/09
5.1K
15
CentOS 6.5安装jdk1.8

By default, all Linux distributions comes with openjdk preinstalled. If you want to install and use Oracle Java JDK/JRE on your system, Just follow the steps below. In this tuto......

天下杰论
2014/06/18
0
0
Ubuntu 安装Oracle Java 7 (JDK)

Oracle Java (JVM/JDK)目前已经不再在Debian/Ubuntu软件源,Oracle已经停止JDL协议,只发布OpenJDK。当然,如果你确实需要Oracle Java,可以从官网下载。 下面是Ubuntu 安装Oracle Java JDK...

枫影Xda
2012/09/02
0
0
甲骨文回应Apache基金会:让Java向前走

之前Apache 基金会抗议 Oracle 的 Java 授权限制,现 Oracle 对此抗议发出回应。 甲骨文呼吁ASF三思,希望能与甲骨文及社群合,好推动Java继续前进。甲骨文并认为ASF此举只是让Java继续多年前...

红薯
2010/11/17
2.8K
31
Kotlin 1.0.3 发布,基于 JVM 的编程语言

Kotlin 1.0.3 发布了。Kotlin 是一个基于 JVM 的新的编程语言,由 JetBrains 开发。 更新内容: compiler: New option to specify the JDK against which the code is compiled Options to ......

oschina
2016/07/01
1K
5

没有更多内容

加载失败,请刷新页面

加载更多

myeclipse 启动到10分之一左右就挂了

删掉 {workspace}/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi

夜醒者
7分钟前
0
0
Hive on Spark 伪分布式环境搭建过程记录

进入hive cli是,会有如下提示: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) ......

PeakFang-BOK
14分钟前
0
0
用户输入和while 循环

# 用户输入和while循环# 7.1函数input() 的工作原理# 函数input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。message = inp...

吕湘颖
14分钟前
0
0
开发函数计算的正确姿势 —— 排查超时问题

写不尽的 code,查不完的 bug 通常我们写 bug,哦,不对,写代码时总不会一帆风顺,往往各种 bug 充斥其中,即使测试有较高的代码覆盖率往往也会有漏网之鱼。能写出一些比较隐蔽或者看起来像...

阿里云云栖社区
19分钟前
1
0
Python3新特性

一、类型注解 例子: def add(x:int, y:int) -> int: return x + y 解释: 类型`的形式指定函数的**参数类型**,用`-> 类型`的形式指定函数的**返回值类型 然后特别要强调的是,Pyt...

_Change_
33分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部