简体   繁体   中英

Proper use of Stored Procedure using Select

I created a Stored Procedure where I can select the column that I address in my Stored Procedure via Callable Statement. I tried to use SELECT SECTION NAME FROM allsections_list WHERE SECTION_NAME = ? Similar to syntax of Prepared Statement but I think its not compatible using this syntax. I'm just new learning this mysql.

Stored Procedure

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

Code

 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);
        }

When I search a records. If the records exist it the value will print out to the textfields. But it doesn't print out. And it throws me a error getSECTION_NAME does not exist . What if I want select multiple values? Because I'm having a project where I'm making a Enrollment System. I choose this Stored Procedure specially than Batch Statement based on what I read. Any help will appreciate. Thanks!

I don't use MySql, but here's a similar example in Oracle (I think this Works in MySql as well).

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 /)

Note that there's no return statement because we're using OUT parameters.

The getOutValueForStoredProcedure method calls the stored procedure and retrieves the out value.

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;
}

To call this stored procedure, you can use

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

For creating stored procedures in MySql , check this link http://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843

For a more elaborate example, check this http://www.mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-out-parameter-example/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM