今天在用jdbc连接oracle 12c数据库时遇到以下错误:
java.sql.SQLException: ORA-01017: invalid username/password; logon denied
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
连接的代码是这样的:
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@ localhost:1521/orcl","scott","tiger");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@ localhost:1521:orcl","scott","tiger");
or
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@ localhost:1521:pdborcl","scott","tiger");
博主从网上找了很久,终于知道,这个是oracle 12c的版本问题,以下给出解决方案:
1,首先进入到oracle主目录下:..\product\12.1.0\dbhome_1\NETWORK\ADMIN
找到tnsnames.ora,并打开,在其中加入:
PDBORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = PDBORCL)
)
)
2,修改以上的错误代码:
<span style="color:#009900;">Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@ localhost:1521/pdborcl","scott","tiger");</span>
这个是oracle 12c有别与其他版本的连接写法。
好了,测试下吧,应该能连上数据库了。
想深入了解oracle 12c jdbc的可以进入以下链接:
1,Oracle 12c jdbc connect string for PDB
参考资料:
https://community.oracle.com/message/11263323
https://community.oracle.com/thread/2566804?start=0&tstart=0