gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:42:00 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com