It been two days since I can't move on with this problem. I know to other's this is very simple. I'm just starting to learn JDBC for 3 months.
Problem
- Can't retrieve value from Result Set.
- It's giving me a null value.
According to what I read and comment about my codes Stored Procedure can return a Result Set, but it my case does not. I should not loop through Result Set. So If I want to retrieve the value I need to invoke a appropriate getXXX() method on the Callable Statement itself not on the Result Set. So what I do is removed my code from Result Set block and put it on the Callable Statement block which gives me a null value. I can't properly retrieve my values.
CREATE TABLE allsections_list
(
SECTION_ID INT PRIMARY KEY,
SECTION_NAME VARCHAR(50) NULL
)
DELIMITER @@
DROP PROCEDURE getSECTION_NAME @@
CREATE PROCEDURE enrollmentdb.getSECTION_NAME
(IN myID INT, OUT myName VARCHAR(50))
BEGIN
SELECT SECTION_NAME INTO myName FROM allsections_list WHERE SECTION_ID = myID;
END @@
DELIMITER ;
When I try to retrieve my Stored Procedure via Call syntax. Successfully I'm retrieve the existing record.
SET @myID = 9;
CALL getSECTION_NAME(@myID, @myName);
SELECT @myName;
As you can see here I'm retrieving the correct value. But when I retrieve it using Result Set it gives me a null value.
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();
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())
{
int counter = 0;
while (myRs.next())
{
sectionID = myRs.getInt("SECTION_ID");
System.out.print(sectionID);
counter++;
}//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);
}
}
What I want to do if I search a existing record will look like this.
Thanks for your time reading my post. Any help will appreciate. Thanks! :)