1
votes

I am trying one program for checking the connection with oracle database which is below

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);
        }
    }  
}  

But after running it in eclipse it is showing following exception.

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

Please help me i am new to oracle database .

1

1 Answers

1
votes

The problem is regarding SID (System Identifier) of oracle, SIDs are created when you install oracle or when you create a database.

Make sure your SID is xe and configured for the listener to be used.

Also check out you listener.ora file in oracle installation. It should look somewhat like following:

 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))
        )
      )

Note the third line where SID_NAME = default, for you listener the SID should be xe or use whatever is specified here.

You might also have multiple SIDs in your system.