文档章节

Oracle Jdbc Version Compatibility

YuanyuanL
 YuanyuanL
发布于 2015/04/28 15:31
字数 978
阅读 16
收藏 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

粉丝 153
博文 319
码字总数 187682
作品 0
济南
部门经理
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
Ubuntu安装JDK7(Oracle JDK)

Ubuntu安装JDK7(Oracle JDK) Preparing your system First you need to remove openjdk for this run the following command from your terminal sudo apt-get purge openjdk* If you inst......

EntDIY
2014/04/23
0
0
ubuntu 12.04安装jdk

在安装之前,系统没有任何jdk软件,也就是说在终端执行 java -version将会输出如下内容: root@haiyang-Aspire:~# java -version程序 'java' 已包含在下列软件包中: default-jre gcj-4.6-jr...

happy圈圈
2014/04/27
0
0
ubuntu 12.04安装jdk

在安装之前,系统没有任何jdk软件,也就是说在终端执行 java -version 将会输出如下内容: root@haiyang -Aspire:~# java -version 程序 'java' 已包含在下列软件包中: * default-jre * gc...

import_key
2014/02/23
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

java并发备忘

不安全的“先检查后执行”,代码形式如下: if(条件满足){ //这里容易出现线程安全问题//doSomething}else{//doOther} 读取-修改-写入 原子操作:使用CAS技术,即首先从V中读取...

Funcy1122
今天
0
0
SpringBoot2.0 停机

最近新建了个SpringBoot2.0的项目,因为原来一直使用的是传统的Tomcat部署war包的形式,所以这次SpringBoot内置Tomcat部署jar包的时候遇到了很多问题。其中一个就是因为没有外置的Tomcat容器...

Canaan_
昨天
0
1
Confluence 6 外部参考

一个外部参考的意思是任何站点链接到你 Confluence 的实例。任何时候当 Confluence 的用户单击这个外部链接的时候,Confluence 可以记录这次单击为参考。 在默认的情况下,外部链接的参考链接...

honeymose
昨天
0
0
Android中的设计模式之抽象工厂模式

参考 《设计模式解析》 第十一章 Abstract Factory模式 《设计模式:可复用面向对象软件的基础 》3.1 Abstract Factory 抽象工厂 对象创建型模式 《Android源码设计模式解析与实战》第6章 创...

newtrek
昨天
0
0
Redis | 地理空间(GEO)的一个坑

Redis的地理空间(Geo)是个好东西,轻轻松松的就可以把地图描点的问题处理了, 最近却遇到一个坑...Redis采用的Msater-Slave模式, 运用GEORADIUS在salve读取对应的数据,新增了从节点但是从不返...

云迹
昨天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部