Search code examples
javamysqljdbcautonumber

How to do batch insert with an autonumber in JDBC?


Previously i had some experience with MS Access and now i am doing JDBC, in Java of course. In MS Access Databases i can have a AutoNumber field, which is very convenient for generating primary keys. I would like to do the same in Java. However, after digging in SO(and google of course) for a few days, what i can find is just some very handy way. One of them is to firstly get the largest primary key( say N ) in the table, and then you insert the latest record with a key of (N+1). This works, i guess, but not efficient. Can any SO genius help me?


Solution

  • MySQL has a similar capability: auto increment. Just add this to your key fields and you're done.