嗨,我这里有这段代码,但是当应用程序开始向我显示错误时!如何修复它? :
错误:
"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/