2

How can I retrieve my existing data using Stored Procedure via Callable Statement? The first parameter is for the SECTION_ID (Primary Key) and the second is for the SECTION_NAME. I don't know which where I go wrong, maybe writing a Stored Procedure. Here is my Stored Procedure correct me if I'm wrong.

Stored Procedure

DELIMITER @@
DROP PROCEDURE getSECTION_NAME @@
CREATE PROCEDURE enrollmentdb.getSECTION_NAME
(IN ID INT, OUT NAME VARCHAR(50))
BEGIN
   SELECT SECTION_NAME INTO NAME FROM allsections_list WHERE SECTION_ID = ID; 
END @@ 
DELIMITER ;

Table

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

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())
            {
                while (myRs.next())
                {
                    sectionID = myRs.getInt("SECTION_ID");
                    String getSection_Name = myRs.getString(2);

                    Section_SectionName_TextField.setText(getSection_Name);//Set the value of text
                    Section_SectionName_TextField.setEnabled(true);//Set to enable

                    System.out.print(sectionID);
                }//end of while
            }//end of resultset
            }//end of if
            }//end of callablestatement
        }//end of connection
        catch (SQLException e) 
        {
            DBUtil.processException(e);
        }

}

When I try to run the project and insert a existing data. It shows nothing. It doesn't print the value that I desire the show. Any help or tips would appreciated! :)

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45

0 Answers0