How to use PostgreSQL nextval with JDBC

PostgreSQLにあるnextvalの値をJavaで取得する方法を調べた。ResultSetでは結果がとれないので、Arrayを使用した。

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.Statement;

public class Util {
  public static int getNextVal(Statement st) throws Exception {
    int id = –1;
    ResultSet rs = st.executeQuery("select nextval('SequenceName')");
    if (rs.next()) {
      Array os = rs.getArray(1);
      String sId = os.toString();
      id = Integer.parseInt(sId);
    }
    return id;
  }
}

How to use PostgreSQL nextval with JDBC」への1件のフィードバック

コメントは停止中です。

同じカテゴリの記事: Java