Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JDBC connection by using SSH to a remote database

843859Nov 1 2007 — edited Nov 20 2014
Hello I am using jsch libraries (last version) to connect to a database with jdbc by using ssh tunnel. But I could not accomplished this and I could not find any example about jdbc with those libraries. I will be happy if you could help me about this situation. Here is the example code that I am currently working to connect with ssh:
import com.mysql.jdbc.*;
import com.jcraft.jsch.*;
import java.lang.Exception;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.awt.*;
import java.io.*;
public class Main {
   
 
    /**
     * @param args the command line arguments
     */
    static Connection conn = null;
    static String userid=UserName, password = Password;
    static String url = "jdbc:mysql://DbServerIpAddress:/3306/tkdeneme";   
    public static Channel channel;
    /** Creates a new instance of DbConnect */
 
    public static Connection initMySQLJDBCConnection() throws InstantiationException, IllegalAccessException{

        try {
                    Class.forName ("com.mysql.jdbc.Driver").newInstance ();   

        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }

        try {
                    conn= DriverManager.getConnection(url,userid, password);
        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.toString());
        }
       
        return conn;
    }
    public static void closeMySQLJDBCConnection(){
        if(conn!=null){
            try{
                conn.close();
            }
             catch(Exception ex){
                ex.printStackTrace();
             }
        }
    }
 
    public static void main(String[] args) throws JSchException {
        // TODO code application logic here
      JSch jsch=new JSch();
      String host=DbServerIpAddress;
      String user=username;
      int port=22;

      Session session=jsch.getSession(user, host, port);
          try{


      // username and password will be given via UserInfo interface.
      UserInfo ui=new MyUserInfo(password);
      session.setUserInfo(ui);
      session.connect();
      System.out.println("Session opened.");
      Channel channel=session.openChannel("exec");
     // channel.
          initMySQLJDBCConnection();
       channel.disconnect();
     
      }
      catch(Exception ex){
          System.err.println(ex.toString());
          System.err.println("Error occured!");
          System.exit(0);
      }
       finally
       {
           System.out.println("Connection established");
           closeMySQLJDBCConnection();
           session.disconnect();
       }
    }
   
}
I am getting SQLException:Communications link failure exception in this code. And I am not sure which channel to use for jdbc.

I would appreciate if you could help me.

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 30 2007
Added on Nov 1 2007
7 comments
6,371 views