简体   繁体   English

SQL从插入返回Auto_increment值

[英]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. 我有一个大数据集(250,000行),需要将其上载到SQL表。 I am currently doing this from a Java application I created, and as expected, it is extremely slow. 我目前正在从我创建的Java应用程序中执行此操作,并且正如预期的那样,它非常慢。 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. 我必须将文本文件中的每一行插入一个表中,并使用一个auto_increment列来存储该新行的ID。 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. 现在,我想使用相同的ID将在第一个表中输入的字符串的修改后的版本插入第二个表,其中的一列包含原始递增的ID,以便可以将两行绑定在一起。 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. 显然,我可以查询刚刚插入的字符串并提取ID,但这是我要避免的每行额外查询。 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? 有没有一种方法可以在执行插入语句时将递增的id值从sql表中拉到我的java类中?

Thanks, 谢谢,

I guess you are using JDBC so use getGeneratedKeys() of java.sql.PreparedStatement . 我猜您正在使用JDBC,因此请使用java.sql.PreparedStatement getGeneratedKeys() The returned Resulset contains the auto-generated key. 返回的Resulset包含自动生成的密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM