簡體   English   中英

使用Select正確使用存儲過程

[英]Proper use of Stored Procedure using Select

我創建了一個存儲過程,可以在其中通過Callable語句選擇要在存儲過程中處理的列。 我嘗試使用SELECT SECTION NAME FROM allsections_list WHERE SECTION_NAME = ? 與Prepared Statement的語法相似,但我認為使用此語法不兼容。 我是剛剛學習此mysql的新手。

儲存程序

CREATE STORED PROCEDURE getSECTION_NAME(OUT SECTION_NAME VARCHAR)
SELECT SECTION_NAME FROM allsections_list

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String searchSection = Section_SearchSection_Textfield.getText();
    String searchSection_Name = Section_SectionName_TextField.getText();

    if (searchSection.isEmpty())
    {
        JOptionPane.showMessageDialog(null, "Please fill up this fields");
    }
    else 

        try (Connection myConn = DBUtil.connect();
             CallableStatement myCs = myConn.prepareCall("{call getSECTION_NAME(?)}"))
        {
            myCs.setString(1, searchSection_Name);

            try (ResultSet myRs = myCs.executeQuery())
            {
                int resultsCounter = 0;
                while (myRs.next())
                {
                    String getSection_Name = myRs.getString(1);
                    Section_SectionName_TextField.setText(getSection_Name);
                    resultsCounter++;
                }
            }
        } 
        catch (SQLException e) 
        {
            DBUtil.processException(e);
        }

當我搜索記錄時。 如果記錄存在,則該值將打印到文本字段。 但是它不會打印出來。 而且它拋出一個錯誤getSECTION_NAME does not exist 如果我想選擇多個值怎么辦? 因為我有一個項目在制作注冊系統。 我會根據閱讀的內容而不是批處理語句來特別選擇此存儲過程。 任何幫助將不勝感激。 謝謝!

我不使用MySql,但這是Oracle中的類似示例(我認為這在MySql中也適用)。

CREATE PROCEDURE get_section_name(OUT secName VARCHAR(100))
BEGIN
SELECT SECTION_NAME INTO secName FROM allsections_list WHERE some_condition   = 100; //your procedure does not use any input arguments if you want to return just one record then you'll probably need to specify a where clause
END
/  //when executing a stored procedure in a DB  client you will need to specify a terminator character (in this case slash /)

請注意,因為我們使用的是OUT參數,所以沒有return語句。

getOutValueForStoredProcedure方法調用存儲過程並檢索出值。

public String getOutValueForStoredProcedure(String procedureName, int sqlType) throws EasyORMException{

    String out=null;
    CallableStatement stmt=null;
    try{
            //out parameters must me marked with question marks just as input parameters
          sqlQuery = "{call " + procedureName +"(?)}";
          stmt=conn.prepareCall(sqlQuery);//I assume that a Connection has been created

          stmt.registerOutParameter(1, sqlType);

          stmt.execute();    

          out = stmt.getString(1);//you get the out variable through the Statement, not the ResultSet

    }catch(Exception e){
          //log exception
    }finally{
          //close stmt
    }
    return out;
}

要調用此存儲過程,可以使用

   String out = getOutValueForStoredProcedure("get_section_name", java.sql.Types.VARCHAR);

要在MySql中創建存儲過程,請檢查此鏈接http://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843

有關更詳細的示例,請查看此http://www.mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-out-parameter-example/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM