使用jdbc连接oracle数据库

时间:2014-09-17 09:11:55

标签: java oracle jdbc

我正在尝试一个程序来检查与oracle数据库的连接,该数据库位于

之下
import java.sql.*;  
class OracleCon{  
public static void main(String args[]){  
    try{  
        //step1 load the driver class  
        Class.forName("oracle.jdbc.driver.OracleDriver");  

        //step2 create  the connection object  
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","SangamOne123");  

        //step3 create the statement object  
        Statement stmt=con.createStatement();  

        //step4 execute query  
        ResultSet rs=stmt.executeQuery("select * from emp");  
        while(rs.next())  {
            System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
        }

         //step5 close the connection object  
         con.close();  

        } catch(Exception e){
            System.out.println(e);
        }
    }  
}  

但是在eclipse中运行它之后会显示以下异常。

  

java.sql.SQLException:监听器拒绝与之连接   以下错误:ORA-12505,TNS:监听器目前不知道   连接描述符中给出的SID

请帮助我,我是oracle数据库的新手。

1 个答案:

答案 0 :(得分:1)

问题在于oracle的SID (System Identifier),在安装oracle或创建数据库时会创建SID。

确保您的SIDxe并配置为要使用的侦听器。

还要在oracle安装中查看listener.ora文件。看起来应该如下:

 SID_LIST_LISTENER =
    (SID_LIST =
        (SID_DESC =
          (SID_NAME = default)
          (ORACLE_HOME = C:\app\oracle\product\11.2.0\dbhome_1)
          (PROGRAM = extproc)
          (ENVS = "EXTPROC_DLLS=ONLY:C:\app\oracle\product\11.2.0\dbhome_1\bin\oraclr11.dll")
        )

       (SID_DESC =
         (GLOBAL_DBNAME = ORCL)
         (ORACLE_HOME = C:\app\oracle\product\11.2.0\dbhome_1)
         (SID_NAME = orcl)
        )

       (SID_DESC =
         (GLOBAL_DBNAME = BMBK)
         (ORACLE_HOME = C:\app\oracle\product\11.2.0\dbhome_1)
         (SID_NAME = BMBK)
        )

     )

    LISTENER =
      (DESCRIPTION_LIST =
        (DESCRIPTION =
          (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
          (ADDRESS = (PROTOCOL = TCP)(HOST = 123.456.789.101)(PORT = 1521))
          (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
        )
      )

请注意SID_NAME = default的第三行,对于您的监听者,SID应为xe或使用此处指定的内容。

您的系统中可能还有多个SID。