根据报错信息来看,仍然是ClassNotFoundException异常,说明仍然找不到com.mysql.jdbc.cj.Driver这个类。这可能是因为你使用的MySQL版本较新,而驱动类的名称已经发生了变化。
在MySQL的8.0版本中,驱动类的名称已经改为com.mysql.cj.jdbc.Driver。所以你需要将代码中的驱动类名称修改为com.mysql.cj.jdbc.Driver,修改后的代码如下所示:import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class JDBCDemo { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/db1"; String username = "root"; String password = "root"; Connection conn = DriverManager.getConnection(url, username, password); String sql = "update account set money = 2000 where id = 1"; Statement stmt = conn.createStatement(); int count = stmt.executeUpdate(sql); System.out.println(count); stmt.close(); conn.close(); }}
请确保你的MySQL版本和驱动版本匹配,并且将驱动jar包导入到项目的classpath中。如果还有问题,请再次检查配置和依赖,并确保数据库已经启动。希望这次能够解决你的问题!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |