Skip to content

Commit

Permalink
feat(database): 优化SQLite数据库连接获取方式
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed Jul 9, 2024
1 parent ef237fe commit 35e5b5d
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/main/java/cc/baka9/catseedlogin/bukkit/database/SQLite.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
package cc.baka9.catseedlogin.bukkit.database;

import org.bukkit.plugin.java.JavaPlugin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.bukkit.plugin.java.JavaPlugin;

public class SQLite extends SQL {
private Connection connection;

public SQLite(JavaPlugin javaPlugin){
super(javaPlugin);
}


@Override
public Connection getConnection() {
try {
if (this.connection != null && !this.connection.isClosed()) {
return this.connection;
}

if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdir();
public Connection getConnection() {
try {
if (connection == null || connection.isClosed()) {
if (!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdir();
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/accounts.db");
}
return connection;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null;
}

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;
}
}

}

0 comments on commit 35e5b5d

Please sign in to comment.