Skip to content

Commit

Permalink
修复MySQL驱动加载并优化连接获取逻辑,同时处理异常。
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed May 29, 2024
1 parent 38ddf7f commit bb68ad6
Showing 1 changed file with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@ public MySQL(JavaPlugin javaPlugin){
}

@Override
public Connection getConnection() throws SQLException{

public Connection getConnection() throws SQLException {
if (this.connection != null && !this.connection.isClosed() && this.connection.isValid(10)) {
return this.connection;
}

try {
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver"); // 修改此处加载驱动程序的方式
this.connection = DriverManager.getConnection(
"jdbc:mysql://" + Config.MySQL.Host + ":" + Config.MySQL.Port + "/" + Config.MySQL.Database + "?characterEncoding=UTF-8",
Config.MySQL.User, Config.MySQL.Password
);
return this.connection;
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | SQLException e) { // 增加SQLException处理
e.printStackTrace();
return null;
}

}

}

0 comments on commit bb68ad6

Please sign in to comment.