我正在尝试在我的javafx应用程序中使用sqlite数据库,但是当我对其执行任何操作时,数据库上什么也没有发生,而且我也没有收到任何错误...
主要:
public void start(Stage primaryStage) throws IOException {
Main.primaryStage = primaryStage;
primaryStage.setTitle("Welcome to ServerDashboard");
primaryStage.setOnCloseRequest(event -> {
event.consume();
onClose();
});
MainConfig.load();
DatabaseController.createNewDatabase("data.db");
MainMenu.show(primaryStage);
}
数据库控制器:
public class DatabaseController {
public static void createNewDatabase(String fileName) {
String url = "jdbc:sqlite:" + fileName;
String sql = "CREATE TABLE IF NOT EXISTS warehouses (\n"
+ " id integer PRIMARY KEY,\n"
+ " name text NOT NULL,\n"
+ " capacity real\n"
+ ");";
try (Connection conn = DriverManager.getConnection(url)) {
if (conn != null) {
DatabaseMetaData meta = conn.getMetaData();
System.out.println("The driver name is " + meta.getDriverName());
System.out.println("The file was created in : " + new File("").getAbsolutePath() + "\\" + fileName);
System.out.println("A new database has been created.");
Statement stmt = conn.createStatement();
stmt.execute(sql);
System.out.println("A new table has been created");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
} 输出:
The driver name is SQLite JDBC
The file was created in : N:\Code\Minecraft Java\ServerDashboard\data.db
A new database has been created.
A new table has been created
我尝试了所有可以找到的教程,但似乎都没有效果...
答案 0 :(得分:0)
我有一个小项目可以建立sqlite连接,也许可以帮助您找到错误。如果不是,请尝试调试脚本以隔离查询语句。它总是有帮助的。
https://github.com/saaimons/Java/tree/master/pyMultas_SQLite3
祝你好运