Skip to content

Commit bb68ad6

Browse files
committed
修复MySQL驱动加载并优化连接获取逻辑,同时处理异常。
1 parent 38ddf7f commit bb68ad6

File tree

1 file changed

+3
-5
lines changed
  • src/main/java/cc/baka9/catseedlogin/bukkit/database

1 file changed

+3
-5
lines changed

src/main/java/cc/baka9/catseedlogin/bukkit/database/MySQL.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@ public MySQL(JavaPlugin javaPlugin){
1515
}
1616

1717
@Override
18-
public Connection getConnection() throws SQLException{
19-
18+
public Connection getConnection() throws SQLException {
2019
if (this.connection != null && !this.connection.isClosed() && this.connection.isValid(10)) {
2120
return this.connection;
2221
}
2322

2423
try {
25-
Class.forName("com.mysql.jdbc.Driver");
24+
Class.forName("com.mysql.cj.jdbc.Driver"); // 修改此处加载驱动程序的方式
2625
this.connection = DriverManager.getConnection(
2726
"jdbc:mysql://" + Config.MySQL.Host + ":" + Config.MySQL.Port + "/" + Config.MySQL.Database + "?characterEncoding=UTF-8",
2827
Config.MySQL.User, Config.MySQL.Password
2928
);
3029
return this.connection;
31-
} catch (ClassNotFoundException e) {
30+
} catch (ClassNotFoundException | SQLException e) { // 增加SQLException处理
3231
e.printStackTrace();
3332
return null;
3433
}
35-
3634
}
3735

3836
}

0 commit comments

Comments
 (0)