2016-03-23 24 views
0

如何在我的數據中獲得現有主鍵ID的值?我試圖選擇一個數據,但它沒有得到我當前表中的值。 當我嘗試搜索現有的記錄。它打印到文本框。另外,當我搜索一個不存在的記錄時,它仍然會打印到文本字段。我錯過了什麼嗎?如何在選擇使用存儲過程之後獲取主鍵值?

不存在的記錄

enter image description here

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

存儲過程

CREATE PROCEDURE getSECTION_NAME (IN SECTION_NAME VARCHAR(50)) 
SELECT SECTION_NAME FROM allsections_list 

DATA

enter image description here

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.setString(1, searchSection);//Get value of Section_SearchSection_Textfield 

       myFirstCs.executeQuery(); 

      try (ResultSet myRs = myFirstCs.executeQuery()) 
      { 
       int resultsCounter = 0; 
       while (myRs.next()) 
       { 
        String getSection_Name = myRs.getString(1); 
        sectionID = myRs.getInt("SECTION_ID"); 

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

        System.out.print(sectionID); 
        resultsCounter++; 

       }//end of while 
       }//end of resultset 
      }//end of callablestatement 
     }//end of connection 
     catch (SQLException e) 
     { 
      DBUtil.processException(e); 
     } 
} 

任何幫助或建議將感激!謝謝!

回答

0

您的程序定義中只有一個IN參數。您可以使用OUT參數,在過程中設置它並在執行後選擇它以獲得結果,如下所示:mysql manual

在第一個示例中。

+0

謝謝,我會讀這個。 – Francisunoxx

相關問題