我想用java连接到Oracle 8i数据库,其中数据库位于本地网络服务器10.3.1.1端口
我已按照示例教程从Oracle网站
建立连接https://docs.oracle.com/html/A67296_01/im_examp.htm#997347
我也遵循了这里指定的指令 https://people.cs.pitt.edu/~chang/156/06oracle/jdbc.html
您可能需要以下详细信息:
这是我的Java代码:
draft3
我收到此错误消息:
package com.oracle8i.testconnect;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
public Connection connection;
public DBConnect() {
// TODO Auto-generated constructor stub
}
public void connect() throws Exception {
String connectString;
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:thin:@10.3.1.1:1521:dbname";
System.out.println("Before DriverManager.getConnection");
try {
connection = DriverManager.getConnection(connectString, "username", "password");
System.out.println("Connection established");
connection.setAutoCommit(false);
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception inside connect(): " + e);
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DBConnect client = new DBConnect();
System.out.println("beginning");
try {
client.connect();
System.out.println("after Connected");
client.connection.close();
System.out.println("after close");
} catch (Exception e) {
// TODO: handle exception
try {
System.out.println("Exception : " + e);
client.connection.close();
e.printStackTrace();
} catch (Exception ex) {
// TODO: handle exception
System.out.println("Close Connection Exception : " + ex);
ex.printStackTrace();
}
}
}
}
我在互联网上寻找解决方案,但没有任何与我合作
这些是我的测试和结果:
我已经测试过更改dabase名称(sid)的名称是否错误,以检查它是否已连接,谢天谢地,似乎与oracle的连接几乎正确,因为它会响应此错误
beginning
Before DriverManager.getConnection
Exception inside connect(): java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 4
at oracle.jdbc.driver.T4C8TTIdty.<init>(T4C8TTIdty.java:491)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1434)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:711)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:558)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.oracle8i.testconnect.DBConnect.connect(DBConnect.java:19)
at com.oracle8i.testconnect.DBConnect.main(DBConnect.java:37)
after Connected
Exception : java.lang.NullPointerException
Close Connection Exception : java.lang.NullPointerException
java.lang.NullPointerException
at com.oracle8i.testconnect.DBConnect.main(DBConnect.java:48)
我已经与管理员核对了监听器状态,并且它正在运行(系统正在运行)
我已禁用防火墙
更新1:添加listener.ora内容
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:711)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:558)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.oracle8i.testconnect.DBConnect.connect(DBConnect.java:19)
at com.oracle8i.testconnect.DBConnect.main(DBConnect.java:37)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:272)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
... 8 more
答案 0 :(得分:0)
我在虚拟基础架构上安装了一个测试站点。
使用不同版本的jdbc驱动程序进行测试:
使用不同的connectString进行测试:
connectString ="jdbc:oracle:thin:@10.89.251.123:1521:ORCL";
connectString ="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.89.251.123)(PORT=1521))(CONNECT_DATA=(SID=ORCL)))";
connectString = "jdbc:oracle:oci:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.89.251.123)(PORT=1521))(CONNECT_DATA=(SID=ORCL)))";
connectString = "jdbc:oracle:oci:@ORCL8";
测试从Oracle Client 9.2.0.1连接到DB 8.1.7.0 SID = ORCL
C:\ oracle \ ora92 \ network&gt; sqlplus scott @ orcl8
SQL*Plus: Release 9.2.0.1.0 - Production on Tue Dec 26 13:44:11 2017
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Enter password:
Connected to:
Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production
With the Partitioning option
JServer Release 8.1.7.0.0 - Production
connected> select * from v$version;
----------------------------------------------------------------
Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production
PL/SQL Release 8.1.7.0.0 - Production
CORE 8.1.7.0.0 Production
TNS for 32-bit Windows: Version 8.1.7.0.0 - Production
NLSRTL Version 3.4.1.0.0 - Production
Elapsed: 00:00:00.00
connected>
Listener.ora
LISTENER.ORA网络配置文件:C:\ oracle \ ora81 \ network \ admin \ listener.ora 由Oracle配置工具生成。LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.89.251.123)(PORT = 1521))
)
)
(DESCRIPTION =
(PROTOCOL_STACK =
(PRESENTATION = GIOP)
(SESSION = RAW)
)
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.89.251.123)(PORT = 2481))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = C:\oracle\ora81)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = ORCL)
(ORACLE_HOME = C:\oracle\ora81)
(SID_NAME = ORCL)
)
)
Oracle Client 9.2中的TNSNAMES.ORA文件
### TNSNAMES.ORA Network Configuration File:
### Generated by Oracle configuration tools.
ORCL8 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.89.251.123)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ORCL)
)
)
这是您的Java代码:(瘦jdbc-&gt; oracle服务器)
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
public Connection connection;
public DBConnect() {
// TODO Auto-generated constructor stub
}
public void connect() throws Exception {
String connectString;
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:thin:@10.89.251.123:1521:ORCL";
System.out.println("Before DriverManager.getConnection");
try {
connection = DriverManager.getConnection(connectString, "scott", "tiger");
System.out.println("Connection established");
connection.setAutoCommit(false);
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception inside connect(): " + e);
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DBConnect client = new DBConnect();
System.out.println("beginning");
try {
client.connect();
System.out.println("after Connected");
client.connection.close();
System.out.println("after close");
} catch (Exception e) {
// TODO: handle exception
try {
System.out.println("Exception : " + e);
client.connection.close();
e.printStackTrace();
} catch (Exception ex) {
// TODO: handle exception
System.out.println("Close Connection Exception : " + ex);
ex.printStackTrace();
}
}
}
}
测试连接成功!!!
C:\Program Files\Java\jdk1.8.0_73\bin>javac.exe DBConnect.java
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Connection established
after Connected
after close
在Oracle Server上的文件listener.log文件中记录连接c:\ oracle \ ora81 \ network \ log \ listener.log
26-DEC-2017 14:03:53 * (CONNECT_DATA=(SID=ORCL)(CID=(PROGRAM=)(HOST=__jdbc__)(USER=))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.88.164.45)(PORT=54225)) * establish * ORCL * 0
将瘦驱动程序更改为oci(jdbc-&gt; oracle客户端库 - > oracle服务器)
public void connect() throws Exception {
String connectString;
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:oci:@ORCL8";
System.out.println("Before DriverManager.getConnection");
try {
connection = DriverManager.getConnection(connectString, "scott", "tiger");
System.out.println("Connection established");
connection.setAutoCommit(false);
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception inside connect(): " + e);
e.printStackTrace();
}
}
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Connection established
after Connected
after close
在Oracle Server c:\ oracle \ ora81 \ network \ log \ listener.log
上的文件Listener.log文件中记录连接26-DEC-2017 14:14:11 * (CONNECT_DATA=(SID=ORCL)(CID=(PROGRAM=C:\Program Files\Java\jdk1.8.0_73\bin\java.EXE)(HOST=IVC-PTK-DEMIN)(USER=DeminDV))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.88.164.45)(PORT=54424)) * establish * ORCL * 0
我继续使用不同版本的jdbc驱动程序进行测试。 http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
版本9.2.0.1,8.1.7.0,10.2.0.5已成功通过测试。 版本11.1.0.5,11.2.0.4,12.1.0.2,12.2.0.1未通过测试。
**http://download.oracle.com/otn/utilities_drivers/jdbc/10205/classes12.jar
classes12.jar (1,621,582 bytes) - for use with JDK 1.2 and JDK 1.3**
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Connection established
after Connected
after close
http://download.oracle.com/otn/utilities_drivers/jdbc/10205/ojdbc14.jar
ojdbc14.jar (1,569,316 bytes) - classes for use with JDK 1.4 and 1.5
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Connection established
after Connected
after close
http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
**http://download.oracle.com/otn/utilities_drivers/jdbc/111070/ojdbc6.jar
ojdbc6.jar (1,988,051 bytes) - Classes for use with JDK 1.6. It contains the JDBC**
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Exception inside connect(): java.lang.ArrayIndexOutOfBoundsException: 7
java.lang.ArrayIndexOutOfBoundsException: 7
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1611)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DBConnect.connect(DBConnect.java:20)
at DBConnect.main(DBConnect.java:38)
after Connected
Exception : java.lang.NullPointerException
Close Connection Exception : java.lang.NullPointerException
java.lang.NullPointerException
at DBConnect.main(DBConnect.java:49)
**http://download.oracle.com/otn/utilities_drivers/jdbc/11204/ojdbc6.jar
ojdbc6.jar (2,739,670 bytes) - (SHA1 Checksum: a483a046eee2f404d864a6ff5b09dc0e1be3fe6c)
Certified with JDK 8, JDK 7 and JDK 6: It contains the JDBC driver**
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Exception inside connect(): java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 4
at oracle.jdbc.driver.T4C8TTIdty.<init>(T4C8TTIdty.java:488)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1199)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:340)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:553)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:254)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DBConnect.connect(DBConnect.java:20)
at DBConnect.main(DBConnect.java:38)
after Connected
Exception : java.lang.NullPointerException
Close Connection Exception : java.lang.NullPointerException
java.lang.NullPointerException
at DBConnect.main(DBConnect.java:49)
**http://download.oracle.com/otn/utilities_drivers/jdbc/121020/ojdbc7.jar
ojdbc7.jar (3,397,734 bytes) - (SHA1 Checksum: a2348e4944956fac05235f7cd5d30bf872afb157)
Certified with JDK 7 and JDK 8; It contains the JDBC driver**
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Exception inside connect(): java.lang.ArrayIndexOutOfBoundsException: 7
java.lang.ArrayIndexOutOfBoundsException: 7
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1611)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DBConnect.connect(DBConnect.java:20)
at DBConnect.main(DBConnect.java:38)
after Connected
Exception : java.lang.NullPointerException
Close Connection Exception : java.lang.NullPointerException
java.lang.NullPointerException
at DBConnect.main(DBConnect.java:49)
**http://download.oracle.com/otn/utilities_drivers/jdbc/122010/ojdbc8.jar
ojdbc8.jar (4,036,257 bytes) - (SHA1 Checksum: 60f439fd01536508df32658d0a416c49ac6f07fb)
Certified with JDK 8; It contains the JDBC driver**
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Exception inside connect(): java.lang.ArrayIndexOutOfBoundsException: 7
java.lang.ArrayIndexOutOfBoundsException: 7
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1548)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:518)
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:688)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:39)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:691)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DBConnect.connect(DBConnect.java:20)
at DBConnect.main(DBConnect.java:38)
after Connected
Exception : java.lang.NullPointerException
Close Connection Exception : java.lang.NullPointerException
java.lang.NullPointerException
at DBConnect.main(DBConnect.java:49)