简体   繁体   English

使用存储过程选择后如何获取主键值?

[英]How to get the Primary Key value after Select using Stored Procedure?

How can I get the value of the existing Primary Key ID in my data? 如何获取数据中现有主键ID的值? I tried to select a data but it's not getting the value in my current table. 我试图选择一个数据,但是它没有在当前表中获取值。 When I try search a existing record. 当我尝试搜索现有记录时。 It prints to the textfields. 它打印到文本字段。 Also when I search a non existing record it still prints to the textfields. 同样,当我搜索不存在的记录时,它仍会打印到文本字段。 Did I missed something? 我错过了什么吗?

Non Existing Record 不存在的记录

在此处输入图片说明

TABLE

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

STORED PROCEDURE 存储过程

CREATE PROCEDURE getSECTION_NAME (IN SECTION_NAME VARCHAR(50))
SELECT SECTION_NAME FROM allsections_list

DATA 数据

在此处输入图片说明

CODE

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.setString(1, searchSection);//Get value of Section_SearchSection_Textfield

                myFirstCs.executeQuery();

            try (ResultSet myRs = myFirstCs.executeQuery())
            {
                int resultsCounter = 0;
                while (myRs.next())
                {
                    String getSection_Name = myRs.getString(1);
                    sectionID = myRs.getInt("SECTION_ID");

                    Section_SectionName_TextField.setText(getSection_Name);//Set the value of text
                    Section_SectionName_TextField.setEnabled(true);//Set to enable

                    System.out.print(sectionID);
                    resultsCounter++;

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

Any help or tips will appreciate! 任何帮助或提示将不胜感激! Thanks! 谢谢!

You only have an IN parameter in your procedure definition. 您的过程定义中只有一个IN参数。 You might use an OUT parameter, set it in the procedure and SELECT it after execution to get the result, like here: mysql manual 您可以使用OUT参数,在过程中进行设置,然后在执行后选择SELECT以获取结果,例如: mysql manual

in the first example. 在第一个示例中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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