JDBC 的应用连接Oracle 遇到问题,错误如下:
ORA-12505,TNS:listener does not currently know of SID given in connect descriptor TheConnection descriptor used by the client was。
我在DB 层面配置了静态注册,并且GLOBAL_DBNAME和SID_NAME 不一样,以往的配置都是一样的,所以没有发现这个问题。
(SID_DESC =
(GLOBAL_DBNAME = dave)
(ORACLE_HOME =D:\app\Administrator\product\11.2.0\dbhome_1)
(SID_NAME = NEWCCS)
)
http://blog.csdn.net/tianlesoftware/article/details/5543166
在网上google 了一下,发现JDBC Thin Driver 的formats 有三种格式:
jdbc:oracle:thin:@//<host>:<port>/<service_name>
Example: jdbc:oracle:thin:@//192.168.2.1:1521/XE
注意这里的格式,@后面有//, 这是与使用SID的主要区别。
这种格式是Oracle 推荐的格式,因为对于集群来说,每个节点的SID 是不一样的,但是SERVICE_NAME 确可以包含所有节点。
jdbc:oracle:thin:@<host>:<port>:<SID>
Example: jdbc:oracle:thin:192.168.2.1:1521:X01A
Note: Support for SID is being phased out. Oracle recommends that users switch over to usingservice names.
jdbc:oracle:thin:@<TNSName>
Example: jdbc:oracle:thin:@GL
Note:
Support for TNSNames was added in the driver release 10.2.0.1
Oracle 是11gR2
Listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = dave)
(ORACLE_HOME =D:\app\Administrator\product\11.2.0\dbhome_1)
(SID_NAME = NEWCCS)
)
)
Tnsnames.ora
DVD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = dave)
)
)
将2.2 节的dbUrl 改成如下:
String dbUrl = "jdbc:oracle:thin:@//127.0.0.1:1521/dave";
输出结果:
MGMT_VIEW--97
ANQING--94
DVD--93
SYSMAN--95
如果在11g里遇到如下错误:
测试运行Java 类,报错:
java.sql.SQLException: The Network Adapter could not establish the connection
可以尝试更换对应的 jdbc connection driver,官网的说明如下:
JDBC Thin Driver 11g Causes"Java.Sql.Sqlexception: Io Exception: The Network Adapter Could NotEstablish The Connection" While Connecting to Oracle Database 11g [ID947653.1]
Change the JDBC connection driver class inyour application server from:
oracle.jdbc.driver.OracleDriver
to
oracle.jdbc.OracleDriver
String dbUrl = "jdbc:oracle:thin:@dvd";
报错如下:
java.sql.SQLException: Unknown host specified
该问题是因为JVM 没有oracle.net.tns_admin的system property。 解决方法有2种:
方法一:在启动VM 时添加如下参数:
-Doracle.net.tns_admin=D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN
方法二:在java 代码里添加:
System.setProperty("oracle.net.tns_admin","D:\\app\\Administrator\\product\\11.2.0\\dbhome_1\\NETWORK\\ADMIN");
添加之后,就可以正常在JDBC中使用tnsnama了。
-------------------------------------------------------------------------------------------------------
版权所有,文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!
Blog: http://blog.csdn.net/tianlesoftware
Weibo: http://weibo.com/tianlesoftware
Email: [email protected]
Skype: tianlesoftware
-------加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请----
DBA1 群:62697716(满); DBA2 群:62697977(满) DBA3 群:62697850(满)
DBA 超级群:63306533(满); DBA4 群:83829929(满) DBA5群: 142216823(满)
DBA6 群:158654907(满) DBA7 群:69087192(满) DBA8 群:172855474
DBA 超级群2:151508914 DBA9群:102954821 聊天 群:40132017(满)