Unix Technical Forum

[JDBC] PreparedStatement and identity column

This is a discussion on [JDBC] PreparedStatement and identity column within the DB2 forums, part of the Database Server Software category; --> Hi to all, I’ve a table with an idendity column : CREATE TABLE USRDB2.MYTABLE ( "ID" INTEGER NOT NULL ...


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-26-2008, 05:39 PM
Dov Moryusef
 
Posts: n/a
Default [JDBC] PreparedStatement and identity column

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

Thanks a lot for your answers

Dov
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-26-2008, 05:39 PM
Knut Stolze
 
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


As the message says, you cannot provide any value for the "id" column, not
even DEFAULT. Change your statement to this:

INSERT
INTO usrdb2.mytable(code)
VALUES ('AD')@

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-26-2008, 05:39 PM
Dov Moryusef
 
Posts: n/a
Default Re: [JDBC] PreparedStatement and identity column

Thanks Knut

>
>
> As the message says, you cannot provide any value for the "id" column, not
> even DEFAULT. Change your statement to this:
>
> INSERT
> INTO usrdb2.mytable(code)
> VALUES ('AD')@
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-26-2008, 05:39 PM
Serge Rielau
 
Posts: n/a
Default Re: [JDBC] PreparedStatement and identity column

Actually DEFAULT is allowed. DEFAULT says:
"Dear DB2, plug in what you believe is right"
For a generated column (identity or expression) that means DB2 produces
the appropriate value.
For any other updatable column it looks up the default value for that
column.
Only for pure expressions as a target does DB2 raise an error today.
The reason for this are two fold:
1. It saves you from listing all the columns on INSERT just to handle
generated columns
2. You can specify:
SET <identity_col> = DEFAULT
in an UPDATE, MERGE statement or a before trigger.
The result is that the identity value will be regenerated.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-26-2008, 05: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! =-----
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-26-2008, 05:40 PM
Dov Moryusef
 
Posts: n/a
Default Re: [JDBC] PreparedStatement and identity column

Thanks a lot. That works !!

Dov
"Ian" <ianbjor@mobileaudio.com> a écrit dans le message de
news:4074aa53_1@corp.newsgroups.com...
> 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! =-----



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:24 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com