【问题标题】:Proper use of Stored Procedure using Select使用 Select 正确使用存储过程
【发布时间】:2016-07-07 22:53:03
【问题描述】:

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

【问题讨论】:

  • 尽量不要使用存储过程,它们会引入对您的数据库的依赖。您的数据库仅用于存储数据。
  • @NimChimpsky 如果我想选择多个值怎么办?
  • 动态查询几乎从来都不是一个好主意。

标签: java mysql stored-procedures jdbc


【解决方案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 方法调用存储过程并检索输出值。

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/

【讨论】:

  • @Downvoter,想解释一下投反对票的原因,以便我改进我的答案吗?
猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 2017-07-20
  • 1970-01-01
  • 2016-12-13
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
相关资源
最近更新 更多