70's futuristic technology

Programming focused drivel

Saturday, February 05, 2005

More MySQl2Derby notes:
Column Names

ResultSet rs = stmt.execute("SELECT COUNT(id) FROM tableName;");

if( rs.next() ){
num = rs.getInt("COUNT(id)");
}

fails for Derby. You must use
rs.getInt(1); 

instead.

Getting last autoincrement value

stmt.execute("INSERT INTO tableName VALUES (LAST_INSERT_ID(), 'foo')");

There is no LAST_INSERT_ID() function, so in Derby you might try adding a new select statement and then using the results in later statements.
SELECT MAX(id) FROM tableName

and then set that value into a Java variable lastId to use in you INSERT statment.
Of course you want to turn autocommit off for executing multiple statement.

1 Comments:

  • At 10:09 AM , Anonymous Anonymous said...

    Now Derby has a built-in function called IDENTITY_VAL_LOCAL(). The statement

    SELECT IDENTITY_VAL_LOCAL() FROM table;

    takes care of retrieving the last auto-generated key.

     

Post a Comment

Links to this post:

Create a Link

<< Home