最近好多网友询问 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:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar
有个官方的说明文件 C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\Readme.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 neededTo use OracleDataSource, you need to do:
import oracle.jdbc.pool.OracleDataSource;2. Create an OracleDataSource instance.
OracleDataSource ods = new OracleDataSource();
3. 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:@<database>");where <database> 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:@<database>");where <database> is either a string of the form
//<host>:<port>/<service_name>, 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();4. Open a connection to the database with getConnection()
methods defined in OracleDataSource class.Connection conn = ods.getConnection();
下面是我的简装版 Oracle 启动的服务:
为了保险,我用的是这个新版本的驱动,然后我在管理器中启用了HR这个账户(密码是hr):
然后使用一款名为 DbVisualizer 的工具访问数据,使用 JDK 1.4 和 JDK 1.6来连接都没有问题,唯独没测试 JDK 1.5.
可以看到数据,很好。
下面是MyEclipse中的测试,使用 Database Explorer 出了问题了:
然后我用 JDK 1.5 和 MyEclipse 自带的 JRE 编写了个Java程序,运行很正常,没发现上面的那个错误信息:
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));
System.out.println();
}
// 6. 释放资源
rs.close();
stmt.close();
conn.close();}
}
输出:
AD_PRES
PresidentAD_VP
Administration Vice PresidentAD_ASST
Administration AssistantFI_MGR
Finance ManagerFI_ACCOUNT
AccountantAC_MGR
Accounting ManagerAC_ACCOUNT
Public AccountantSA_MAN
Sales ManagerSA_REP
Sales RepresentativePU_MAN
Purchasing ManagerPU_CLERK
Purchasing ClerkST_MAN
Stock ManagerST_CLERK
Stock ClerkSH_CLERK
Shipping ClerkIT_PROG
ProgrammerMK_MAN
Marketing ManagerMK_REP
Marketing RepresentativeHR_REP
Human Resources RepresentativePR_REP
Public Relations Representative
把Eclipse换成 JDK 1.6 启动,start eclipse\eclipse.exe -clean -vm C:\Java\jdk1.6.0_10\bin\javaw.exe
Database Explorer依然报错如故。
最后,Google 吧:cannot access nls data files JDBC
结果在CSDN上得到结果:http://topic.csdn.net/t/20060923/20/5043490.html
问题已经解决。更换类文件为class12.jar
OK,原来换用classes12.jar就行了啊!看来还是没Google到位!