1
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:orcl

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:261)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)  
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)  
at oraenter code herecle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)  
at java.sql.DriverManager.getConnection(DriverManager.java:582)  
at java.sql.DriverManager.getConnection(DriverManager.java:185)  
at com.test.Date.main(Date.java:13)

This is my code

Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger");  

I veried tnsnames.ora file also

ORCL =
(DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = orcl)
    )
)

I added ojdbc14.jar in my classpath and i am using oracle10g

Could you please help me on this.

1
  • Can do you a tnsping from command prompt tnsping orcl and see what you are getting? Commented Aug 11, 2012 at 6:55

1 Answer 1

1

Like mentioned check whether you are able to do a tnsping like tnsping orcl and if that works fine try to connect to your database from sqlplus or any tool like TOAD or SQLDeveloper. Post the errors if you are getting any, else use the following to connect to your database and see how it works.

    try {
                Class.forName("oracle.jdbc.OracleDriver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return;
            }
            Connection con = null;
            try {
                con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger");
                   } catch (SQLException ex) {            
            ex.printStackTrace();            
        } finally {
            close(connection);
        }
return 

Update 1

Can you try with the following code

jdbc:oracle:thin:@//localhost:1521:orcl","system","tiger

If that doesn't work try this as well

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCPS)(HOST=<host>)(PORT=<port>))(CONNECT_DATA=(SERVICE_NAME=<service>)))

More information here

Regards

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.