0

I'm trying to connect to a database which is hosted in a remote machine. I have a java project in my local and it has to connect to DB which is in remote machine for an update. What is the java code which I can use to connect. Traditional code of connection doesn't work and it shows below error.

import java.beans.Statement;
import java.sql.*;

public class connectTest {

    public static void main(String[] args) {

           try{  
                  //step1 load the driver class  
                  Class.forName("oracle.jdbc.driver.OracleDriver"); 
                 // Class.forName("oracle.jdbc.OracleDriver");

                  Connection con=DriverManager.getConnection(  
                               "jdbc:oracle:thin:@172.25.250.183:1521/aaadv4","boomerang","Telus2014"); 

                  Wrapper stmt=con.createStatement(); 

                  ResultSet rs=((java.sql.Statement) stmt).executeQuery("select * from ttv_dhcp_log.ACCT_MSG where SUB_IF='BACUPQXQOT01 PON 1/1/12/02:21.1.1'");  
                  while(rs.next())  
                  System.out.println(rs.getString(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  

                  //step5 close the connection object  
                  con.close();  

                  }catch(Exception e){ System.out.println(e);}  

                  }
}

I expect it to connect to DB but it fails with below message.

java.sql.SQLException: Io exception: The Network Adapter could not establish the connection

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Are you sure your database server is running on port 1521 ? That's not the default port. Your error can mean two things: either the server at 172.25.250.183 cannot be reached (wrong IP, network issue, etc.) or port 1521 is not open to connections. – Catar4 Jun 19 '19 at 21:38
  • 172.25 is a private space IP... not accessible outside of internal network. – pcalkins Jun 19 '19 at 21:42
  • You seem to be connecting to a machine that is only exposed to the local network. you would have to port forward to your machine – EDToaster Jun 19 '19 at 21:45

1 Answers1

1

There could be a couple of things you need to verify.

  1. Check if the port is accessible from the machine . Use comamnds like telnet to check that. Ex: telnet IPaddress portnumber

  2. Check your firewall in your machine to see if connections to the port range are allowed or not.

  3. Check the firewall rules in the machine where Oracle db is hoster as well.
  4. Ensure your Credentials are correct.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = null;
connection = DriverManager.getConnection("jdbc:oracle:thin:@172.25.250.183:1521:aaadv4","username","password");
connection.close();

Use the above. Your DB connection URL contains a "/" instead of ":" . Also, ensure your IP address is accessible from your machine. If you are in the same network. It should be. If not, follow the debugging steps given above.

  • After connecting to my client vpn and when i wanted to connect to that database, normally i use the address of remote machine and then use login credentials to get into the machine. Then accessing of database is a normal process. Now, i want the same to be done via Java code. So is there a way to do that exact operation via code using the same port? Or is it the PORT/Firewall issue which could be stopping me? – Mahesh P Jun 22 '19 at 08:09