使用选择

时间:2016-03-21 12:21:22

标签: java mysql stored-procedures jdbc

我创建了一个存储过程,我可以通过Callable Statement选择我在存储过程中寻址的列。我尝试使用 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 。如果我想选择多个值怎么办?因为我有一个项目,我正在制作一个注册系统。我根据我读到的内容特别选择了这个存储过程而不是批处理语句。任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:-1)

我不使用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 /)

请注意,没有return语句,因为我们使用的是OUT参数。

getOutValueForStoredProcedure方法调用存储过程并检索out值。

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/