I use Jsch to connect to a remote mysql database, where the ssh host does not match the mysql host, as shown in the upper half in the figure below: 
Below is the code that I use to connect SSH:
private static void connectSSH() throws SQLException { try { java.util.Properties config = new java.util.Properties(); JSch jsch = new JSch(); jsch.setLogger(new MyLogger()); session = jsch.getSession(sshUser, sshHost, 22); jsch.addIdentity(SshKeyFilepath, sshPassword); config.put("StrictHostKeyChecking", "no"); config.put("ConnectionAttempts", "3"); session.setConfig(config); session.connect(); System.out.println("SSH Connected"); Class.forName(driverName).newInstance(); int assinged_port = session.setPortForwardingL(localPort, remoteHost, remotePort); System.out.println("localhost:" + assinged_port + " -> " + sshHost + ":" + remotePort); System.out.println("Port Forwarded"); } catch (Exception e) { e.printStackTrace(); } }
and ultimately connect to the database using the following code:
Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:" + localPort, dbUser, dbPassword);
I can successfully make an SSH connection, but execution is suspended when the line below:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:" + localPort, dbUser, dbPassword);
I double-checked my SSH and database credentials by connecting to the command line. I can connect to the database using
mysql -h host1 -u "myusername" -p"mypasswordhere"
I suppose the problem may be due to the fact that on the remote host, the mysql host is not localhost, but host1 , which I do not know where to specify in jdbc url.