如何在JavaFX应用程序中连接Godaddy MySQL

时间:2018-12-08 06:04:29

标签: android mysql javafx jdbc javafxports

在javafx桌面/ android应用程序中连接GoDaddy MySQL连接时出现问题。我想直接连接到GoDaddy上的MySQL,而不使用ssh技术。

这是我的Main.java文件:

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);
    }
}

这是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"
            }
        }
    }
}

这是错误/堆栈跟踪:

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

我已经如图所示将IP地址添加到了Godaddy Remote MySQL 中。

enter image description here

如果我使用ssh技术,则它可在桌面而不是在android设备中工作。请参阅我的帖子Javafx android mysql connect over ssh

2 个答案:

答案 0 :(得分:0)

否,您不想直接连接到mysql。不是来自android。为此,您需要在应用程序中输入数据库的密码,这意味着数据库将受到威胁。正确的方法是设置一个Web服务,并仅从该Web服务访问数据库。然后,Android应用程序将调用网络服务。

相同的规则适用于桌面应用。直接连接到数据库的唯一应用应该是在自己的服务器上运行的应用。

答案 1 :(得分:0)

好!好!好吧!....

我真的在前两天晚上之前解决了我的问题...

解决方案非常简单...

跟我来:

转到google.com,如图find my ip address所示进行搜索:

enter image description here

通过点击godaddy remote mysql按钮,在remote MySQL设置页面中输入此公共IP地址,如下所示:

enter image description here

添加,然后在以下字段中输入您的公共IP:

enter image description here

然后编写Java程序进行测试。它也可以在台式机和Android设备上使用。要连接到远程MySQL(godaddy mysql),您不需要ssh技术。仅您的IP必须具有连接Godaddy服务器(公共IP)的权限。

如果有任何困惑评论我......