diff --git a/src/server/tools/DBConnection.java b/src/server/tools/DBConnection.java index 6f523fd..f6dc894 100644 --- a/src/server/tools/DBConnection.java +++ b/src/server/tools/DBConnection.java @@ -1,26 +1,45 @@ package server.tools; import java.sql.Connection; +import java.sql.DriverManager; import java.sql.PreparedStatement; +import java.sql.SQLException; public class DBConnection { - private String driverName; - private String url; - private String user; - private String password; + private final static String driverName = "com.mysql.cj.jdbc.Driver"; + private final static String url = "jdbc:mysql://localhost:3306/lda?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT"; + private final static String user = "root"; + private final static String password = "516849"; // 加载驱动 static { - + try { + Class.forName(driverName); + } catch (Exception e) { + e.printStackTrace(); + } } public static Connection getConnection() { - - return null; + Connection connection = null; + try { + connection = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + e.printStackTrace(); + } + return connection; } public static void close(Connection con, PreparedStatement pst) { - + try { + if (pst != null) { + pst.close(); + } + if (con != null) { + con.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } } - -} +} \ No newline at end of file