Apache Derby ID列应该逐个自动递增 - 但它没有,为什么?

时间:2015-01-01 02:18:22

标签: java apache jdbc derby

我正在尝试创建Apache Derby-Table并通过JDBC接口在其中插入数据。

以下是我的实施的简短摘录:

public class SQLDatabase {

    private Connection connection = null;
    public final static String HOME_DIRECTORY = System.getProperty("user.home");
    public final static String TABLE_NAME = "PORPHYRIE";

    public SQLDatabase() {

        setConnection();
        if (!(isTableExisting(TABLE_NAME))) {

            createTable();
        }

    }




    // OTHER

    public void createTable() {
        String statement = "CREATE TABLE PORPHYRIE("
                + "ID int NOT NULL GENERATED ALWAYS AS IDENTITY"
                + "(START WITH 1,INCREMENT BY 1)," + "ENTRYDATE DATE NOT NULL,"
                + "DRUGTYPE varchar(255) NOT NULL,"
                + "UPPERLIMIT DOUBLE NOT NULL," + "NORMOSANG BOOLEAN NOT NULL,"
                + "MENSTRUATION BOOLEAN NOT NULL,"
                + "DRUGAMOUNT_MG DOUBLE NOT NULL,"
                + "DRUGAMOUNT_ML DOUBLE NOT NULL,"
                + "AMPOULE_NUMBERS DOUBLE NOT NULL,"
                + "RECORDDATE DATE NOT NULL," + "PRIMARY KEY(ID)" + ")";
        updateStatement(statement);
    }

    public void dropTable() {
        String statement = "DROP TABLE PORPHYRIE";
        updateStatement(statement);
    }

    public void addData(EntryPoint entry) {
        DrugAmount drugAmount = entry.getDrugAmountObject();
        SimpleDateFormat form = new SimpleDateFormat("dd.MM.yyyy");
        String statement = "INSERT INTO PORPHYRIE (ENTRYDATE,DRUGTYPE,UPPERLIMIT,NORMOSANG,MENSTRUATION,DRUGAMOUNT_MG,DRUGAMOUNT_ML,AMPOULE_NUMBERS,RECORDDATE)"
                + " values('"
                + form.format(entry.getEntryDate().getTime())
                + "','"
                + entry.DRUG_TYPE_DOLANTIN
                + "',"
                + entry.DRUG_UPPER_LIMIT
                + ","
                + entry.getNormoSang()
                + ","
                + entry.getMenstruation()
                + ","
                + drugAmount.getAmpoulesInMG()
                + ","
                + drugAmount.getAmpoulesInML()
                + ","
                + drugAmount.getNumberOfAmpoules() + ",CURRENT_DATE)";
        System.out.println("Add data!");
        updateStatement(statement);
    }


   private void setConnection() {
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            connection = DriverManager.getConnection("jdbc:derby:"
                    + HOME_DIRECTORY + "\\MyDB;create=true");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    private void updateStatement(String statementString) {
        try {
            PreparedStatement preparedStatement = getConnection()
                    .prepareStatement(statementString);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

这就是我得到的:

enter image description here

ID列的问题是什么?

第一次创建SQL数据库对象时,应该创建表,这样可以正常工作。 然后你应该能够通过addData() - 方法插入一些数据,这看起来也很好。 但是如果你创建一个新的SQL-Database对象并在其中使用addData-method(),那么它会添加数据(上图中的第五行),但不能正确地自动递增。这有什么不对?

3 个答案:

答案 0 :(得分:2)

生成的序列号中的间隙是Derby的正确记录行为。

例如,请参阅https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html

并且:https://issues.apache.org/jira/browse/DERBY-5151

答案 1 :(得分:0)

正如布莱恩所描述的那样,这个错误是众所周知的。但好的是你可以通过使用:

来解决它

DriverManager.getConnection("jdbc:derby:;shutdown=true");

这将关闭您在derby中拥有的所有数据库,您始终可以指定要关闭的数据库。

DriverManager.getConnection("jdbc:derby:" + HOME_DIRECTORY + "\\MyDB;shutdown=true")

答案 2 :(得分:0)

问题是我们必须在注销或关闭应用程序时关闭derby db,然后只会正确插入值,否则缓存值会阻止数据库表中的确切插入值。

按照以下步骤操作:

con = DriverManager.getConnection(“jdbc:derby:”+“db”+“; create = true”); - >这是用于通过提及“db”name来加载derby db。这个代码我们必须放置在您的应用程序登录操作中。

DriverManager.getConnection(“jdbc:derby:”+“db”+“; shutdown = true”); - >这是用于关闭数据库'db'。我们必须将此代码放在您的应用程序注销操作中或关闭地点。

注意:这适用于嵌入式数据库