473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

[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
Nov 12 '05 #1
5 8405
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
Nov 12 '05 #2
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')@

Nov 12 '05 #3
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
Nov 12 '05 #4
Ian
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! =-----
Nov 12 '05 #5
Thanks a lot. That works !!

Dov
"Ian" <ia*****@mobileaudio.com> a écrit dans le message de
news:40********@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! =-----

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dani | last post by:
Hi everyone Description of the problem: Using a PreparedStatement to write down an integer (int) plus a timestamp for testing purposes. When read out again the integer looks very different. We...
3
by: aj | last post by:
Red Hat AS 2.1 DB2 WSE v8.1 FP5 Type 4 DB2 JDBC driver Using java.sql.DatabaseMetaData and ResultSetMetaData, I've written a JDBC/Java utility that shows, table by table, columns & their...
18
by: gimme_this_gimme_that | last post by:
I'm driving Weblogic 8.1.3 on WinTel. DB2 8.1.4 on Sun64. I'm using Weblogic DB2's Type 2 driver, which has been very reliable - so far. I have a PreparedStatement that updates perfectly when...
6
by: Jim Steinberger | last post by:
Hey all - my inaugural post, I have a Java web application on Tomcat 5 using pg73jdbc3.jar to connect to PostgreSQL 7.3.4 running on the same server (Red Hat Linux ). I have a Perl...
2
by: Diego | last post by:
Hi everybody! I'm using DB2 PE v8.2.3 for linux. I've defined a database with the following schema: ANNOTATION(ID,AUTHOR,TEXT) ANNOTATION_BOOK(ANNOTATION_ID,OBJECT_ID)...
1
by: sbanil | last post by:
We have a JAVA application which uses following Database product name : DB2 Database product version : DSN07012 JDBC driver name : IBM DB2 JDBC Universal Driver Architecture JDBC driver version ...
1
by: tom.eeraerts | last post by:
Hello, I have a problem migrating an application from v5r2 to v5r3. The problem is with the prepared statements. To see what the problem is, i extracted a small piece of code and debugged the...
0
by: princego1 | last post by:
I 've got a table ,and I want to check for a unique column ,so that there is no dups in my mysql. So how is it done using jdbc ? This is my part code / Create PreparedStatement object String...
2
by: Zunil | last post by:
Hi All, We are testing one of our component with JDBC + DB2 using db2jcc driver. We afced a strange issue. Scenario is like this. 1. We insert 1 record to a table having IDENTITY column as the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.