首页 > 解决方案 > 如何正确关闭 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));

标签: javajavafxjdbcderby

解决方案


推荐阅读