Search code examples
sqljdbcprepared-statementdefaultucanaccess

JDBC PreparedStatement default values on Insert (UCanAccess)


I was wondering how I can fetch the default values of newly inserted rows of MS Access via UCanAccess.

Table: my_tbl

Column   Type
-------+-----------------------
ID     | PrimaryKey, AutoNumber
Label  | Text
DT     | Date/Time, Default: Now()

Code

PreparedStatement st = conn.prepareStatement("INSERT INTO my_tbl (Label) VALUES (?)", Statement.RETURN_GENERATED_KEYS);
st.setString(1, "my new label");

int insertResult = st.executeUpdate();
if(insertResult > 0) {
    ResultSet rs = st.getGeneratedKeys();
    rs.next();
    System.out.println("ID: " + rs.getInt(1));
}

//How do I get the generated value of DT

Limitations

I cannot use the keyword DEFAULT because UCanAccess throws an SQLException (SO discussion about DEFAULT):

Caused by: org.hsqldb.HsqlException: DEFAULT keyword cannot be used as column has no DEFAULT

Do you have another solution for me?


Solution

  • How do I get the generated value of DT

    You will need to use the value returned by rs.getInt(1) to execute another SELECT query of the form

    SELECT DT FROM my_tbl WHERE ID = ?
    

    and then retrieve the value from the ResultSet produced by calling executeQuery for that query.