我的SQLite数据库出了问题。为了实现这一点:除了查询之外,一切正常(执行/创建表/ ...)。
重要:autoCommit = False(但我使用的查询功能不需要它)
以下是代码:
--- => Database.class< = ---
package me.Syloh.Core;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import me.Syloh.Core.Customized.LOG;
public class Database {
public static Connection connection;
public static String path = "plugins/Core.db";
public static void setup() throws SQLException, ClassNotFoundException {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + path);
connection.setAutoCommit(false);
DatabaseMetaData metadata = connection.getMetaData();
ResultSet resultset = metadata.getTables(null, null, "%", null);
List<String> tables = new ArrayList<String>();
while(resultset.next()) {
tables.add(resultset.getString(3));
LOG.info("Table '" + resultset.getString(3) + "' has been found.");
}
if(!tables.contains("Bans")) {
execute("CREATE TABLE Bans (player TEXT, date TEXT, sender TEXT, reason TEXT, ip TEXT, time DOUBLE)");
LOG.info("Table 'Bans' has been created.");
}
}
public static void execute(String execute) {
try {
Statement statement = connection.createStatement();
statement.execute(execute);
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void execute(List<String> execute) {
try {
Statement statement = connection.createStatement();
for(String e : execute) { statement.execute(e); }
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static ResultSet query(String query) {
try {
Statement statement = connection.createStatement();
ResultSet resultset = statement.executeQuery(query);
statement.close();
return resultset;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void commit() {
try {
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
---=> Main.class <=---
package me.Syloh.Core;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
Plugin Core = this;
@Override
public void onDisable() {
Database.commit();
}
@Override
public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(this, this);
try {
Database.setup();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Core, new Runnable() {
public void run() {
Database.commit();
}
}, 60L, 4L);
Database.execute("INSERT INTO Bans VALUES ('hI', 'a', 'b', 'c', 'd', 1)");
ResultSet rs = Database.query("SELECT * FROM Bans WHERE player = 'hi'");
try {
while(rs.next()) {
System.out.println(rs.getString("player"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我使用SQL数据库编辑器检查了我的数据库并完成了所有插入操作。但是当我试图显示它们时,根本没有任何东西显示出来。
请帮帮我!提前谢谢。
此致 最大
答案 0 :(得分:2)
问题是,您正在关闭statement
,这会自动关闭相关的ResultSet
:
Statement statement = connection.createStatement();
ResultSet resultset = statement.executeQuery(query);
statement.close(); // This is the problem
return resultset;
来自Statement#close()
javadoc:
当Statement对象关闭时,它的当前ResultSet对象(如果存在)也将关闭。
话虽如此,你绝不应该围绕你的方法传递你的ResultSet
对象。这样就很难管理资源,因为您必须保持Statement
和ResultSet
对象在各种方法中打开。
您应该使用获取ResultSet
的相同方法构建一些数据结构,并返回该数据结构。
此外,您应关闭ResultSet
块中的Statement
,Connection
和finally
等资源,以确保它们始终处于关闭状态,即使在例外情况。