mysql - 如何使用 Gradle 导入 MySQL 并连接到 JavaFX

标签 mysql gradle javafx

我正在使用 IntelliJ IDEAJavaFX 10 用于我的练习学习。我要连接 MySQL并使用 Gradle 导入它。

我在互联网上找到了多个示例,但我没有找到任何最新的,大部分功能已经贬值,阅读这么多不同的示例让我感到困惑。

最佳答案

您可能想先阅读官方 documentation而不是阅读多个示例。

我假设你是初学者。

1.) First thing to do is to Download the MySQL Installer to intall the Server into your machine. Remember that things won't work without this.

During the installation you will need to set your Server PORT, root password or add a new user, just remember the Server PORT and root Password and leave things in default.

2.) Go to Maven Central. We need to import the MySQL Connector. In order to import the MySQL Connector into Gradle we need to get the correct group, name and version for it.

You notice that in Maven Central there are multiple selection on how you can import the jar into your project, this time we want to import it using Gradle, so you have to choose the Gradle and copy the code: compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'

NOTE - In order to avoid problems, just always choose and use the latest version of MySQL Installer and MySQL Connector, at the moment the latest version is 8.0.11.

3.) In IntelliJ IDEA, in order to import in Gradle, in your project there is build.gradle click it to open then paste the code you copied in dependencies.

it should look like:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'
}

4.) Create a class where you want to connect into MySQL, the below code is my example. Main.Class

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {

    public static void main(String[] args){

        Connection conn = null;
        String user = "whatEverUserNameYouSetup";
        String password = "whatEverPasswordYouSetup";
        String database = "whatEverTheNameOfYourDatabase";
        int port = 3306; //default port, change it depending on your setup
        try {
            Class.forName("com.mysql.cj.jdbc.Driver").getConstructor().newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost:" + port + "/" + database, user, password);

            // Do something with the Connection

        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

注意:文档是您的 friend 。

更新:如果你运行时遇到类似这样的错误

Wed Dec 09 22:46:52 CET 2015 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

然后您需要将useSSL设置为false,代码如下所示:

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Main {

    public static void main(String[] args){

        Connection conn = null;
        Properties properties = new Properties(); //I use Properties to make things easer. 
        properties.setProperty("user", "root");
        properties.setProperty("password", "YourPassword");
        properties.setProperty("useSSL", "false"); //Set useSSL to false to solve the problem.

        try {
            Class.forName("com.mysql.cj.jdbc.Driver").getConstructor().newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306", properties);

            // Do something with the Connection

        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

关于mysql - 如何使用 Gradle 导入 MySQL 并连接到 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51111546/

相关文章:

php - 当循环的一部分时,仅将唯一值插入 MySQL

php pdo 查询不适用于数组

php - 如何在单独的类中处理连接到 mysql 数据库?

php - 搜索多列,就好像它是 mysql 中的一列

android - Gradle同步失败:找不到com.android.tools.build

java - Android Studio : Could not find com. android.tools.build :gradle:3. 6.2 导入 Eclipse 项目期间

spring-boot - 有没有一种简单的方法可以更改应用程序iml中的目录以构建用于测试的文件夹?

javafx listview自动滚动到末尾

JavaFX、MouseEvent问题

java - 如何监听 JavaFX tableview 中行的取消选择?