【问题标题】:How to call Stored Procedure and prepared statement如何调用存储过程和准备好的语句
【发布时间】:2013-09-03 01:59:18
【问题描述】:

在下面的代码中,我想调用一个存储过程并执行一个查询。我在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");

            }    

 }

【问题讨论】:

  • 问题似乎出在您尝试执行的 sql 语句中。我的意思是,是来自 DB2 的错误,而不是来自 java 的错误。可能我们需要整个 sql 语句来确定错误在哪里。
  • 嗨,你是对的..问题出在查询中。我纠正了。谢谢你。
  • 太棒了!你很高兴:)

标签: java sql stored-procedures jdbc db2


【解决方案1】:

有点离题:如果你想调用存储过程,你应该使用CallableStatement(参见documentation):

CallableStatement callableStatement = connection.prepareCall("{call opptymgmt.Load_SiebelOpportunity}");
ResultSet rs = callableStatement.executeQuery();

我还建议您查看此主题How to properly clean up JDBC resources in Java?。这对我很有帮助。

更新:基于此堆栈跟踪:

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 

问题似乎出在您尝试执行的 sql 语句中。我的意思是,是来自 DB2 的错误,而不是来自 java 的错误。你应该检查你的 sql 语句。

【讨论】:

  • 您好,感谢您的回复。我在 statement.executeUpdate(); 处收到错误;
  • com.ibm.db2.jcc.am.mo:DB2 SQL 错误:SQLCODE=-104,SQLSTATE=42601,SQLERRMC=MERGE INTO OPPTYMGMT.REVENUE_VALIDAT;BEGIN-OF-STATEMENT; , 驱动=4.7.85
  • 你能检查一下我是否关闭了连接,callablestatement,准备好了statments吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多