import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Demo1 {
//数据库地址
private static String dbUrl="jdbc:mysql://localhost:3306/db_book?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
//驱动名称
private static String jdbcName="com.mysql.cj.jdbc.Driver";
//用户名
private static String userName="root";
//密码
private static String passWord="root";
public static void main(String[] args){
try {
Class.forName(jdbcName);
System.out.println("加载驱动成功!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("加载驱动失败!");
}
//连接数据库
Connection c = null;
try {
c = DriverManager.getConnection(dbUrl, userName, passWord);
System.out.println("连接数据库成功");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("连接数据库失败!");
}finally{
try {
//关闭数据库
c.close();
System.out.println("数据库已关闭!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
其中数据库地址变为:"jdbc:mysql://localhost:3306/db_book?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"。
驱动名称变为:com.mysql.cj.jdbc.Driver。