Apache Derby ID 列应该自动一一递增 - 但它不会,为什么?
Apache Derby ID column should auto increment one by one - but it does not, why?
我正在尝试创建一个 Apache Derby-Table 并通过 JDBC-interface 在其中插入数据。
以下是我的实施的一小段摘录:
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();
}
}
这是我得到的:
ID 列有什么问题?
第一次创建 SQL-Database 对象时,应该创建 table,效果很好。
然后您应该能够通过 addData() 方法插入一些数据,这看起来也不错。
但是,如果您创建一个新的 SQL-Database 对象并在其中使用 addData-method(),那么它会添加数据(上图中的第五行)但不会正确地 auto-increment。这里有什么问题?
生成的序列号中的间隙是 Derby 的正确行为并记录在案。
参见,例如,https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html
正如 Bryan 所描述的,这个错误是众所周知的。但好消息是您可以使用以下方法解决它:
DriverManager.getConnection("jdbc:derby:;shutdown=true");
这将关闭您在 derby 中的所有数据库,您始终可以指定要关闭的数据库。
DriverManager.getConnection("jdbc:derby:" + HOME_DIRECTORY + "\MyDB;shutdown=true")
问题是每当我们注销或关闭我们的应用程序时,我们都必须关闭 derby 数据库,然后只有正确插入值,否则缓存值会阻止数据库中的准确插入值 table .
按照以下步骤操作:
con = DriverManager.getConnection("jdbc:derby:" + "db" + ";create=true");->这是为了通过提及 'db' name.this 我们必须在您的应用程序登录操作中放置的代码。
DriverManager.getConnection("jdbc:derby:"+"db"+";shutdown=true");->这是为了关闭数据库'db'。我们必须把这个您的应用程序注销操作或关闭位置中的代码。
注意:这适用于嵌入式数据库
我正在尝试创建一个 Apache Derby-Table 并通过 JDBC-interface 在其中插入数据。
以下是我的实施的一小段摘录:
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();
}
}
这是我得到的:
ID 列有什么问题?
第一次创建 SQL-Database 对象时,应该创建 table,效果很好。 然后您应该能够通过 addData() 方法插入一些数据,这看起来也不错。 但是,如果您创建一个新的 SQL-Database 对象并在其中使用 addData-method(),那么它会添加数据(上图中的第五行)但不会正确地 auto-increment。这里有什么问题?
生成的序列号中的间隙是 Derby 的正确行为并记录在案。
参见,例如,https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html
正如 Bryan 所描述的,这个错误是众所周知的。但好消息是您可以使用以下方法解决它:
DriverManager.getConnection("jdbc:derby:;shutdown=true");
这将关闭您在 derby 中的所有数据库,您始终可以指定要关闭的数据库。
DriverManager.getConnection("jdbc:derby:" + HOME_DIRECTORY + "\MyDB;shutdown=true")
问题是每当我们注销或关闭我们的应用程序时,我们都必须关闭 derby 数据库,然后只有正确插入值,否则缓存值会阻止数据库中的准确插入值 table .
按照以下步骤操作:
con = DriverManager.getConnection("jdbc:derby:" + "db" + ";create=true");->这是为了通过提及 'db' name.this 我们必须在您的应用程序登录操作中放置的代码。
DriverManager.getConnection("jdbc:derby:"+"db"+";shutdown=true");->这是为了关闭数据库'db'。我们必须把这个您的应用程序注销操作或关闭位置中的代码。
注意:这适用于嵌入式数据库