2013-08-30 24 views
-3

在下面的代碼中,我想調用一個存儲過程並執行一個查詢。我在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"); 

      }  

} 
+1

這個問題似乎是在你想要執行的sql語句中。我的意思是,來自DB2的錯誤,而不是Java。可能我們需要整個sql語句來確定錯誤的位置。 – dic19

+1

嗨,你正確..問題出在查詢。我糾正了。謝謝你。 – 123HIS

+0

太棒了!你很歡迎:) – dic19

回答

3

小題外話:

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

我也建議你查一下這個話題How to properly clean up JDBC resources in Java?:如果你要調用一個存儲過程(見documentation)您應該使用CallableStatement代替。這對我非常有幫助。

更新:基於該堆棧跟蹤:

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語句。

+0

嗨,謝謝你的回覆。我得到錯誤在statement.executeUpdate(); – 123HIS

+0

com.ibm.db2.jcc.am.mo:DB2 SQL錯誤:SQLCODE = -104,SQLSTATE = 42601,SQLERRMC = MERGE INTO OPPTYMGMT.REVENUE_VALIDAT; BEGIN-OF-STATEMENT; ,DRIVER = 4.7.85 – 123HIS

+0

你能否檢查我是否已經關閉了連接,可打印的陳述,準備好的陳述是否正確? – 123HIS

相關問題