我似乎能够使用运行SQL命令行连接到(远程)oracle数据库,但不能使用JDBC。 因此,假设服务器名称为 srv_oracle_prod ,数据库名称为 bdcompany.company 。在SQL命令行中,我可以使用:
进行连接connect user/password@srv_oracle_prod:1521/bdcompany.company
但是当我尝试
时public class TestRealDatabase {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:srv_oracle_prod:1521/bdcompany.company",
"user",
"password");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我在eclipse中遇到这个错误:
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:419)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:536)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:228)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at theOpenTutorials.TestRealDatabase.main(TestRealDatabase.java:22)
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.NSProtocol.connect(NSProtocol.java:386)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1054)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:308)
... 7 more
我搜索了很多但找不到任何解决方案,而且我是使用Oracle的noobie。
这是我的 tnsnames.ora 文件,如下所示:
XE =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = PC-HP-08.COMPANY.local)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = XE)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = CLRExtProc)
(PRESENTATION = RO)
)
)
答案 0 :(得分:2)
我的JDBC瘦URL通常如下所示:
jdbc:oracle:thin:@SERVER:PORT:SID
在我看来,您正在尝试使用TNS名称作为服务器名称。但是Oracle瘦驱动程序并没有使用TNS(据我所知)。
所以我的猜测是你的JDBC URL应该是这样的:
jdbc:oracle:thin:@PC-HP-08.COMPANY.local:1521:PLSExtProc
答案 1 :(得分:1)
查看您的网址:
jdbc:oracle:thin:srv_oracle_prod:1521/bdcompany.company
您获得的错误是因为 SID 部分。这是:bdcompany.company
。因此,请联系您的DBA并询问她的Oracle实例的 SID 是什么。
如果您使用 tns名称,那么如果您在问题中发布 tnsnames.ora 文件内容,它可能会对我们有所帮助。
答案 2 :(得分:0)
由于“XE”是服务名称而不是SID,因此您应该使用此连接字符串:
jdbc:oracle:thin@//PC-HP-08.COMPANY.local:1521/XE
也许这个主题可以帮到你:Java JDBC - How to connect to Oracle using Service Name instead of SID
答案 3 :(得分:0)
使用服务名称进行连接:
的DriverManager.getConnection(&#34; JDBC:预言:瘦:@ // {HOST_NAME}:{PORT} / {SERVICE_NAME}&#34;&#34; {USERNAME}&#34;&# 34; {PASSWORD}&#34);
HOST_NAME,PORT&amp;可以在&#34; tnsnames.ora&#34;中找到SERVICE_NAME。文件位于&#34; ORACLE HOME \ NETWORK \ ADMIN目录&#34;