aspose file tools*
The moose likes JDBC and the fly likes Auto Increment ID Field Table in the Oracle Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Auto Increment ID Field Table in the Oracle Database" Watch "Auto Increment ID Field Table in the Oracle Database" New topic
Author

Auto Increment ID Field Table in the Oracle Database

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have been using the MySQL. And the ID field of the database table is AUTO INCREMENT. When I insert a new record into a database table, I can have a statement like:

and I do not have to put the ID variable in the above method. The table will give the new record an ID number that is equivalent to the ID number of the last record plus one automatically.

Now, I am inserting a new record into an Oracle database table. I am told that I cannot do what I am used to doing with the MySQL database.

How do I revise the createThread method while I have no idea about what the next sequence number shall be?
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
OKay, the table into which I am trying to insert new records has a sequence: dhsinfo_content-grp_seq

The DBA has done that part. I am worrying about the Java part. To give you an example the origin of my confusion, I have:


All the parameters of the updateContent() methods have known values. Where in the code we use that sequence key for INSERT? Do I insert new record this way:


[edited to fix page allignment]
[ September 30, 2004: Message edited by: Jeanne Boyarsky ]
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200


From your code section above, you try to get the sequence value from dual in the insert statement.

You don't need the "FROM DUAL" above...

If you want to see the next value of the sequence you can
SELECT <someseq>.NEXTVAL FROM DUAL

If you want to use the sequence in a query, you can.. if you have a table called customers... this would work as well....

SELECT cust.name_first, cust.name_last, <someseq>.NEXTVAL
FROM customers cust


SCJP 1.4 SCWCD 1.4
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Do you mean this is what I should do:

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Let me make sure that I am doing it right.

In my case,

1. the table name in the Oracle database is DHSInfoContentGroup
2. ID is the column name of that table for auto increment
3. dhsinfo_content_grp_seq has been taken care of by DBA

Therefore, the code should look like:
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I am very confused. The source of the confusion is switching from the MySQL database to the Oracle database. If I use the Oracle database:


What should I do with my Java code in database operation?
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
[B]
Do you mean this is what I should do:

code:

[/B]


Exactly - for Oracle
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
For MySQL you would have to

1. remove the field from the query
2. change the field in the table to auto increment

-C
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
What about the statement:

or this is the correct way to do:
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
The second one :-)
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thank you. It works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Auto Increment ID Field Table in the Oracle Database