gpt4 book ai didi

java - 如何修复: “No suitable driver found for jdbc:mysql://localhost/dbname”

转载 作者:行者123 更新时间:2023-11-29 12:58:59 26 4
gpt4 key购买 nike

嗨,我这里有这段代码,但是当应用程序开始向我显示错误时!如何修复它? :

错误:

"No suitable driver found for jdbc:mysql://localhost/database"

代码:

try {
connection = DriverManager.getConnection("jdbc:mysql://"+host+"/"+database,username,password);
connection.setAutoCommit(false);
System.out.println("Connected to MySQL!");

} catch (SQLException e) {
System.out.println("Couldn't connect to MySQL!");
System.out.println(e.getMessage());
System.exit(0);
}

最佳答案

确保您拥有正确版本的 jar - 类似于 mysql-connector-java-5.1.14-bin.jar

记住主机参数是否也包括端口。 (默认情况下 3306 是 MySQL 的端口。)

像下面一样加载类

Class.forName("com.mysql.jdbc.Driver").newInstance(); 

然后就可以正常工作了。

关于java - 如何修复: “No suitable driver found for jdbc:mysql://localhost/dbname” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23613651/

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