我的 SQLite 数据库有问题。直截了当地说:除查询外,一切正常(执行/创建表/ ...)。
重要提示:autoCommit = False(但我使用的查询功能不需要它)
这是代码:
---=> 数据库.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 数据库编辑器检查了我的数据库,并且所有插入都已完成。但是当我试图显示它们时,什么都没有显示。
请帮我!提前致谢。
真诚的,马克斯