Search code examples
javaoraclejdbcoracle-sqldeveloperdbaas

Not able to connect to database using Connection class


I really dont know what is wrong in the below code. I am able to connect from SQL developer but through JDBC it is giving exception.

package com.shruti.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;

public class ReadData {
    
    public static void main(String[] args) throws Exception {
        
        Class.forName("oracle.jdbc.driver.OracleDriver"); 
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@100.92.25.90:1521:PDB1.pvtsub1.xxxxx0011iad.oraclevcn.com","gotapps","WElcome***#11");
        
    }

}

same connection details I am mentioning in SQL DEveloper and it is working.

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
 
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:801)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:782)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:39)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:704)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.onkar.jdbc.ReadData.main(ReadData.java:10)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
 
    at oracle.net.ns.NSProtocolNIO.negotiateConnection(NSProtocolNIO.java:273)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:318)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1481)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:540)
    ... 6 more

Solution

  • The correct jbdc url is :

    "jdbc:oracle:thin:@100.92.25.90:1521/PDB1.pvtsub1.xxxxx0011iad.oraclevcn.com"

    you need to change the ":" between 1521 and PDB1 by "/"