我们的项目在服务器上有一个数据库
当我运行程序时,它运行得非常好。我可以登录并注册另一个帐户
但当我的同学运行代码时,他得到了一个异常:No suitable driver found for jdbc:mysql://ServerIp//DBName
我的代码如下:
String hostURL = "jdbc:mysql://ServerIp:3306/Database"; //There's an actual ip of course
Connection con = DriverManager.getConnection(hostURL, "username", "password");
System.out.println("Connected successfully");
我们已经将mysql-connector
添加到依赖项(IntelliJ(中,但它根本没有帮助
此外,我甚至不需要添加它,这个项目对我来说很好,什么都没做。该项目在Java Enterprise中->Web应用程序
如果JDBC无法为传递给getConnection((的URL格式方法,例如";jdbc:mysql://";在我们的案例中。
为了解决这个错误,您需要MySQL JDBC驱动程序,如"mysql-connector-java-5.1.36.jar";在类路径中。
或者是因为您忘记向java应用程序注册mysqljdbc驱动程序。
Connection con = null;
try {
//registering the jdbc driver here, your string to use
//here depends on what driver you are using.
Class.forName("something.jdbc.driver.yourdriver");
con = DriverManager.getConnection(hostURL, ...);
} catch (SQLException e) {
throw new RuntimeException(e);
}