1

Possible Duplicate:
Java Oracle localhost connection error (ORA-12505)

When I try to connect to Oracle 10g Express Edition, it gives me error:

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

My sample code:

    Class.forName("oracle.jdbc.OracleDriver");
    Connection connection = null;
    connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "mk119", "mk119");
    Statement stmt = connection.createStatement();

In database:

SQL> connect mk119
Enter password:
Connected.
SQL> ed
Wrote file afiedt.buf

  1* select sys_context('userenv', 'instance_name') from dual
SQL> /

SYS_CONTEXT('USERENV','INSTANCE_NAME')
----------------------------------------------------------------------

xe

SQL>

Please help me on this.

EDIT: Stacktrace:

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.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 oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
        at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
        at java.sql.DriverManager.getConnection(DriverManager.java:579)
        at java.sql.DriverManager.getConnection(DriverManager.java:221)
        at fingerprint.DatabaseTest.main(DatabaseTest.java:14)

EDIT: Complete Code:

import java.sql.*;

public class DatabaseTest {
    public static void main(String [] args){
        try{
            Class.forName("oracle.jdbc.OracleDriver");
            Connection connection = null;
            connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "mk119", "mk119");
            Statement stmt = connection.createStatement();
            System.out.println("Connection created");
            stmt.close();
            connection.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
Community
  • 1
  • 1
nullptr
  • 3,320
  • 7
  • 35
  • 68
  • 1
    check this out: http://stackoverflow.com/questions/6805705/java-oracle-localhost-connection-error-ora-12505; make sure the connection url is ok (port, sid etc) – acostache Dec 28 '12 at 19:30
  • can you run, from a dos prompt, `tnsping xe`. if so , whats the output? – DazzaL Dec 28 '12 at 20:15

1 Answers1

3

As per the answer provided here, check the following (answer is the one from the related, maybe duplicate, question):

"Check if listener.ora file under the \admin\NETWORK directory has the following value:

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

"

Therefore check your connection url: host, port, sid, to make sure it is properly set.

Community
  • 1
  • 1
acostache
  • 2,177
  • 7
  • 23
  • 48
  • Yes I checked. listener.ora was not having that information. I added that and restarted database, but still it is not working. – nullptr Dec 28 '12 at 19:39
  • Are you getting any new errors? – acostache Dec 28 '12 at 19:40
  • no, same error I am getting – nullptr Dec 28 '12 at 19:42
  • Try to check the tnsnames.ora file (probably located under $ORACLE_HOME/network/admin) and check the similar structure (add info if required). – acostache Dec 28 '12 at 19:47
  • Its there in tnsnames.ora, but still getting same erro – nullptr Dec 28 '12 at 19:53
  • Could you please edit your description/question and paste some more of the stacktrace, if possible all of it? – acostache Dec 28 '12 at 19:54
  • edited question for complete code and stack trace – nullptr Dec 28 '12 at 20:01
  • Can you try something like Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/orcl", "scott", "tiger"); and then jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCPS)(HOST=)(PORT=))(CONNECT_DATA=(SERVICE_NAME=))) (see http://www.orafaq.com/wiki/JDBC) - idea from http://stackoverflow.com/questions/11911459/db-connection-in-oracle) – acostache Dec 28 '12 at 20:01
  • Same error: java.sql.SQLException: Listener refused the connection with the following error: ORA-12514, TNS:listener does not currently know of service requested in connect descriptor The Connection descriptor used by the client was: (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xe))) – nullptr Dec 28 '12 at 20:07
  • Not the same error; the error code is different ORA-12514. Can you connect successfuly through sql developer or some other similar tool? – acostache Dec 28 '12 at 20:15
  • It got worked after restarting whole system. I don't know what problem may be. May be listener got messed. Thanks for you help. – nullptr Dec 29 '12 at 09:06