gpt4 book ai didi

java - ORA-12505 :TNS listener does not currently know of SID given in connect descriptor

转载 作者:搜寻专家 更新时间:2023-10-31 08:10:52 24 4
gpt4 key购买 nike

我正在使用 Oracle 数据库。我用 Java 编写了一个小型 JDBC 连接程序,但我遇到了监听器问题。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcConnection {
public static void main(String[] args) throws SQLException,ClassNotFoundException {
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "system";
String password = "password";
Connection connection = null;

Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(url, user, password);
if(connection!=null){
System.out.println("Success in connnection");
} else {
System.out.println("failure in connection ");
}
}
}

我收到以下异常:

C:\Users\Administrator\Desktop>java JdbcConnection
Exception in thread "main" java.sql.SQLException: Listener refused the connectio
n 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:
localhost:1521:orcl

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:261)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:
441)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtensio
n.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JdbcConnection.main(JdbcConnection.java:18)

这是lsnrctl status的输出

LSNRCTL for 64-bit Windows: Version 12.1.0.1.0 - Production on 16-JUN-2015 13:43
:41

Copyright (c) 1991, 2013, Oracle. All rights reserved.

Welcome to LSNRCTL, type "help" for information.

LSNRCTL> status
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for 64-bit Windows: Version 12.1.0.1.0 - Produ
ction
Start Date 16-JUN-2015 12:02:52
Uptime 0 days 1 hr. 40 min. 52 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File C:\app\orauser\product\12.1.0\dbhome_1\network\admin\l
istener.ora
Listener Log File C:\app\orauser\diag\tnslsnr\hydwemvm\listener\alert\lo
g.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC1521ipc)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=hydwemvm)(PORT=1521)))
Services Summary...
Service "CLRExtProc" has 1 instance(s).
Instance "CLRExtProc", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

最佳答案

如果您知道您的 oracle 数据库 SID,则使用

jdbc:oracle:thin:@localhost:1521:orcl

如果您有服务名称,则在下面使用

jdbc:oracle:thin:@localhost:1521/orcl

此外,确保名称为 ORCL 的服务名称应该已启动并正在运行。如果还是不行,那你需要重启机器再试上面的方法。

还是不行?然后,尝试以下操作:

使用SYSTEM 用户登录并注册 LOCAL_LISTENER 通过在 SQL 下运行。

alter system set local_listener = '(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))' scope = both;
alter system register;

如何查看oracle SID和服务名称:

SELECT sys_context('USERENV', 'SID') FROM DUAL; -- It will return your oracle database SID

SELECT sys_context('USERENV', 'SERVICE_NAME') FROM DUAL; -- It will return your oracle database service name

关于java - ORA-12505 :TNS listener does not currently know of SID given in connect descriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30861061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com