Javafx android mysql connect over ssh

2020-03-31 09:34发布

问题:

I want to connect mysql database located at hosting server (e.g. godaddy) using ssh technique (because it does not allow direct connection) in android javafx (using javafxmobile-plugin) project.

I scussfully connected to mysql database using ssh with running in my desktop pc (window). But when i run it on the android it all stuck....

Currently my codding is...

.java file:

public static final Integer PORT = 53009;

public static final String DB_NAME = "";
public static final String DB_URL = "jdbc:mysql://localhost:" + PORT + "/" + DB_NAME;
public static final String USERNAME = "";
public static final String PASSWORD = "";

public static final String DRIVER = "com.mysql.cj.jdbc.Driver";

--------------------------------------------------------

Connection con = null;
Session session = null;

//Fields
String rHost = "";
String user = "";
String password = "";

int rPort = 3306;

public void getRootConnection() throws SQLException {
    close();

    try {
        //Create the JSCH object
        JSch jsch = new JSch();

        //Get the session
        session = jsch.getSession(user, rHost);

        //Set the password
        session.setPassword(password);

        //To be able to connect to any host (this is just for testing!)
        session.setConfig("StrictHostKeyChecking", "no");

        //Connect the session
        session.connect();

        //Set port forward
        session.setPortForwardingL(PORT, "localhost", rPort);

        //Show message
        System.out.println("Waiting for connections…");

        //mysql database connectivity
        Class.forName(DRIVER).newInstance();

        con = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);

        System.out.println("Database connection established");
        System.out.println("DONE");
    } catch (ClassNotFoundException | JSchException | InstantiationException | IllegalAccessException ex) {
        Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void close() {
    try {
        if (con != null && !con.isClosed()) {
            System.out.println("Closing Database Connection");
            con.close();
        }
        if (session != null && session.isConnected()) {
            System.out.println("Closing SSH Connection");
            session.disconnect();
        }
    } catch (SQLException ex) {
        Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.3.16'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

mainClassName = 'com.maqboolsolutions.Main'

dependencies {
    compile 'com.gluonhq:charm:5.0.2'

    compile 'mysql:mysql-connector-java:8.0.13'
    compile 'com.jcraft:jsch:0.1.54'
}

jfxmobile {
    downConfig {
        version = '3.8.6'
        // Do not edit the line below. Use Gluon Mobile Settings in your project context menu instead
        plugins 
    }
    android {
        manifest = 'src/android/AndroidManifest.xml'

        javafxportsVersion = '8.60.11'

        android {
            manifest = 'src/android/AndroidManifest.xml'

            packagingOptions {
                exclude 'META-INF/INDEX.LIST'
            }

            dexOptions {
                javaMaxHeapSize "4g"
            }
        }
    }
}

If you like to help me and want to test your own this is the link to the project located at github mysql-ssh-android test project

My main problem is that when i run it on android, ssh connection works. also forwarded ports works. but when i connect mysql using localhost and forwarded port, my app suddenly close with error unresponsive..

Any help?

If you need more info. I am ready. Please help me if you can???