我正在尝试查找一条记录。它让我选择使用存储过程在我的数据库中查找现有记录。当我尝试搜索现有数据时,它没有给我想要的值。当我点击搜索按钮时,它没有将值打印到文本框中。
代码
存储过程
任何帮助都将不胜感激!谢谢!更新!根据我所读的,存储过程可以返回结果集。我想检索OUT参数的值。
我将
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
)
任何帮助都将不胜感激!谢谢!更新!根据我所读的,存储过程可以返回结果集。我想检索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);
从结果集块中移除并放入可调用语句块中。当我运行程序时,唯一的变化是文本字段变为启用状态,并打印出一个“null”值。
第二次更新! 如果我想返回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);
}
}
它仍然给我一些空值,我不知道为什么会得到这个值。
我的GUI只有一个文本框变为可用状态,但它没有在下一个文本框中打印我想要的值。 :(谢谢您的回复。随意发表评论。