package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBHelper { // 0,导入jar包 // 1,数据库驱动 private final static String driver = "com.mysql.jdbc.Driver"; // 2,连接数据库的URL地址 private final static String url ="jdbc:mysql://localhost:3306/Book?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; private final static String username = "root"; private final static String password="123"; private static Connection conn = null; /* 静态代码块加载驱动 */ static { try { Class.forName(driver); }catch(Exception e){ e.printStackTrace(); } } /* 案例模式返回数据库连接对象 */ public static Connection getConnection() throws SQLException { if(conn==null) { conn = DriverManager.getConnection(url, username, password); return conn; } return conn; } }