I'm trying to create an Apache Derby-Table and to insert data in it by the JDBC-interface.
Here is a short excerpt of my implementation:
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();
}
}
This is what I get:

What is here the problem with the ID column?
The first time when a SQL-Database object is created, is should create the table, which works fine. Then you should able to insert some data by the addData()-method, which also looks fine. But if you create an new SQL-Database Object and use the addData-method() within, then it adds the data (row five in the picture above) but do not auto-increment correctly. What is wrong here?
Gaps in the generated sequence numbers are a correct and documented behavior of Derby.
See, for example, https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html
As Described by Bryan, this error is well known. But good thing is you can solve it by using:
DriverManager.getConnection("jdbc:derby:;shutdown=true");
This will shutdown all the databases that you have in derby, you can always specify the database you want to shutdown.
DriverManager.getConnection("jdbc:derby:" + HOME_DIRECTORY + "\\MyDB;shutdown=true")
the problem is we have to shut down the derby db whenever we are sign out or closing our application,then only the values will be inserted properly otherwise the cache values prevents the exact insertion value in database table.
follow below steps:
con = DriverManager.getConnection("jdbc:derby:" + "db" + ";create=true");->this is for loading derby db by mentioning 'db' name.this code we have to place in your application login action.
DriverManager.getConnection("jdbc:derby:"+"db"+";shutdown=true");->this is for closing the database 'db'.we have to place this code in your application logout action or closing place.
note : This is for Embedded DB
来源:https://stackoverflow.com/questions/27727863/apache-derby-id-column-should-auto-increment-one-by-one-but-it-does-not-why