我正在尝试执行以下代码。

public static void main(String[] args) throws Exception {
        Connection connection = null;
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        try {
            System.out.println();
             connection = DriverManager.getConnection("jdbc:db2://localhost:port/database", username, password);
             connection.setAutoCommit(false);
            // String plsql = "BEGIN INSERT INTO t VALUES(42); COMMIT; END;";
            String plsql = "BEGIN UPDATE TABLE SET PROPERTY_VALUE='test6' where property_name='TEST'; COMMIT; END;";
            PreparedStatement pstatement = connection.prepareStatement(plsql);
            pstatement.execute();


        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }


而且我得到以下错误:

com.ibm.db2.jcc.b.co: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=END;NAME='TEST'; COMMIT;;END, DRIVER=3.53.70


如何解决这个问题。为什么在这种情况下支持准备好的语句?

最佳答案

我认为您需要使用对commit的JDBC调用来connection.commit()事务。

这个article给你一个很好的例子。

您的代码将变为下面的内容

public static void main(String[] args) throws Exception {
    Connection connection = null;
    try {
        Class.forName("com.ibm.db2.jcc.DB2Driver");
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    try {
         System.out.println();
         connection = DriverManager.getConnection("jdbc:db2://localhost:port/database", username, password);
         connection = DriverManager.getConnection("jdbc:db2://localhost:port/database", username, password);
         connection.setAutoCommit(false);
         String plsql = "UPDATE AR01.REBILL_PROPERTIES SET PROPERTY_VALUE='test6' where property_name='TEST'";
         PreparedStatement pstatement = connection.prepareStatement(plsql);
         pstatement.executeUpdate();
         connection.commit();
    } catch (SQLException ex) {
        connection.rollback();
        ex.printStackTrace();
    }
}


希望这可以帮助。

关于java - 准备好的语句失败,并出现DB2 SQL错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34994596/

10-13 06:31