需要导的jar包
import org.apache.commons.dbutils.DbUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
public DBUtil(){
}
// static {
// try {
// Class.forName("com.mysql.cj.jdbc.Driver");
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// }
static {
DbUtils.loadDriver("com.mysql.cj.jdbc.Driver");
System.out.println("驱动连接成功...........");
}
public static Connection getcConnection(){
try {
return DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/bookstore?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8",
"root","root123");
} catch (SQLException e) {
throw new RuntimeException("数据库连接失败....",e);
}
}
}