使用executeBatch()“插入”大量数据后如何获取ID

时间:2019-05-05 01:25:39

标签: java jdbc

这是我的代码的简历:

cf.getConnection().setAutoCommit(false);
PreparedStatement ps = 
cf.getConnection().prepareStatement(SQL_INSERT_TC, 
Statement.RETURN_GENERATED_KEYS);

for (TesteCaseTSBean testCase : testCases) {
ps.setString(1, testCase.getTestPlan());
ps.setString(2, testCase.getSTIPRJ());
ps.setString(3, testCase.getFase());
ps.setString(4, testCase.getTestPhase());
ps.setString(5, testCase.getTestScriptName());
ps.setString(6, testCase.getTestScriptDescription());
ps.setString(7, testCase.getStepDescription());
ps.setString(8, testCase.getExpectedResults());
ps.setString(9, testCase.getProduct());
ps.setTimestamp(10, new 
java.sql.Timestamp(testCase.getDataPlanejada().getTime()));
ps.setString(11, testCase.getNumeroCenario());
ps.setString(12, testCase.getNumeroCt());
ps.setString(13, testCase.getComplexidade());
ps.setBoolean(14, testCase.isAutomatizado());
ps.setString(15, testCase.getCenario());
ps.setBoolean(16, testCase.isRework());
ps.setBoolean(17, testCase.isPriority());
ps.setBoolean(18, testCase.isRegression());
ps.setBoolean(19, testCase.isData());
ps.setInt(20, testCase.getIdSystem());
ps.setString(21, testCase.getCreatedBy());
ps.setString(22, testCase.getModifiedBy());
ps.setTimestamp(23, new 
java.sql.Timestamp(testCase.getModifyDate().getTime()));
ps.setTimestamp(24, new 
java.sql.Timestamp(testCase.getCreateDate().getTime()));
ps.setInt(25, testCase.getOrder());
ps.setInt(26, testCase.getIdTestPlanTS());
ps.addBatch();

int[] row = ps.executeBatch();

ResultSet rs = ps.getGeneratedKeys();

运行ResultSet rs = ps.getGeneratedKeys();时返回错误:

  

必须先执行该语句,然后才能获得任何结果。

0 个答案:

没有答案