通过结果集调用存储过程

时间:2016-03-24 12:16:13

标签: java mysql stored-procedures jdbc 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);
        }

}

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

0 个答案:

没有答案