2011-08-17 33 views
1

這種情況最好的解決方案是什麼:我在Netbeans中實現了一個基於SOAP的web服務,客戶端應該點擊一些複選框,然後發送到服務器並存儲。假設我的web服務具有這些複選框的全部或部分可選用:應該如何將多個複選框的值傳遞給服務器

種族: 1.Caucasian 2.South-EST亞洲 3.South亞洲 4.African (五)其他

在另一相同的web服務的一部分我已經實現有關

性別的複選框: 1.Male 2.Female

正如以下其中之一或都可以選擇,但解決方案看起來非常複雜,我的種族部分,我有更多的複選框的其他部分!

客戶端代碼:

 private void salvaActionPerformed(java.awt.event.ActionEvent evt) {          
     // TODO add your handling code here: 
      disease=malattia.getText(); 
      etastr=eta.getText(); 
      age=java.lang.Integer.parseInt(etastr); 
      description=descrizione.getText(); 

     //Here i'm initiating the array using sexint as the dimension 
     sexarra=new String[sexint]; 
     if(sexint==1) 
     sexarra[0]=sexone; 
     else if(sexint==0) 
      JOptionPane.showMessageDialog(null, "Bisogna specificare  almeno un valore del campo sesso", "Errore", JOptionPane.ERROR_MESSAGE); 
     else{ 
      sexarra[0]=sexone; 
      sexarra[1]=sextwo;} 


     // I define the parameters and afterwards send them to server 
      Vector parametri = new Vector(); 
        parametri.addElement(new Parameter("malattia", String.class, disease, null)); 
       parametri.addElement(new Parameter("age", int.class, age, null)); 
       parametri.addElement(new Parameter("descrizione", String.class, description, null)); 
        parametri.addElement(new Parameter("sexarra",String[].class, sexarra, null)); 



     //Code related to calculating sexint which is used above as the dimension to array 


    private void femaleActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 

     if(female.isSelected()){ 
      if(sexint==0){ 
       sexint++; 
       sexone=female.getText(); 

      } 

      else if(sexint==1){ 
      sexint++; 

      sextwo=female.getText(); 
      } 
      else 
      sexint--; 
     } 
     }          

     private void maleActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
       if(male.isSelected()){ 
      if(sexint==0){ 
       sexint++; 
       sexone=male.getText(); 

      } 

      else if(sexint==1){ 
      sexint++; 

      sextwo=male.getText(); 
      } 
      else 
      sexint--; 
     } 
     }     

代碼與服務器端:

 public String aggiungi_malattia(String malattia, int eta, String descrizione, String[] sexarra) { 
       String ris = "no"; 
       String q = null, w = null; 
       String errore = connetti(); 

       if(sexarra.length == 2){ 
     q = "INSERT INTO malattia (nome, eta, descrizione, sesso) " 
        + "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[0] + "')"; 
     w="INSERT INTO malattia (nome, eta, descrizione, sesso) " 
        + "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[1] + "')"; 
       } 
     { 
      q = "INSERT INTO malattia (nome, eta, descrizione, sesso) " 
        + "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[0] + "')"; 

謝謝大家的時間和精力!

回答

0

創建一個列表或數組,以在類級別維護所選項目(字符串)的列表。

List<String> selectedEthnicities = new ArrayList(); 

你應該轉換多個複選框的複選框如數組開始:

JCheckBox[] ethnicities = new JCheckBox[5]; 
ethnicities[0] = new JCheckBox("USA"); 
ethnicities[1] = new JCheckBox("Brazil"); 
ethnicities[2] = new JCheckBox("Mexico"); 
ethnicities[3] = new JCheckBox("Canada"); 

然後你就可以在一個循環中添加ItemListener在箱子:

for(int i=0; i< ethnicities .length; i++) { 
    ethnicities [i].addItemListener(new EthnicityItemListener(selectedEthnicities)); 
} 

請注意,您已將selectedEthnicities數組列表傳遞給項監聽器。 現在EthnicityItemListener項偵聽器的實現可能是這樣的:

private class EthnicityItemListener implements ItemListener { 
     ArrayList selectedList; 
     private EthnicityItemListener(ArrayList<String> selectedEthnicities) { 
      this.selectedList = selectedEthnicities; 
     } 

     @Override 
     public void itemStateChanged(ItemEvent e) { 
      //add or remove selected chk box text to the list based on 
      //selection state. 
      if(e.getStateChange() == ItemEvent.SELECTED){ 
       this.selectedList.add(((JCheckBox)e.getSource()).getText()); 
      }else { 
       this.selectedList.remove(((JCheckBox)e.getSource()).getText()); 
      } 
      System.out.println(selectedList); 
     } 
    } 

類變量selectedEthnicities可以循環創建SQL語句。 這樣可以減少很多不必要的代碼複雜度。

+0

非常感謝您的詳細回覆,我仍然試圖完全整合您的解決方案。問題是我已經在Netbeans的設計模式中創建了接口,因此有很多機器生成的代碼...再次感謝! – rosa

相關問題