Java – getgeneratedkeys() in Preparedstatement After executebatch()

I want to insert a few lines using Preparedstatement:

ps = con.prepareStatement(query,PreparedStatement.RETURN_GENERATED_KEYS);

for(Element e:listOfElements){
    ps.setString(1,this.col_val_1);
    ps.setString(2,this.col_val_2);
    ps.setInt(3,this.col_val_3);

    ps.addBatch();
}

ps.executeBatch();
ResultSet rs = ps.getGeneratedKeys();

At this point, I want to get the PK generated by each insert. I get the sqlserverexception:

com.microsoft.sqlserver.jdbc.sqlServerException: The statement must be executed before any results can be obtained.

I expect to get a resultset, one row for each insert, so I can get each PK generation

Am I wrong? Did I do wrong? Can batch execution be used in different ways?

Solution

The support for getgeneratedkeys () during batch execution is based on the JDBC specification The SQL Server driver probably does not support batch processing

I tried to find a clear statement on the Microsoft website, but I couldn't find it The forum post on this old (2007) MSDN does not support: http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/6cbf5eea-e5b9-4519-8e86-f4b65ce3f8e1

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>