2012-05-29 86 views
0

我的代碼有什麼問題?第二個JcomboBox只顯示一個項目,即使數據庫中有更多項目。 這應該如何工作: 第一個JcomboBox充滿了來自數據庫的數據,這取決於用戶在第一個comboBox上的選擇,第二個JcomboBox被填充,然後一些Jlabel根據從第二個選擇的項目填充數據JComboBox中。JcomboBox只顯示一個項目

我試過如果(o == comboBox2)仍然是同樣的問題。

@Override 
public void actionPerformed(ActionEvent e) { 
    Object o = e.getSource(); 
    if(o == comboBox1) {    
     Object matricule = comboBox1.getSelectedItem(); 
     String sqll = "Select * FROM clients WHERE matricule = " +matricule;    

      try { 
       rs = stat.executeQuery(sqll); 
       while(rs.next()) { 
        nom.setText(rs.getString("nom")); 
        prenom.setText(rs.getString("prenom")); 
        cin.setText(rs.getString("cin")); 
        adresse.setText(rs.getString("adresse"));     
       } 
      } catch (SQLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

     comboBox2.removeAllItems(); 

     Object index = comboBox1.getSelectedItem(); 
     String sql = "Select distinct p.code FROM parcelle p, clients c WHERE c.matricule = p.exploitant AND c.matricule = " +index; 

      try { 
       rs = stat.executeQuery(sql); 
       while(rs.next()) { 
        System.out.println(rs.getInt("code")); 
        comboBox2.addItem(rs.getInt("code")); 
       } 
      } catch (SQLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      }   
     return; 
    }  

     Object code = comboBox2.getSelectedItem(); 
     String sqll = "Select * FROM parcelle WHERE code = " +code; 

     try { 
      rs = stat.executeQuery(sqll);      

      while(rs.next()) { 
       sau.setText(rs.getString("sau")); 
       sol.setText(rs.getString("type_sol")); 
       irrigation.setText(rs.getString("mode_irrigation")); 
       exploitation.setText(rs.getString("type_exploitation"));      
      } 

     } catch (SQLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
     }  
} 
+1

還有更多的數據庫,但你能確認你的查詢返回多行嗎? –

+0

是的,我試圖通過phpMyAdmin查詢,並返回所有現有的行。 http://i46.tinypic.com/2uyr6e8.jpg –

+0

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。對數據進行硬編碼。 –

回答

1

你能否確認'code'的值在任何記錄中都不爲空?從事物的聲音來看,第二條記錄中'code'的值爲空(或其他任何不能被解析爲int的東西),這會導致異常。

+0

感謝您的回覆, 不存在空值。 –

+0

「代碼」的任何字母數字值如何? –

+0

不,所有'code'的值都是int。 –