aspose file tools*
The moose likes JDBC and the fly likes How to INSERT with an autoincrement column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to INSERT with an autoincrement column" Watch "How to INSERT with an autoincrement column" New topic
Author

How to INSERT with an autoincrement column

Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 180

An autoIncrement column is supposed to be read-only. If that's the case, how does one INSERT a new row? (I'm using MS Access with a JDBC/ODBC bridge)
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
To add a record insert values into all the fields apart from the auto-incrementing one.
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 180

Originally posted by David Peterson:
To add a record insert values into all the fields apart from the auto-incrementing one.

Sorry, doesn't work. If you leave out any of the fields JDBC complains at run-time.
Dorothy Finkel-Laverty
Ranch Hand

Joined: Nov 24, 2001
Posts: 51
You have to first figure out what the next "auto increment" number would be - you can use a "get max" first on the column, deal with the result set & getInt(1), the increment it by one (assuming it's an int), then when you insert the new record, you have the correct next value to insert.

[This message has been edited by Dorothy Finkel-Laverty (edited November 24, 2001).]
Daniel Dunleavy
Ranch Hand

Joined: Mar 13, 2001
Posts: 276
Carey,
You list columns you are inserting and values.
ex. insert into testtab (lastname, firstname) values('ann', 'jones')
Dan
[This message has been edited by Daniel Dunleavy (edited November 24, 2001).]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Sorry, doesn't work. If you leave out any of the fields JDBC complains at run-time.[/B]

This does work. There may be something else wrong with your insert. What is the error message?
This works:
Table emp, columns id (autonumber), name (string)
sql: insert into emp (name) values ("John Fingers")
this will insert the next autonumber field and the name into the emp table.
Jamie
sandeep balyan
Ranch Hand

Joined: Jul 25, 2001
Posts: 134
my friend make one oracle sequence it will work i Solved it before
sandeep


Want to do something great will do one day
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by sandeep balyan:
my friend make one oracle sequence it will work i Solved it before
sandeep

tough to make an Oracle sequence when you are using MSAccess Autonumber!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to INSERT with an autoincrement column