View Single Post

   
  #5 (permalink)  
Old 02-26-2008, 06:40 PM
Ian
 
Posts: n/a
Default Re: [JDBC] PreparedStatement and identity column

Dov Moryusef wrote:

> Hi to all,
>
> I’ve a table with an idendity column :
>
> CREATE TABLE USRDB2.MYTABLE (
> "ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0,
> INCREMENT BY 1, NO CACHE),
> "CODE" CHARACTER (3),
> PRIMARY KEY (ID))IN MYTS01@
>
> I would like to execute the following request (which works in a db2cmd):
>
> db2 insert into usrdb2.mytable (id, code) values (DEFAULT, 'AD') @
>
> with JDBBC:
>
> String REQUEST = "insert into usrdb2.mytable (id, code) values (?, ?)";
> PreparedStatement stmt = connection. prepareStatement(REQUEST);
> stmt.setInt(1,...);
> stmt.setString(2, 'AD');
> stmt.execute();
>
> but I don't know how. I got the error SQL0798N : A value cannot be
> specified for column "ID" which is defined as GENERATED ALWAYS


DEFAULT is a key word, so you need to include it in your statement --
it is not a value that you use when you execute a prepared statement.
Therefore,

String REQUEST = "insert into usrdb2.mytable (id, code) values (DEFAULT, ?)";
PreparedStatement stmt = connection. prepareStatement(REQUEST);
stmt.setString(1, 'AD');
stmt.execute();


Or, as Knut suggested, you can just avoid the column 'id' in your
insert statement entirely.


Good luck,



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Reply With Quote