Java Oracle localhost 连接错误 (ORA-12505)

发布于 2024-11-26 03:18:40 字数 2287 浏览 2 评论 0原文

我当前正在尝试连接到当前计算机上的数据库。

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

public class Main {
    public static void main(String[] argv) throws Exception {

        Connection connection = null;
        try {
            // Load the JDBC driver
            String driverName = "oracle.jdbc.driver.OracleDriver";
            Class.forName(driverName);

            // Create a connection to the database
            String serverName = "localhost";
            String portNumber = "1521";
            String sid = "xe";
            String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
            String username = "scott";
            String password = "tiger";
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("Success");
        } catch (ClassNotFoundException e) {
            System.out.println("Class Not Found Error");
        } 
    }
}

我不断收到此错误,我不知道为什么......

Exception in thread "main" 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
The Connection descriptor used by the client was:
localhost:1521:xe

at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:171)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:496)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:411)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:490)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:202)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:465)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:21)

在我的服务器中我使用了该命令(以 sys 身份登录) SQL>从 v$thread 选择实例; (它返回) 实例--> xe

我可能做错了什么?

谢谢!

PS我也尝试过 127.0.0.1 而不是 localhost

I am trying to currently connect to a database on my current computer.

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

public class Main {
    public static void main(String[] argv) throws Exception {

        Connection connection = null;
        try {
            // Load the JDBC driver
            String driverName = "oracle.jdbc.driver.OracleDriver";
            Class.forName(driverName);

            // Create a connection to the database
            String serverName = "localhost";
            String portNumber = "1521";
            String sid = "xe";
            String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
            String username = "scott";
            String password = "tiger";
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("Success");
        } catch (ClassNotFoundException e) {
            System.out.println("Class Not Found Error");
        } 
    }
}

I keep getting this error and I do not know why...

Exception in thread "main" 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
The Connection descriptor used by the client was:
localhost:1521:xe

at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:171)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:496)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:411)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:490)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:202)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:465)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:21)

Within my server I have used the command (logged on as sys)
SQL> select instance from v$thread;
(it returns)
Instance--> xe

What could I be doing incorrect?

Thanks!

P.S. I have also tried 127.0.0.1 instead of localhost

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多孤肩上扛 2024-12-03 03:18:40

检查\admin\NETWORK目录下的listener.ora文件是否有以下值:

XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )

Check if listener.ora file under the <ORACLE_HOME>\admin\NETWORK directory has the following value:

XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )
橘虞初梦 2024-12-03 03:18:40

而不是
String url = "jdbc:oracle:thin:@" + 服务器名 + ":" + 端口号 + ":" + sid;

使用这个:

String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + "/" + sid;

Instead of
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;

use this:

String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + "/" + sid;

我爱人 2024-12-03 03:18:40

将 xe 替换为安装时设置的数据库名称,

如果您忘记了数据库名称,您一定会成功,可以从 oracle 目录中的文件 tnsnames.ora 中检索它

replace xe with database name which you have set during installation, you will surely get success

if you forgot the dbname it could be retreived from file tnsnames.ora in your oracle directory

浮光之海 2024-12-03 03:18:40

我在连接到 oracle rac 时遇到了同样的问题。我将网址从 port:servicename 更改为 port/servicename 并且它对我有用。

I faced same problem while connecting to oracle rac. I changed the url from port:servicename to port/servicename and it worked for me.

雨后彩虹 2024-12-03 03:18:40

我猜测 TNS 侦听器已经启动,但数据库实例在侦听器启动之前启动。

当数据库实例启动时,它将向 TNS 侦听器注册自身。但是,如果没有要注册的侦听器,则无法执行此操作。当侦听器启动时,它不会检查它所知道的实例是否已启动。

我可以提供一个演示。我在 Windows 7 上使用 Oracle 11g XE Beta。最初,OracleServiceXE 服务正在运行,但 OracleXETNSListener 服务未运行。

我运行了您的数据库连接代码,出现以下错误:

线程“main”java.sql.SQLRecoverableException中出现异常:IO错误:网络适配器无法建立连接

如果您收到 ORA-12505 错误,则显然您的 TNS 侦听器正在运行。

然后我启动了 TNS 侦听器并重新运行数据库连接代码。这次我得到了以下输出:(我已经重命名了您的类并更改了其中的用户名和密码,但除此之外,其中的代码是相同的):(

C:\Users\Luke\stuff>java DbConnTest
Exception in thread "main" 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
[stacktrace snipped]

此错误与您的错误不同:我没有没有得到 客户端使用的连接描述符是: 部分,我不是 100% 确定原因。)

在上面的情况下,修复方法是连接到 SQL*Plus 作为 >SYS 并运行 ALTER系统寄存器。这会向侦听器注册实例:

C:\Users\Luke\stuff>sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Beta on Sun Jul 24 11:13:57 2011

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Beta

SQL> alter system register;

System altered.

SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Beta

执行此操作后,我能够连接到数据库:

C:\Users\Luke\stuff>java DbConnTest
Success

I'm going to guess that the TNS listener has started, but the database instance started up before the listener did.

When the database instance starts up, it will register itself with the TNS listener. However, if there's no listener to register with, it can't do this. When the listener starts up, it doesn't check to see whether the instances it knows about have started up.

I can provide a demonstration. I'm using Oracle 11g XE Beta on Windows 7. Initially, the OracleServiceXE service is running but OracleXETNSListener service is not.

I ran your database connection code and I got the following error:

Exception in thread "main" java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

If you're getting a ORA-12505 error, then clearly your TNS listener is running.

I then started the TNS listener and re-ran your database connection code. I got the following output this time: (I've renamed your class and changed the username and password within it, but other than that, the code within it is the same):

C:\Users\Luke\stuff>java DbConnTest
Exception in thread "main" 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
[stacktrace snipped]

(This error isn't identical to yours: I didn't get a The Connection descriptor used by the client was: section in it. I'm not 100% sure why.)

In the case above, the fix is connect to SQL*Plus as SYS and run ALTER SYSTEM REGISTER. This registers the instance with the listener:

C:\Users\Luke\stuff>sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Beta on Sun Jul 24 11:13:57 2011

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Beta

SQL> alter system register;

System altered.

SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Beta

After doing this, I was able to connect to the database:

C:\Users\Luke\stuff>java DbConnTest
Success
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文