package com.until;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
public static Connection conn = null;
// 数据库账号,密码,名字
String url = "jdbc:mysql://localhost:3306/wms?serverTimezone=UTC";
String user = "root";
String password = "root";
public DBUtil() {
// 连接驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("加载驱动成功");
} catch (Exception e) {
System.out.println("加载驱动失败");
e.printStackTrace();
}
// 连接数据库
try {
conn = DriverManager.getConnection(this.url, this.user, this.password);
System.out.println("连接数据库成功");
} catch (SQLException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
}
}
}
若MySQL版本为8,则Class.forName("com.mysql.cj.jdbc.Driver");
若MySQL版本为5,则Class.forName("com.mysql.jdbc.Driver");