Auto-incrementation with HSQLDB (2.2.8) + DDLUtils

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 16:49:26

Here's an example that prints out

0
1
2

on my machine:

import java.io.File;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Date;

public class Test {

  public static void main(String[] args) throws Exception {

    File dbDir = new File("/tmp/identity_test"); 
    String connectionTemplate = "jdbc:hsqldb:file:%s/test";
    String connStr = String.format(connectionTemplate, dbDir);
    Connection connection = DriverManager.getConnection(connStr, "", "");
    Statement s = connection.createStatement();
    s.execute("CREATE TABLE test (id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, s VARCHAR(10))");
    PreparedStatement psInsert = connection.prepareStatement("INSERT INTO test (s) VALUES (?)");
    for (int i = 0; i < 3; i++) {
      psInsert.setString(1, "hello");
      psInsert.executeUpdate();
      PreparedStatement psIdentity = connection.prepareStatement("CALL IDENTITY()");
      ResultSet result = psIdentity.executeQuery();
      result.next();
      int identity = result.getInt(1);
      result.close();
      System.out.println(identity);
    }
    connection.close();
  }
}

If you use an ORM, they will perform the identity column work for you. sormula makes it easy with an annotation. See org.sormula.tests.identity package within the project for examples.

Row class defined:

public class IdentityTest
{
    @Column(identity=true)
    int id;
    ...

From org.sormula.identity.tests.InsertTest:

 IdentityTest row = new IdentityTest(-1, "Insert one");
 assert getTable().insert(row) == 1 : "insert one failed";
 assert row.getId() > 0 : "indentity column was not generated";

HSQLDB is included in the tests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!