JDBC连接oracle地址出错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package com; import java.sql.*; import javax.sql.*; public class Test { public static void main(String[] args){ Connection conn = null ; Statement stmt = null ; ResultSet rs = null ; //1。注册驱动 try { Class.forName( "oracle.jdbc.driver.OracleDriver" ); //2。通过驱动管理器建立连接 String url = "jdbc:oracle:thin:@192.168.0.106:1521:dylanfire" ; String dbUsername = "scott" ; String dbPassword = "dylan" ; conn = DriverManager.getConnection(url, dbUsername, dbPassword); //3.建立连接对象 stmt = conn.createStatement(); //4、通过对象传递数据,并返回结果 String sql = "select ename, job, sal, hiredate from emp" ; rs = stmt.executeQuery(sql); while (rs.next()){ String ename = rs.getString( "ename" ); String job = rs.getString( "job" ); Double sal = rs.getDouble( "sal" ); java.sql.Date hiredate = rs.getDate( "hiredate" ); System.out.println(ename + ", " + job + ", " + sal + ", " + hiredate); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
oracle服务器的server-name是dylanfire 192.168.0.106是局域网安装oracle数据库的IP。 我通过telnet可以访问到oracle的数据库 不知道为什么通过JAVA就不可以 eclipse给出的异常为: java.sql.SQLException: The Network Adapter could not establish the connection at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:451) at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:535) at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:218) at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:29) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.Test.main(Test.java:21) Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:356) at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:419) at oracle.net.ns. NSProtocol .establishConnection( NSProtocol .java:811) at oracle.net.ns. NSProtocol .connect( NSProtocol .java:249) at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1407) at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:328) ... 7 more Caused by: java.net.ConnectException: Connection refused: connect at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:138) at oracle.net.nt.ConnOption.connect(ConnOption.java:120) at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:334) ... 12 more Exception in thread "main" java.lang.NullPointerException at com.Test.main(Test.java:44) 希望各位大侠帮我看看。有什么可能出现这种问题的情况 我一个一个的排查 地址url不对:具体ip的话去掉@,localhost前加@
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";// 127.0.0.1是本机地址,XE是精简版Oracle的默认数据库名
// String url = "jdbc:oracle:thin:192.168.0.167:1521:ORCL";// 127.0.0.1是本机地址,XE是精简版Oracle的默认数据库名 |