0

I am having a problem in connecting my GoDaddy MySQL connection in a javafx desktop/android application. I want to connect directly to MySQL located at GoDaddy, without using ssh technique.

This is my Main.java file:

package com.maqboolsolutions.mysql_ssh_android;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application {

    String HOST = ""; // Hosting provider ip address / name (e.g. example.com)
    String DB_NAME = ""; // Database name (e.g. db_test)

    String DB_URL = "jdbc:mysql://" + HOST + ":3306/" + DB_NAME;

    String USERNAME = ""; // mysql database user name (e.g. admin)
    String PASSWORD = ""; // mysql database password (e.g. 123)

    @Override
    public void start(Stage primaryStage) throws IOException {
        getConnection();
    }

    private void getConnection() {
        try {
            //mysql database connectivity
            Class.forName("com.mysql.cj.jdbc.Driver");

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

            System.out.println("Database connection established");
            System.out.println("DONE");

        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

This is the gradle.build:

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.mysql_ssh_android.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"
            }
        }
    }
}

This is the error/stacktrace:

Executing: gradle :mysql_ssh_androidApp:run
Arguments: [-PcmdLineArgs=, -PjvmLineArgs=, -c, C:\Users\MaqboolSoftPC\Documents\NetBeansProjects\mysql-ssh-android\mysql_ssh_android\settings.gradle]

:mysql_ssh_androidApp:compileJava
:mysql_ssh_androidApp:processResources UP-TO-DATE
:mysql_ssh_androidApp:classes
:mysql_ssh_androidApp:compileDesktopJava NO-SOURCE
:mysql_ssh_androidApp:processDesktopResources NO-SOURCE
:mysql_ssh_androidApp:desktopClasses UP-TO-DATE
Dec 08, 2018 10:46:19 AM com.maqboolsolutions.mysql_ssh_android.Main getConnection
SEVERE: null
java.sql.SQLException: Access denied for user 'admin'@'example.com' (using password: YES)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.maqboolsolutions.mysql_ssh_android.Main.getConnection(Main.java:39)
    at com.maqboolsolutions.mysql_ssh_android.Main.start(Main.java:31)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

:mysql_ssh_androidApp:run

BUILD SUCCESSFUL in 4s
3 actionable tasks: 2 executed, 1 up-to-date

I already add my IPaddress in godaddy Remote MySQL as shown.

enter image description here

If I use ssh technique, then it works in desktop and not in android device. See my post Javafx android mysql connect over ssh.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • To connect from a JavaFX app would require your MySQL Server to be publicly accessible, which would be a bad idea (or alternatively need some kind of VPN or whitelisting setup). Reconsider your design and maybe use a REST service to mediate between your app and the database. – Mark Rotteveel Dec 08 '18 at 06:53
  • any idea! or may be any tutorial / resource should be best. – CTO - Abid Maqbool Dec 08 '18 at 08:33

2 Answers2

0

No, you don't want to directly connect to the mysql. Not from android. To do so, you need to put your password for the db in your app, which means your database will be compromised. The correct way to do this is to set up a webservice, and access the db only from the webservice. The android app would then call the webservice.

Same rules apply for a desktop app. The only apps that connect directly to your db should be apps run on your own servers.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Ok! Ok! Ok!....

I really solved my problem before last two days evening...

The solutions is very simple...

Follow me:

Go to google.com and search like this find my ip address as shown in the figure:

enter image description here

Enter this public IP address in godaddy remote mysql setting page by clicking remote MySQL button as shown:

enter image description here

Add then enter your public IP in the following field:

enter image description here

Then write your java program for testing. It's works on desktop also and the android device. For connecting to remote MySQL (godaddy mysql) you do not need ssh technique. Only your ip must have permission to connect godaddy server (public ip).

If any confusion comments me......