求大佬解释一下这段代码的意思,前半部分是链接数据库的,如果链接异常就打印Can't load Driver我能理解,我不理解的是后半部分的public static int runUpdate(String sql) throws SQLException里面的内容
public class DBUtil {
private static String driver = ("com.mysql.cj.jdbc.Driver");
private static String URL = "jdbc:mysql://localhost:3306/caipiao?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
private static Connection con = null;
private static Statement smt = null;
private static ResultSet rs = null;
private static Connection createConnection() {
try {
Class.forName(driver);
return DriverManager.getConnection(URL, "root", "root");
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (java.lang.ClassNotFoundException e) {
System.out.println("Can't load Driver");
}
return null;
}
public static int runUpdate(String sql) throws SQLException {
int count = 0;
if (con == null) {
con = createConnection();
}
if (smt == null) {
smt = con.createStatement();
}
count = smt.executeUpdate(sql);
if (smt != null) {
smt.close();
smt = null;
}
if (con != null) {
con.close();
con = null;
}
return count;
}