步骤:
String diver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/grade"
+ "?user=root&password=18170021&serverTimezone=UTC";
Class.forName(diver);
Connection conn = DriverManager.getConnection(url, user, password);
具体代码
package cn.edu.hbue.wmp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionDemo01 {
public static void main(String args[]){
String diver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/grade"
+ "?user=root&password=18170021&serverTimezone=UTC";
try {
Class.forName(diver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
if(conn != null)
System.out.println("成功");
else
System.out.println("失败");
}
}