1

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;

enter image description here

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

enter image description here

What I want to do if I search a existing record will look like this. enter image description here

Thanks for your time reading my post. Any help will appreciate. Thanks! :)

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
  • How will your ResultSet have `"SECTION_ID"` in it? Will it not just have the `Section Name` in it? Please correct me if I'm wrong. – user2004685 Mar 25 '16 at 09:17
  • @user2004685 Hi! Thanks for responding. I'm just trying to assign the `sectionID` to the 1st index. So I can get the value of the ID that I enter. I just put that code so I can distinguish what ID did I get. :) – Francisunoxx Mar 25 '16 at 09:33
  • I was talking about this statement: `sectionID = myRs.getInt("SECTION_ID");` – user2004685 Mar 25 '16 at 09:43
  • Sorry. I'm just trying to get the value of ID. Correct me if I'm wrong. Thanks. – Francisunoxx Mar 25 '16 at 09:46
  • I'm not sure but I think you'll only have the `Section Name` in your ResultSet and not `Section ID`. As I said before, I'm not sure. I'll have to try it myself to see what it actually returns in the ResultSet. – user2004685 Mar 25 '16 at 09:48
  • 1
    You are posting a duplicate, again. Instead of posting the same (slightly modified) question over and over again, you should **edit** the original question to provide more information. – Mark Rotteveel Mar 25 '16 at 09:50
  • @user2004685 When I only put the `Section Name` in my ResultSet it prints nothing. – Francisunoxx Mar 25 '16 at 09:52
  • As user2004685 pointed out you cannot have `section_id` in your ResultSet when your query in the stored procedure is `SELECT SECTION_NAME INTO myName FROM allsections_list WHERE SECTION_ID = myID;`. But you can test `myRs.getString(1)` to see what you get from the database. – ujulu Mar 25 '16 at 10:37
  • Mia, your stored procedure does not create a ResultSet (because it just uses an OUT parameter). If section_name were returned in a return statement by the stored procedure (e.g return myName; ), then you would get the result from the ResultSet. – dsp_user Mar 25 '16 at 19:09

0 Answers0