Skip to content

Commit 35e5b5d

Browse files
committed
feat(database): 优化SQLite数据库连接获取方式
1 parent ef237fe commit 35e5b5d

File tree

1 file changed

+13
-21
lines changed
  • src/main/java/cc/baka9/catseedlogin/bukkit/database

1 file changed

+13
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
package cc.baka9.catseedlogin.bukkit.database;
22

3-
import org.bukkit.plugin.java.JavaPlugin;
4-
53
import java.sql.Connection;
64
import java.sql.DriverManager;
75
import java.sql.SQLException;
86

7+
import org.bukkit.plugin.java.JavaPlugin;
8+
99
public class SQLite extends SQL {
1010
private Connection connection;
1111

1212
public SQLite(JavaPlugin javaPlugin){
1313
super(javaPlugin);
1414
}
1515

16-
1716
@Override
18-
public Connection getConnection() {
19-
try {
20-
if (this.connection != null && !this.connection.isClosed()) {
21-
return this.connection;
22-
}
23-
24-
if (!plugin.getDataFolder().exists()) {
25-
plugin.getDataFolder().mkdir();
17+
public Connection getConnection() {
18+
try {
19+
if (connection == null || connection.isClosed()) {
20+
if (!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdir();
21+
Class.forName("org.sqlite.JDBC");
22+
connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/accounts.db");
23+
}
24+
return connection;
25+
} catch (ClassNotFoundException | SQLException e) {
26+
e.printStackTrace();
27+
return null;
2628
}
27-
28-
Class.forName("org.sqlite.JDBC");
29-
this.connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/accounts.db");
30-
31-
return this.connection;
32-
} catch (ClassNotFoundException | SQLException e) {
33-
e.printStackTrace();
34-
return null;
3529
}
3630
}
37-
38-
}

0 commit comments

Comments
 (0)