File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes inserting  a row when there is a sequence in that table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "inserting  a row when there is a sequence in that table" Watch "inserting  a row when there is a sequence in that table" New topic
Author

inserting a row when there is a sequence in that table

Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

Hi,

What is the best way to insert a row in a table that has a column which values are based on a sequence (my_seq.nextval) ?
In SQL, that would be:

insert into mytable (toto, message) values (maseq.nextval, "hello");
but how do you handle the nextval in a JDBC statement ?
Can anyone please provide me with some sample code to do that ?

Thanks !
[ August 16, 2005: Message edited by: Max longbeach ]

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

I usually define the default value for the column to be the nextval function and let it default to that upon an insert.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dave Salter
Ranch Hand

Joined: Jul 20, 2005
Posts: 292

Try using a PreparedStatement with SQL of the format:





All you need to do then is set the value of the message field in the prepared statement.
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

hehe that was really easy then. Thanks a lot to both of you, you saved me some precious time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inserting a row when there is a sequence in that table