gpt4 book ai didi

java - 如何使用存储过程获取Select后的主键值?

转载 作者:行者123 更新时间:2023-11-29 21:13:24 24 4
gpt4 key购买 nike

如何获取数据中现有主键 ID 的值?我尝试选择一个数据,但它没有获取当前表中的值。当我尝试搜索现有记录时。它打印到文本字段。此外,当我搜索不存在的记录时,它仍然打印到文本字段。我错过了什么吗?

不存在的记录

enter image description here

表格

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

存储过程

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

数据

enter image description here

代码

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

任何帮助或提示将不胜感激!谢谢!

最佳答案

您的过程定义中只有一个 IN 参数。您可以使用 OUT 参数,在过程中设置它并在执行后 SELECT 来获取结果,如下所示:mysql manual

在第一个示例中。

关于java - 如何使用存储过程获取Select后的主键值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36171147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com