错误:
No suitable driver found for jdbc:mysql://localhost:3306
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
这是我的代码。
public Connection getConnection() throws SQLException {
Connection conn;
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "pass");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" , connectionProps);
System.out.println("Connected to database");
return conn;
}
答案 0 :(得分:1)
import java.sql.*;
尝试放入课堂顶部。
同时将此Class.forName("com.mysql.jdbc.Driver");
放在DriverManager.getconnection
之前,以便识别驱动程序。
答案 1 :(得分:0)
您需要在获得连接之前注册您的驱动程序(如果它已经注册,则不会执行任何操作):
public Connection getConnection() throws SQLException {
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/";
Class.forName(myDriver);
Connection conn;
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "pass");
conn = DriverManager.getConnection(myUrl , connectionProps);
System.out.println("Connected to database");
return conn;
}