2

如何通过 Callable Statement 使用存储过程检索现有数据?第一个参数用于SECTION_ID(主键),第二个参数用于SECTION_NAME。我不知道我哪里出错了,也许是写了一个存储过程。如果我错了,这是我的存储过程纠正我。

存储过程

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 ;

桌子

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

代码

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

}

当我尝试运行项目并插入现有数据时。它什么也没显示。它没有打印出我想要的节目的价值。任何帮助或提示将不胜感激!:)

4

0 回答 0