Hi Stefan,
After researching the javadoc on Statement, I think I've found the culprit. It's nothing to do with the Statement, it's with the ResultSet.
ResultSet columns are 1-origin not the normal Java 0-origin. Try:
genIds = stmt.getGeneratedKeys(); modifiedId = genIds.getInt(1); // 1-origin
Craig
On Apr 4, 2005, at 12:52 AM, Stefan Marr wrote:
Hi,
I'm trying to get a little practice using derby with JDBC. So I've begin to do some "standard" SQL stuff.
Unfortunately I get in trouble with my real first INSERT-statement.
I'd like to have an auto-increment column for ids and get the id from the
done insert.
http://incubator.apache.org/derby/manuals/reference/sqlj229.html
or
http://publib.boulder.ibm.com/infocenter/cldscp10/index.jsp?topic=/ com.ibm.c
loudscape.doc/sqlj229.htm
told me, this should be possible.
My test database is set up with the following lines:
stmt.execute("CREATE TABLE personen (person_id INTEGER NOT NULL GENERATED
ALWAYS AS IDENTITY, person_name VARCHAR(100), person_ort VARCHAR(32),
PRIMARY KEY (person_id))");
stmt.execute("CREATE TABLE daten (datum_id INTEGER NOT NULL GENERATED ALWAYS
AS IDENTITY, person_id INTEGER, datum_value TIMESTAMP, datum_typ SMALLINT,
PRIMARY KEY (datum_id))");
Now I'm trying to insert:
stmt.execute("insert into personen (person_name, person_ort) values ('peter', 'london')", Statement.RETURN_GENERATED_KEYS);
And get the Id:
genIds = stmt.getGeneratedKeys(); modifiedId = genIds.getInt(0);
On the last line I'm getting an error instead of the value :( I've also tried genIds.getInt("person_id");
In the documentation is written an array with name or column index should
work to, so I'd try the following:
String[] colIndexes = new String[1]; colIndexes[0] = "person_id";
or
int[ ] colIndexes = new int[1]; colIndexes[0] = 1; //or 0
stmt.execute("insert into personen (person_name, person_ort) values ('peter', 'london')", colIndexes);
But now I'm getting a "not implemented".
Any suggestions?
May be the tables aren't set up right?
Kind Regards Stefan Marr
Craig Russell Architect, Sun Java Enterprise System http://java.sun.com/products/jdo 408 276-5638 mailto:[EMAIL PROTECTED] P.S. A good JDO? O, Gasp!
smime.p7s
Description: S/MIME cryptographic signature
