如何调用存储过程和准备好的声明

123*_*HIS -3 java sql db2 stored-procedures jdbc

在下面的代码中,我想调用一个存储过程并执行一个Query.我正面临错误statement.executeUpdate();请帮助修复它.我不知道哪里出错了.

public void Dbexe() {

    Connection connection;
    connection = DatabaseConnection.getCon();
     CallableStatement stmt;
    try {
        stmt = connection.prepareCall("{CALL optg.Ld_SOpp}");
        stmt.executeUpdate();
        stmt.close();
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

     System.out.println("Stored Procedure executed");

     //PreparedStatement statement = null;
    // ResultSet rs = null;

    try{


     PreparedStatement statement;
    try {
        statement = connection.prepareStatement("MERGE INTO OPTG.R_VAL AS TARGET USING" + 
              ........... +
             "");

         statement.executeUpdate(); //Here the exception is thrown  
         statement.close();

         connection.commit();
         connection.close();


    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   

    // statement = connection.prepareStatement(query);

     //statement.close();

    }

    finally{

        System.out.println("Data is copied to the  Table");

            }    

 }
Run Code Online (Sandbox Code Playgroud)

dic*_*c19 5

一点偏离主题:CallableStatement如果你想调用一个商店程序,你应该使用它(参见文档):

CallableStatement callableStatement = connection.prepareCall("{call opptymgmt.Load_SiebelOpportunity}");
ResultSet rs = callableStatement.executeQuery();
Run Code Online (Sandbox Code Playgroud)

我还建议您查看此主题如何正确清理Java中的JDBC资源?.这对我很有帮助.

更新:基于此堆栈跟踪:

com.ibm.db2.jcc.am.mo: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=MERGE INTO OPPTYMGMT.REVENUE_VALIDAT;BEGIN-OF-STATEMENT;<variable_set>, DRIVER=4.7.85 
Run Code Online (Sandbox Code Playgroud)

问题似乎在你试图执行的sql语句中.我的意思是,DB2是一个错误,而不是java.你应该检查你的sql语句.