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

时间:2011-07-24 08:18:03

标签: java database oracle exception sqlexception

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

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&GT;从v $ thread中选择实例; (它返回) 实例 - &GT; XE

我可能做错了什么?

谢谢!

P.S。我也尝试过127.0.0.1而不是localhost

5 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:4)

而不是 String url =“jdbc:oracle:thin:@”+ serverName +“:”+ portNumber +“:”+ sid;

使用它:

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

答案 2 :(得分:2)

将xe替换为您在安装期间设置的数据库名称,您一定会获得成功

如果您忘记了dbname,可以从oracle目录中的文件tnsnames.ora中检索它

答案 3 :(得分:1)

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

答案 4 :(得分:0)

我猜测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]

(此错误与您的错误不同:我没有收到The Connection descriptor used by the client was:部分。我不是百分之百确定原因。)

在上面的情况中,修复程序以SYS身份连接到SQL * Plus并运行ALTER SYSTEM REGISTER。这将向侦听器注册实例:

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