Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
主要查看url写正确没有。
比如:url="jcbc:mysql://localhost3306/db_cjky"; 在3306少了一个冒号。服务器无法链接,就报此错误。
public class StudentDao {
public Connection getConnection() {
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/db_cjky";
String username="root";
String password="123456";
conn=DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}
return conn;
}