From 4b40a33bc74128bb09ad4ab8617a78d41bba70e0 Mon Sep 17 00:00:00 2001 From: Shuiling <132723284+shulng@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:42:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20SQLite.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../catseedlogin/bukkit/database/SQLite.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/main/java/cc/baka9/catseedlogin/bukkit/database/SQLite.java b/src/main/java/cc/baka9/catseedlogin/bukkit/database/SQLite.java index 70f79b0..b72fd84 100644 --- a/src/main/java/cc/baka9/catseedlogin/bukkit/database/SQLite.java +++ b/src/main/java/cc/baka9/catseedlogin/bukkit/database/SQLite.java @@ -1,7 +1,6 @@ package cc.baka9.catseedlogin.bukkit.database; import org.bukkit.plugin.java.JavaPlugin; - import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -9,30 +8,29 @@ public class SQLite extends SQL { private Connection connection; - public SQLite(JavaPlugin javaPlugin){ - super(javaPlugin); + public SQLite(JavaPlugin plugin) { + super(plugin); } - - @Override - public Connection getConnection() throws SQLException{ - + public Connection getConnection() { if (this.connection != null && !this.connection.isClosed()) { return this.connection; } - if (plugin.getDataFolder().exists()) { - try { - Class.forName("org.sqlite.JDBC"); - this.connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/accounts.db"); - return this.connection; - } catch (ClassNotFoundException | SQLException e) { - e.printStackTrace(); - return null; - } - } else { - final boolean mkdir = plugin.getDataFolder().mkdir(); - return this.getConnection(); + + // 确保插件的数据文件夹存在 + if (!plugin.getDataFolder().exists()) { + plugin.getDataFolder().mkdir(); + } + + try { + // 加载SQLite的JDBC驱动 + Class.forName("org.sqlite.JDBC"); + // 创建数据库连接 + this.connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/accounts.db"); + } catch (ClassNotFoundException | SQLException e) { + e.printStackTrace(); } - } + return this.connection; + } } \ No newline at end of file