[英]Cannot retrieve the value I desired to Select using Stored Procedure
I'm trying to find a record.我试图找到一个记录。 Which let me choose to find a existing record in my database using Stored Procedure.这让我可以选择使用存储过程在我的数据库中查找现有记录。 When I tried to search a existing data it doesn't give me the value that I want.当我尝试搜索现有数据时,它没有给我想要的值。 When I hit the search button it's not printing the value to the textfield.当我点击搜索按钮时,它不会将值打印到文本字段。
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.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);
}
}
Stored Procedure存储过程
CREATE PROCEDURE getSECTION_NAME(IN ID INT, OUT NAME VARCHAR(50))
SELECT * FROM allsections_list WHERE SECTION_ID = ID AND SECTION_NAME = NAME
Table桌子
CREATE TABLE
(
SECTION_ID INT PRIMARY KEY AUTO_INCREMENT,
SECTION_NAME VARCHAR(50) NOT NULL
)
Any help would be appreciated!任何帮助,将不胜感激! Thanks!谢谢!
Update!更新! According to what I read Stored Procedure can return a result set.根据我读到的存储过程可以返回一个结果集。 I want to retrieve the values of the OUT parameter.我想检索 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);
}
}
I removed the String sectionName = myRs.getString(2); Section_SectionName_TextField.setText(sectionName); Section_SectionName_TextField.setEnabled(true);
我删除了String sectionName = myRs.getString(2); Section_SectionName_TextField.setText(sectionName); Section_SectionName_TextField.setEnabled(true);
String sectionName = myRs.getString(2); Section_SectionName_TextField.setText(sectionName); Section_SectionName_TextField.setEnabled(true);
out of the Result Set block and put it in the Callable Statement block.从结果集块中取出并将其放入可调用语句块中。 When I run the program.当我运行程序时。 The only changes is the textfield become enabled and prints me a "null" value.唯一的变化是文本字段被启用并向我打印一个“空”值。
2nd Update!第二次更新! I want to returned the values of OUT parameter I should not use Result Set to retrieve it.我想返回 OUT 参数的值我不应该使用结果集来检索它。 So I used Callable Statement parameter with OUT parameter of stored procedure according to @Gord Thompson.所以我根据@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);
}
}
Its still giving me a null values where I don't why I getting this value.它仍然给我一个空值,我不知道为什么我得到这个值。
The only changes to my GUI is the textfield become enabled and it's not printing the value I want in the following textfield.对我的 GUI 的唯一更改是启用了文本字段,并且它没有在以下文本字段中打印我想要的值。 :( :(
Thanks for responding.感谢您的回复。 Feel free to comment.随意发表评论。
If you want the value that is returned via an OUT parameter of a stored procedure then you don't use a ResultSet, you use the CallableStatement parameter associated with the OUT parameter of the stored procedure.如果您想要通过存储过程的 OUT 参数返回的值,则不使用 ResultSet,而是使用与存储过程的 OUT 参数关联的 CallableStatement 参数。 For example, for the test table例如,对于测试表
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
containing the sample data包含样本数据
SECTION_ID SECTION_NAME
---------- ---------------
1 one_section
2 another_section
and the stored procedure和存储过程
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
then the following Java code那么下面的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);
}
prints印刷
another_section
you should use delimiter
to change the delimiter to something different than ";"您应该使用delimiter
将分隔delimiter
更改为不同于“;”的内容。 when you create the procedure:创建过程时:
delimiter //
CREATE PROCEDURE getSECTION_NAME(IN ID INT, OUT NAME VARCHAR(50))
BEGIN
SELECT SECTION_NAME INTO NAME FROM allsections_list WHERE SECTION_ID = ID;
END
//
delimiter ;
The first delimiter
statement sets the delimiter to "//".第一个delimiter
语句将分隔delimiter
设置为“//”。 This way, the ";"这样,“;” in your stored procedure code is not interpreted as a delimiter anymore.在您的存储过程中,代码不再被解释为分隔符。 Your CREATE PROCEDURE
statement then correctly ends at the "//".然后您的CREATE PROCEDURE
语句正确地以“//”结尾。 Aftwerwards, the second delimiter
statement changes the delimiter back to ";".之后,第二个delimiter
语句将定界符改回“;”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.