0

am trying to connect mysql in remote server through SSH connection from my pc runing windows OS . I installed all the required ssh, Java jars and trust am able to access SSH but instead of connecting to remote host am connecting the local db one my machine how cloud i told below code to went to remote server since also my server provider not giving ip only allow access through ssh the code is hereunder

package connectsshserver;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.Connection;
public class MySqlConnOverSSH {
    public static void main(String[] args) throws SQLException {
        int lport=3306;
        String rhost="host47.registrar-servers.com";
        String host="host47.registrar-servers.com";
        int rport=3306;
        String user="soft123a"; //ssh user name 
        String password="123487ab"; //ssh password
        String dbuserName = "root"; 
        String dbpassword = "";
        String url = "jdbc:mysql://127.0.0.1"+"/arbaccounting"; //host+db
        String driverName="com.mysql.jdbc.Driver";
        Connection conn = null;
        Session session= null;
        try{
            //Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            session=jsch.getSession(user, host, 21098);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");
            int assinged_port=session.setPortForwardingL(21098, rhost, 3306);
            System.out.println("127.0.0.1:"+assinged_port+" -> "+rhost+":"+rport);
            System.out.println("Port Forwarded");
            //mysql database connectivity
            Class.forName(driverName).newInstance();
            conn = DriverManager.getConnection (url, dbuserName, dbpassword);
            System.out.println ("Database connection established");
            System.out.println("DONE");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(conn != null && !conn.isClosed()){
                System.out.println("Closing Database Connection");
                conn.close();
            }
            if(session !=null && session.isConnected()){
                System.out.println("Closing SSH Connection");
                session.disconnect();
            }
        }
    }

}

the out perfect but for local db what i change to connect to remote db ? Connected 127.0.0.1:21098 -> host47.registrar-servers.com:3306 Port Forwarded Database connection established DONE Closing Database Connection Closing SSH Connection BUILD SUCCESSFUL (total time: 9 seconds)

1 Answer 1

1

in the line: DriverManager.getConnection (url, dbuserName, dbpassword), the url reference points to localhost (127.0.0.1) which establishes the DB connection with your location machine

java.net.InetAddress#getByName(String) will allow you to retrieve the IP of the remote host in order to use it. You should also be able to replace the 127.0.0.1 with the name of the server, rather than the IP.

Sign up to request clarification or add additional context in comments.

2 Comments

i put ip address as well as hostname but have got error : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Do you have any information about the underlying exception? I.E. comms rejected, buffer failure, etc.?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.