ORA-12505,TNS:监听器当前不知道连接描述符中给出的SID

时间:2012-04-15 22:55:26

标签: database oracle10g tns

我对数据库主题非常新,并尝试从Netbeans连接做一些初学者的事情。但是,当我测试我的连接时,我面临一些错误(我搜索了这个错误,但无法理解任何事情)。我为Oracle 10g Express Database Edition安装了ojdbc6.jar并下载了Oracle Thin。这是我的简单代码,仅用于测试驱动程序版本;

import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;

    class JDBCVersion
    {
      public static void main (String args[]) throws SQLException
      {
        OracleDataSource ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:@localhost:1521:XE [hr on HR]");
        Connection conn = ods.getConnection();

        // Create Oracle DatabaseMetaData object
        DatabaseMetaData meta = conn.getMetaData();

        // gets driver info:
        System.out.println("JDBC driver version is " + meta.getDriverVersion());
      }
    }

我得到了

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

正如我所说我是这个数据库的新手,如果你能帮忙解释我会很高兴。无论如何,谢谢

1 个答案:

答案 0 :(得分:4)

以下是有关如何连接Oracle数据库的代码:

public class JDBCVersion {

  public static void main(String[] args) {
    try {
        Class.forName("oracle.jdbc.OracleDriver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return;
    }
    Connection connection = null;
    try {
        connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:XE", "username",
                "password");
    } catch (SQLException e) {
        e.printStackTrace();
        return;

    }
}

必须相应更改用户名和密码。此外,如果您安装了Oracle 10g,那么正确的版本将是ojdbc14.jar。

相关问题