Search code examples
sqljdbcderby

Check if Auto Increment exist in a table in derby database


I have created a table in Derby Database with Auto Increment.

   For example: 
   CREATE TABLE USERS (ID BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) , USER_NAME)

I have to check if the id column detail now,

String query = "SELECT ID FROM USERS ";
conn = new Kanexon().db();
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
rsmd = rs.getMetaData();

if(rs.next()){
    System.out.println("ColumnLabel: "+rsmd.getColumnLabel(1));
    System.out.println("ColumnName: "+rsmd.getColumnName(1));
    System.out.println("ColumnTypeName: "+rsmd.getColumnTypeName(1));
}

It gives me output something like this:

ColumnLabel: ID
ColumnName: ID
ColumnTypeName: BIGINT

Is there a way by which I can check that the ID column is auto incremented or not.


Solution

  • To find out if the column is auto incremented, use the isAutoIncrement method on ResultSetMetadata: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSetMetaData.html#isAutoIncrement(int)