gpt4 book ai didi

Java与MySQL连接不成功

转载 作者:行者123 更新时间:2023-11-30 00:19:27 25 4
gpt4 key购买 nike

我在 Eclipse 中从 Java 数据库连接连接到 MySQL 时遇到问题。我使用了最新的连接器 mysql-connector-java-5.1.30-bin.jar。另一方面,我的 MySQL 数据库没有密码,所以我决定在用户​​名“root”(例如“”)纠正我(如果我错了)后将其留空。附件是我的代码和屏幕截图。请让我知道该怎么做,非常感谢您的帮助。

<%@ page import="java.sql.*" %><%@ page import="java.io.*" %><%@ page import="com.mysql.*" %><?xml version="1.0"?>
<tours>
<%
Connection connection = null;
Statement statement = null;
ResultSet result = null;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tours", "root", "");
out.println("connected to database");
}
catch(SQLException e) {
out.println("error connecting to database");
}
%>
</tours>

最佳答案

<%@ page import="java.sql.*" %><%@ page import="java.io.*" %>
<?xml version="1.0"?>
<tours>
<%
Connection connection = null;
Statement statement = null;
ResultSet result = null;

try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tours", "root", "");
out.println("connected to database");
}
catch(SQLException e) {
e.printStackTrace(e);
}
%>
</tours>

据我所知,不需要使用 newInstance() 方法。也许你应该对 mysql 使用密码。并在 catch block 中使用 e.printStackTrace() 方法,以便您可以知道代码出了什么问题。 java.sql.* 和 com.mysql. 之间可能存在冲突。使用java.sql。连接到mysql数据库。

关于Java与MySQL连接不成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23356519/

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