package dbb;
import java.sql.*;
public class test {
public static void main (String[] args) {
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pass = "1234";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Searched");
conn = DriverManager.getConnection(url, user, pass);
System.out.println("Connection Succeed" + conn);
} catch (ClassNotFoundException e) {
System.out.println("Driver Not Searched");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
它显示已搜索到驱动程序,但连接仍然失败。
如何解决错误?
答案 0 :(得分:1)
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pass = "Admin@123";
java.sql.Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Searched");
conn = DriverManager.getConnection(url, user, pass);
System.out.println("Connection Succeed" + conn);
} catch (ClassNotFoundException e) {
System.out.println("Driver Not Searched");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
它在mysql-connector-java-5.1.6jar
的系统中正常运行。可能这是MySql
驱动程序的问题。
您可以尝试使用以下内容: 您可能需要在jdbc url。中明确指定时区:
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
答案 1 :(得分:0)
正如评论中建议的那样,使用非ssl连接与最新的驱动程序,你应该使用下面的代码:
package dbb;
import java.sql.*;
public class test {
public static void main (String[] args) {
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String user = "root";
String pass = "1234";
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Searched");
conn = DriverManager.getConnection(url, user, pass);
System.out.println("Connection Succeed" + conn);
} catch (ClassNotFoundException e) {
System.out.println("Driver Not Searched");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}