最近好多网友询问 MyEclipse 连接 Oracle 10g 的连接问题,因为电脑没来得及装Oracle 10g,但是我装的是Oracle 10g Express,我想应该差不多吧。

核心提示:

MyEclipse Database Explorer 连接使用 Oracle 10g 或者 9i 中提供的 classes12.jar 即可。

普通Java开发使用 ojdbc14.jar 即可。

仅测试过 Oracle 10g Express,期待得到Oracle 10g正式版证实。

** 

初步试了一下,发现如下情况:

驱动程序使用的是 C:oraclexeapporacleproduct10.2.0serverjdbclibojdbc14.jar

有个官方的说明文件 C:oraclexeapporacleproduct10.2.0serverjdbcReadme.txt 里面提到支持 Java 5,以下是文档中的部分说明:

Oracle JDBC Drivers release 10.2.0.1.0 production (10g R2) README

What Is New In This Release ?
—————————–

Support for J2SE 5.0
    J2SE 5.0 (AKA J2SE 1.5 and Tiger) is fully supported. 5.0 supports
    JDBC 3.0, the same as J2SE 1.4, so there are no additional
    standard JDBC features. Prior to this release J2SE 5.0 was not
    officially supported.

These are a few simple things that you should do in your JDBC program:

  1. Import the necessary JDBC classes in your programs that use JDBC.
        For example:

      import java.sql.;
      import java.math.
; // if needed

    To use OracleDataSource, you need to do:
      import oracle.jdbc.pool.OracleDataSource;

  1. Create an OracleDataSource instance.

      OracleDataSource ods = new OracleDataSource();

  1. set the desired properties if you don’t want to use the
        default properties. Different connection URLs should be
        used for different JDBC drivers.

      ods.setUser(“my_user”);
      ods.setPassword(“my_password”);

    For the JDBC OCI Driver:
      To make a bequeath connection, set URL as:
      ods.setURL(“jdbc:oracle:oci:@”);

      To make a remote connection, set URL as:
      ods.setURL(“jdbc:oracle:oci:@");

      where is either a TNSEntryName       or a SQL*net name-value pair defined in tnsnames.ora.     For the JDBC Thin Driver, or Server-side Thin Driver:       ods.setURL("jdbc:oracle:thin:@");

      where is either a string of the form       //:/, or a SQL*net name-value pair,       or a TNSEntryName.

    For the JDBC Server-side Internal Driver:
      ods.setURL(“jdbc:oracle:kprb:”);

      Note that the trailing ‘:’ is necessary. When you use the
      Server-side Internal Driver, you always connect to the
      database you are executing in. You can also do this:

      Connection conn =
        new oracle.jdbc.OracleDriver().defaultConnection();

  1. Open a connection to the database with getConnection()
        methods defined in OracleDataSource class.

      Connection conn = ods.getConnection();

 

下面是我的简装版 Oracle 启动的服务:

image

为了保险,我用的是这个新版本的驱动,然后我在管理器中启用了HR这个账户(密码是hr):

image

然后使用一款名为 DbVisualizer 的工具访问数据,使用 JDK 1.4 和 JDK 1.6来连接都没有问题,唯独没测试 JDK 1.5.

image

image

可以看到数据,很好。

下面是MyEclipse中的测试,使用 Database Explorer 出了问题了:

image

然后我用 JDK 1.5 和 MyEclipse 自带的 JRE 编写了个Java程序,运行很正常,没发现上面的那个错误信息:

image

import java.sql.SQLException;
/**

  • 第一个 JDBC 的 HelloWorld 程序, 数据库访问 Mysql.
  • @author Administrator
  • @version 0.1 2007-09-26
    */
    public class JDBCHelloWorld {

    /**
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {
        // 1. 注册驱动

            new oracle.jdbc.OracleDriver();

        // 2. 获取数据库的连接
        java.sql.Connection conn = java.sql.DriverManager.getConnection(
                “jdbc:oracle:thin:@localhost:1521:xe”, “hr”, “hr”);
        // 3. 获取表达式
        java.sql.Statement stmt = conn.createStatement();
        // 4. 执行 SQL
        java.sql.ResultSet rs = stmt.executeQuery(“select * from jobs”);
        // 5. 显示结果集里面的数据
        while(rs.next()) {
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
 &#16

0;          System.out.println();
        }
        // 6. 释放资源
        rs.close();
        stmt.close();
        conn.close();

    }

}

 

输出:

AD_PRES
President

AD_VP
Administration Vice President

AD_ASST
Administration Assistant

FI_MGR
Finance Manager

FI_ACCOUNT
Accountant

AC_MGR
Accounting Manager

AC_ACCOUNT
Public Accountant

SA_MAN
Sales Manager

SA_REP
Sales Representative

PU_MAN
Purchasing Manager

PU_CLERK
Purchasing Clerk

ST_MAN
Stock Manager

ST_CLERK
Stock Clerk

SH_CLERK
Shipping Clerk

IT_PROG
Programmer

MK_MAN
Marketing Manager

MK_REP
Marketing Representative

HR_REP
Human Resources Representative

PR_REP
Public Relations Representative

把Eclipse换成 JDK 1.6 启动,start eclipseeclipse.exe -clean -vm C:Javajdk1.6.0_10binjavaw.exe

Database Explorer依然报错如故。

最后,Google 吧:cannot access nls data files JDBC

结果得到结果:原来换用classes12.jar就行了啊!看来还是没Google到位!

 

期待试过Oracle 10g正式版的同志,给个答案!

转载请注明:WebLogic Android 博客 » 讨论:JDBC连接Oracle 10g 出错的问题及其解决