ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

quartaela picture quartaela · Apr 16, 2012 · Viewed 19.4k times · Source

I'm very new on Database subjects and trying to do some beginner stuff with connecting from Netbeans. but when i test my connection i'm facing with some error (i searched this error but couldnt understand anything). i installed Oracle 10g Express Database Edition and downloaded ojdbc6.jar for Oracle Thin. this my simple code just for test the driver version;

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

and i get

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

as i said i am newbie about this database stuff and i will be very glad if you can help with explaining. thanks anyway

Answer

Ravi Kadaboina picture Ravi Kadaboina · Apr 16, 2012

Here is the code on how to connect to your Oracle Database:

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;

    }
}

Where username and password have to be changed accordingly. Also if you have installed Oracle 10g then the right version would be ojdbc14.jar.