java - 无法检索我希望使用存储过程选择的值

标签 java mysql stored-procedures jdbc callable-statement

我正在尝试查找记录。这让我选择使用存储过程在我的数据库中查找现有记录。当我尝试搜索现有数据时,它没有给我想要的值。当我点击搜索按钮时,它没有将值打印到文本字段。

代码

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

  String searchSection = Section_SearchSection_Textfield.getText();
  String searchSection_Name = Section_SectionName_TextField.getText();
  int sectionID = 0;

  if (searchSection.isEmpty())
    {
        JOptionPane.showMessageDialog(null, "Please fill up this fields");
    }
  else 
        try (Connection myConn = DBUtil.connect())
        {   
            try (CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
            {
                myFirstCs.setInt(1, sectionID);// I set the ID for Primary Key
                myFirstCs.registerOutParameter(2, Types.VARCHAR);
                myFirstCs.setString(2, searchSection_Name);


                boolean hasresults = myFirstCs.execute();

            if (hasresults)
            {
            try (ResultSet myRs = myFirstCs.getResultSet())
            {
                int resultsCounter = 0;
                while (myRs.next())
                {
                    sectionID = myRs.getInt("SECTION_ID");
                    String sectionName = myRs.getString(2);
                    Section_SectionName_TextField.setText(sectionName);//Set the value of text
                    Section_SectionName_TextField.setEnabled(true);//Set to enable

                    resultsCounter++;

                }//end of while
               }//end of if
               }//end of resultset
            }//end of callablestatement
        }//end of connection
        catch (SQLException e) 
        {
            DBUtil.processException(e);
        }
}

存储过程

CREATE PROCEDURE getSECTION_NAME(IN ID INT, OUT NAME VARCHAR(50))
SELECT * FROM allsections_list WHERE SECTION_ID = ID AND SECTION_NAME = NAME

表格

CREATE TABLE
(
SECTION_ID INT PRIMARY KEY AUTO_INCREMENT,
SECTION_NAME VARCHAR(50) NOT NULL
)

任何帮助将不胜感激!谢谢!

更新! 根据我的阅读,Stored Procedure 可以返回一个结果集。我想检索 OUT 参数的值。

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 myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
        {

             myFirstCs.setInt(1, sectionID);// I set the ID for Primary Key
             myFirstCs.registerOutParameter(2, Types.VARCHAR);

            boolean hasresults = myFirstCs.execute();

        if (hasresults)
        {
        try (ResultSet myRs = myFirstCs.getResultSet())
        {
            while (myRs.next())
            {
                sectionID = myRs.getInt("SECTION_ID");

                System.out.print(sectionID);
            }//end of while

        }//end of resultset
        }//end of if
                String sectionName = myFirstCs.getString(2);
                Section_SectionName_TextField.setText(sectionName);//Set the value of text
                Section_SectionName_TextField.setEnabled(true);//Set to enable
                System.out.print(sectionName);
        }//end of connection
        catch (SQLException e) 
        {
            DBUtil.processException(e);
        }

}

我删除了 String sectionName = myRs.getString(2); Section_SectionName_TextField.setText(sectionName); Section_SectionName_TextField.setEnabled(true); 从 Result Set block 中取出并放入 Callable Statement block 中。当我运行程序时。唯一的变化是文本字段已启用并向我打印一个“空”值。

enter image description here

第二次更新! 我想返回 OUT 参数的值我不应该使用结果集来检索它。因此,根据@Gord Thompson,我使用了 Callable Statement 参数和存储过程的 OUT 参数。

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 myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
        {

             myFirstCs.setInt(1, 2);// I set the ID for Primary Key
             myFirstCs.registerOutParameter(2, Types.VARCHAR);
             myFirstCs.execute();

             String sectionName = myFirstCs.getString(2);  // retrieve value from OUT parameter
             Section_SectionName_TextField.setText(sectionName);//Set the value of text
             Section_SectionName_TextField.setEnabled(true);//Set to enable
             System.out.println(sectionName);

        }//end of connection
        catch (SQLException e) 
        { 
            DBUtil.processException(e);
        }
}

它仍然给我一个空值,但我不知道为什么会得到这个值。

enter image description here

我的 GUI 的唯一变化是启用了文本字段,它没有在下面的文本字段中打印我想要的值。 :(

enter image description here

感谢您的回复。欢迎发表评论。

最佳答案

如果您想要通过存储过程的 OUT 参数返回的值,则不使用 ResultSet,而是使用与存储过程的 OUT 参数关联的 CallableStatement 参数。比如对于测试表

CREATE TABLE `allsections_list` (
 `SECTION_ID` int(11) NOT NULL,
 `SECTION_NAME` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`SECTION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

包含示例数据

SECTION_ID  SECTION_NAME
----------  ---------------
         1  one_section
         2  another_section

和存储过程

CREATE PROCEDURE `getSECTION_NAME`(IN myID INT, OUT myName VARCHAR(50))
BEGIN
   SELECT SECTION_NAME INTO myName FROM allsections_list WHERE SECTION_ID = myID;
END

然后是下面的Java代码

try (CallableStatement myFirstCs = conn.prepareCall("{call getSECTION_NAME(?,?)}")) {
    myFirstCs.setInt(1, 2);  // set IN parameter "myID" to value 2
    myFirstCs.registerOutParameter(2, Types.VARCHAR);
    myFirstCs.execute();
    String sectionName = myFirstCs.getString(2);  // get value from OUT parameter "myName"
    System.out.println(sectionName);
}

打印

another_section

关于java - 无法检索我希望使用存储过程选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200008/

相关文章:

java - 在 thymeleaf 中包含 JavaScript 变量

Java - 使用BufferedReader而不消耗内存

java - 通用接口(interface)以自身为参数。递归泛型?

mysql - 我可以将同一个表中的多列与所有数据合并在一列中吗?

java - 有没有办法轻松修改 ANTLR4 的错误消息?

php - MPDF 仅对每页结果求和

mysql - 删除Cascade而不改变表结构

MySql存储过程,逻辑上或物理上删除依赖于现有表引用的记录

SQL Server - 测试存储过程的结果

SQL Server 存储过程 - 我可以在一个过程中执行多项操作吗?