gpt4 book ai didi

javafx 程序无法连接到 MySQL

转载 作者:行者123 更新时间:2023-12-01 16:20:50 28 4
gpt4 key购买 nike

我在将 javafx 程序连接到 MySQL 数据库时遇到问题。我正在尝试连接我正在制作的实际程序,但一开始尝试连接基本上是一个空白程序,该程序仅连接到数据库并打印那里的内容,所以至少我知道我的连接良好。我正在使用 intellij 和 mysql 工作台。我刚刚从笔记本电脑中删除了所有 mysql 内容并重新安装了它。我在那里遇到了问题,但学会了如何正确删除它并重新安装它。这是我非常没有受过教育的选择,因为我的mysql连接器j,数据库没有连接。我忘记下载了什么,因为已经太久了,但我认为我刚刚下载的mysql是版本8.0.20,但我的程序只接受6.0.5。这里有各种图像和代码片段。 我更改了代码中的一件事,现在已连接!!! 好的,当我写这篇文章时,我更改了一些内容,现在已连接。看起来我正在混合连接器 j 的两个版本。

sample.fxml
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
<center>
<Button text="Test Database" onAction="#testDBConnection"/>
</center>
</BorderPane>`

ConnectionUtil

package sample;
import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionUtil {

public static Connection conDB(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quizdb?useSSL=false", "root", "hockey");
System.out.println("Connected");
return con;
} catch (Exception ex) {
System.out.println("no connection");
return null;
}
}

}

Controller

package sample;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Controller {

Connection con = ConnectionUtil.conDB();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

public void testDBConnection(){
String sql = "select * from question";

try {
preparedStatement = con.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
if(!resultSet.next()){
System.out.println("End of questions");
} else {
int questionId = Integer.parseInt(resultSet.getObject("id").toString());
String question = resultSet.getObject("question").toString();
String correctAnswer = resultSet.getObject("correctAnswer").toString();
System.out.println("Question# " + questionId + "\nQuestion: " + question + "\nCorrect Answer: " + correctAnswer);

}
} catch (SQLException throwables) {
throwables.printStackTrace();
}

}

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>kruzel.rob</groupId>
<artifactId>TestConnection</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>

</dependencies>


</project>

这里是我的mysql db、项目结构、外部库等的图片

database instance

what the db tables look like

external libraries/project structure

所以我想我现在的问题是为什么在 pom.xml 版本中我需要 6.0.5 但我的外部库链接到 8.0.20?它们最初链接到 6.0.5,但我无法连接,因此我下载了 8.0.20 jar 和 source.jar 并添加了它们。我知道连接终于可以正常工作,但想了解这里发生了什么。预先感谢您的帮助!!

最佳答案

正如我的评论中所述,这是因为 MySQL 类名发生了更改,旧的类名已被废弃。在这里阅读: MySQL Docs

关于javafx 程序无法连接到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62287403/

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