Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
192.168.1.1:1521:bbcd
Oracle
使用jdbc连接oracle xxxxxxx 数据库的时候出现了下列错误:
Connection refused(DESCRIPTION=(TMP=)(VSNNUM=153093120)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
而直接通过plsql可以正常连接数据库,或者可以通过sqlplus 连接数据库
经过debug和查找相关的资料发现问题原因如下:
jdbc连接数据库的时候,需要使用数据库的sid_name,而不是数据库的services_name
而使用plsql连接数据库的时候,只需要数据库的services_name即可,所以修改连接字符串中的services_name 为sid_name
附:
察看数据库中当前的sid:
SQL> select INSTANCE_NAME from v$instance;
INSTANCE_NAME
----------------
hasl
通过PL/SQL developer工具sqlplus ,用户名是aty,密码是xiu,url是11.12.22.142:1522/wfm能够正常连接oracle数据库。
但是通过以下JDBC代码不能连接
- public static Connection getOracleConnection() throws Exception
- {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- String url = "jdbc:oracle:thin:@11.12.22.142:1522:wfm";
- String user = "aty";
- String pwd = "xiu";
- Connection connection = DriverManager.getConnection(url, user, pwd);
- return connection;
- }
Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186647296)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
通过pl/sql developer,执行以下sql
- select * from v$instance;
- select * from v$database;
- select * from v$parameter where name like '%db_name%';
- select * from v$parameter where name = 'instance_name';
- select * from v$parameter where name = 'service_names';
发现数据库名称是wfm,但是实例名是wfm1。jdbc连接使用的是实例名,改成wfm1即可。
关于数据库名、实例名、服务名、域名等oracle中的概念,现在不是很理解,后面有时间再去了解。