我有一个带有merge的预准备语句,执行后它总是返回-2。
我的代码:
private StringBuilder sb = new StringBuilder();
sb.append("MERGE INTO EMP_BONUS EB USING (SELECT 1 FROM DUAL) on (EB.EMP_id = ?) WHEN MATCHED THEN UPDATE SET TA =?,DA=?,TOTAL=?,MOTH=?, NAME=? WHEN NOT MATCHED THEN "
+ "INSERT (EMP_ID, TA, DA, TOTAL, MOTH, NAME)VALUES(?,?,?,?,?,?) ");
public void executes(String threadName) throws Exception {
ConnectionPro cPro = new ConnectionPro();
Connection connE = cPro.getConection();
threadN = threadN + "||" + threadName;
PreparedStatement pStmt = connE.prepareStatement(sb.toString());
try {
count = count + 1;
for (Employee employeeObj : employee) {
pStmt.setInt(1, employeeObj.getEmp_id());
pStmt.setDouble(2, employeeObj.getSalary() * .10);
pStmt.setDouble(3, employeeObj.getSalary() * .05);
pStmt.setDouble(4, employeeObj.getSalary()
+ (employeeObj.getSalary() * .05)
+ (employeeObj.getSalary() * .10));
pStmt.setInt(5, count);
pStmt.setString(6, threadN);
pStmt.setInt(7, employeeObj.getEmp_id());
pStmt.setDouble(8, employeeObj.getSalary() * .10);
pStmt.setDouble(9, employeeObj.getSalary() * .05);
pStmt.setDouble(10, employeeObj.getSalary()
+ (employeeObj.getSalary() * .05)
+ (employeeObj.getSalary() * .10));
pStmt.setInt(11, count);
pStmt.setString(12, threadN);
pStmt.addBatch();
}
int arr[] = pStmt.executeBatch();
connE.commit();
System.out.println("Size=" + arr.length);
if (arr.length != 0) {
for (int i = 0; i < arr.length; i++) {
System.out.println(i + "==" + arr[i]);
}
}
} catch (Exception e) {
connE.rollback();
throw e;
} finally {
pStmt.close();
connE.close();
}
}
请参阅代码,准备好的语句总是返回-2(数组内的所有值都是-2)sql命令的值,如update,merge等,我使用的库像ojdbc6.jar,ojdbc5.jar等
答案 0 :(得分:3)
这没有被打破。
如果您阅读文档http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html#executeBatch%28%29,您将看到executeBatch可以返回值SUCCESS_NO_INFO。
如果你比较你得到的结果与SUCCESS_NO_INFO,你会发现它们是相同的 - 也就是说,SUCCESS_NO_INFO = -2。
所以你的代码一直在努力。