【问题标题】:Prepared statement returning -2 value always准备好的语句总是返回 -2 值
【发布时间】:2012-10-29 13:35:44
【问题描述】:

我有一个准备好的合并语句,执行后它总是返回-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命令的值,如更新、合并等,我使用过像ojdbc6.jar、ojdbc5.jar等库

【问题讨论】:

  • 您要执行的 SQL 语句在哪里?
  • 请检查一下,我已经添加了sql语句..
  • 嗯,我看到了需要指定的 12 个参数。你只设置了 6。
  • 请立即查看.....
  • 还是不行? -2 是什么意思?

标签: oracle jdbc batch-file prepared-statement


【解决方案1】:

这没有坏。

如果您阅读文档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。

所以你的代码一直在工作。

【讨论】:

  • 您好,我的目的是识别失败的行,然后重新处理它。但是 SUCCESS_NO_INFO 值对此没有帮助。关于一个清晰的想法,请参阅我以前的问题,stackoverflow.com/questions/13048338/…
  • 请提供一个清晰的单个问题来证明您的问题。该问题应提供重现您的问题所需的一切。
  • 并且——澄清一下——如果批处理中的所有行都标记为-2,那么根据 JDBC,所有行都是成功的。
猜你喜欢
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2015-07-13
  • 2012-01-31
  • 1970-01-01
  • 2014-08-04
相关资源
最近更新 更多