1
votes

I want to connect my java file to amazon oracle database.

DataSource.java

public class DataSource {

private static String dbName = System.getenv("PUBLIC");
private static String userName = System.getenv("root");
private static String password = System.getenv("3deemala7sas");
private static String hostname = System.getenv("oracleinstance.ctnrs4kazdmm.ap-south-1.rds.amazonaws.com");
private static String port = System.getenv("1521");

public static final String JDBC_DRIVER = "oracle.jdbc.OracleDriver";
public static final String DB_URL = "jdbc:oracle:thin://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password;

public static boolean checkDomain()
{
    if (System.getenv("oracleinstance.ctnrs4kazdmm.ap-south-1.rds.amazonaws.com") != null) {
        return true;
    }
    return false;
}

DbConnection.java

public static Connection getConnection( )
{


    try {
        Class.forName(DataSource.JDBC_DRIVER);
         connection = DriverManager.getConnection(DataSource.DB_URL);
        //connection = (Connection) DriverManager.getConnection(DataSource.DB_URL);

        System.out.println("Connected ....");

        return connection;
    }
     catch (SQLException ex) {
        System.out.println("Error syntax");
         System.out.println(ex.toString());
         ex.printStackTrace();
    }catch (ClassNotFoundException ex) {
        System.out.println("Class not found error ...");
    }


    return null;
}

App01.java

public class App01 {



public static void main(String[] args) throws SQLException {
    Connection connection = null ;
    System.out.println(DataSource.checkDomain());
   connection =  DbConnection.getConnection();

}}

Exception

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

Note: I have connected successfully using dbvisualizer, also tried to use neatbeas database services and it is not connecting too

Netbeans error : Cannot establish a connection to jdbc:oracle:thin:@oracleinstance.ctnrs4kazdmm.ap-south-1.rds.amazonaws.com:1521:XE using oracle.jdbc.OracleDriver (Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor )

2

2 Answers

1
votes

Thanks for answering my question.

I have figure it out. The mistake was in the URL to connect to db as u mentioned. So, what found is that the url should be like this :-

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@"+hostname+":1521:orcl", "username", "password");

ORCL is the defualt db name for AWS ( AMAZON cloud )

Thanks

0
votes

Based on your error message ORA-12505, TNS:listener does not currently know of SID given in connect descriptor, I think you have an error in your connection string. Specifically, you are likely using the wrong SID.

oracleinstance.ctnrs4kazdmm.ap-south-1.rds.amazonaws.com:1521:XE

Try the following connection string, oracleinstance.ctnrs4kazdmm.ap-south-1.rds.amazonaws.com:1521:orcl. Normally, when you create an Oracle instance in RDS the default SID is orcl.

You can also use the AWS CLI to determine the SID.

$ aws rds describe-db-instances
{
"DBInstances": [
...
  {
        "DBInstanceStatus": "available",
        "DBInstanceIdentifier": "MYORACLEDB",
        "MasterUsername": "USERWITHSYSDBA",
        "EngineVersion": "11.2.0.4.v1",
        ...
        "Endpoint": {
            "Port": 1521,
            "Address": "MYORACLEDB.blahblahblach.us-west-2.rds.amazonaws.c                                                                                                                                                         om"
        },
        "PendingModifiedValues": {},
        ...
        "DBName": "ORCL",
        ...

Where the DBName is the SID (in this case ORCL).