Java program to connect to remote MySQL db (having ssh server running) -


i want develop java swing application. in which, mysql db stored on 1 pc. , software can access db anywhere. new type of jdbc connection. so, searching possible ways create such remote connection. got following possible solutions:

1) can host db on web. that's worked me. tried host db on freesqldatabase.com allows remote db connection.

2) heard ssh tunneling. topic new me. so, first created ssh server on pc having mysql db. so, pc running ssh server. tried putty software on client machine connect ssh server. , working. able give access client machine through ssh tunnel.

so remote connection working now. want execute jdbc connection client computer. connect ssh running computer having db.

i familiar db connection using ip address, like,

string url = "jdbc:mysql://server ip address/databasename";            class.forname ("com.mysql.jdbc.driver");            conn = drivermanager.getconnection (url,"username","password"); 

so please tell me, ssh connection in java programming? how can connect server(pc having ssh running , mysql db) client pc?

thanx in advance.

jsch looking for.

this library doing same thing have done, tunneling local machine remote machine. jsch start tunnelling , jdbc connect thru jsch setting.

this example here.

package com.journaldev.java.ssh  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 {      /**      * java program connect remote database through ssh using port forwarding      * @author pankaj@journaldev      * @throws sqlexception       */     public static void main(string[] args) throws sqlexception {          int lport=5656;         string rhost="secure.journaldev.com";         string host="secure.journaldev.com";         int rport=3306;         string user="sshuser";         string password="sshpassword";         string dbusername = "mysql";         string dbpassword = "mysql123";         string url = "jdbc:mysql://localhost:"+lport+"/mydb";         string drivername="com.mysql.jdbc.driver";         connection conn = null;         session session= null;         try{             //set stricthostkeychecking property no avoid unknownhostkey issue             java.util.properties config = new java.util.properties();              config.put("stricthostkeychecking", "no");             jsch jsch = new jsch();             session=jsch.getsession(user, host, 22);             session.setpassword(password);             session.setconfig(config);             session.connect();             system.out.println("connected");             int assinged_port=session.setportforwardingl(lport, rhost, rport);             system.out.println("localhost:"+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();             }         }     }  } 

im never done config local machine start tunneling remote server when machine up.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -