java - 如何正确关闭 derby 数据库?
问题描述
我有一个 JavaFX 应用程序,它使用嵌入式数据库来存储所有数据。除了“id”列自动递增 100 而不是 1 之外,建立连接和插入数据工作得很好。我读过(嵌入式 Derby/Java DB 中的自动递增错误)我必须使用以下方法正确关闭数据库我的主要课程中的代码:
@Override
public void stop() {
try {
DriverManager.getConnection("jdbc:derby:"+"db"+";shutdown=true");
}
catch (SQLException sqlException){
sqlException.printStackTrace();
System.out.println("Database shutted down...");
}
}
但是它仍然增加了 100,我不知道为什么。
我建立连接和插入的代码:
try {
Connection connection = DriverManager.getConnection("jdbc:derby:db");
connection.setAutoCommit(true);
System.out.println("Connection established");
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO orders(order_name, order_date, price) VALUES (?, ?, ?)");
preparedStatement.setString(1, "TestOrder");
preparedStatement.setDate(2, Date.valueOf(LocalDate.now()));
preparedStatement.setDouble(3, 1.1);
preparedStatement.execute();
preparedStatement.close();
connection.close();
} catch (SQLException throwables) {
System.out.println("Unable to establish connection");
throwables.printStackTrace();
}
和我创建表的 SQL 语句
DROP TABLE orders;
CREATE TABLE orders(
order_number INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1, INCREMENT BY 1),
order_name VARCHAR(100),
order_date DATE,
price DECIMAL(4, 2));
解决方案
推荐阅读
- angular - 刷新后路由总是重定向到主页组件,我该如何阻止这种情况发生?
- blockchain - 索引币安智能链上的相关区块
- django - 如何使用tenant_schemas 为 Django 应用程序创建写入和读取副本
- laravel - 我在 if else 语句中哪里出错了?
- flutter - Flutter 应用程序在调试构建时完美运行,但无法构建为发布版本
- jenkins - Jenkins:当我们期望 0 时,批处理命令返回负值
- python - 从 API 将奇怪的 NBA 数据结构转换为 pandas 数据帧
- php - 如何设置动态最大输入日期?
- amazon-web-services - AWS ElasticBeanstalk ebextensions 命令在部署期间失败,但在我手动运行时工作正常
- flask - 获取“ValueError:附件太多”