user1028882
user1028882

Reputation: 53

SQL Return Auto_increment value from insert

I have a large data set(250,000 rows) that I need to upload to a SQL table. I am currently doing this from a Java application I created, and as expected, it is extremely slow. I have to insert each row from a text file into a table, with an auto_increment column that stores an id for that new row. Now I want to use that same id to insert a modified version of the string I entered to the first table, into a second table, where one of the columns holds that original incremented id, so that I can tie the two rows together. Obviously I can query the string I just inserted, and pull the id, but that is an extra query per row I'm trying to avoid. Is there a way to pull the incremented id value from the sql table to my java class at the time the insert statement is executed?

Thanks,

Upvotes: 1

Views: 205

Answers (1)

davidmontoyago
davidmontoyago

Reputation: 1833

I guess you are using JDBC so use getGeneratedKeys() of java.sql.PreparedStatement. The returned Resulset contains the auto-generated key.

Upvotes: 1

Related Questions