2011-08-17 8 views
1

이 시나리오에서 가장 좋은 해결책은 무엇일까요? Netbeans에서 구현 된 SOAP 기반 웹 서비스에서 클라이언트가 여러 체크 박스를 클릭 한 다음 서버로 전송해야합니다. 저장됩니다. 가정 내 웹 서비스 전부 또는 일부를 선택할 수 있습니다이 체크 박스가 있습니다여러 체크 박스의 값을 서버에 전달하는 방법

인종 : 1.Caucasian 2.South - 이스트 아시아 3.South 아시아 4.African 5. 기타

다른 년을 같은 웹 서비스의 일부 제가

성별과 관련된 체크 박스 구현 : 다음과 같이 1.Male 2.Female

어디 하나 또는 모두을 선택할 수 있지만 솔루션은 Ethnicity 부분에 대해 나에게 매우 복잡해 보입니다. 더 많은 체크 박스가있는 다른 부분이 있습니다!

클라이언트 측 코드 : 서버 측에 관련

 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(); 
당신은 다음과 같은 체크 박스의 배열에 여러 확인란을 변환 시작해야

: 그럼 당신은 루프에서 상자에 ItemListener을 추가 할 수 있습니다

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

:

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

selectedEthnicities 배열 목록을 항목 수신기에 전달했음을 유의하십시오. 이제 EthnicityItemListener 항목 청취자의 구현은 다음과 같이 보일 수 있습니다 : selectedEthnicities 변수 클래스는 SQL 문을 만들

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

을 반복 할 수 있습니다. 이렇게하면 불필요한 코드 복잡성을 줄일 수 있습니다.

+0

대단히 감사합니다. 저는 여전히 여러분의 solution.The 문제를 통합하기 위해 노력하고 있습니다. 그래서 Netbeans의 디자인 모드에서 인터페이스를 생성 했으므로 코드 생성기가 많이 있습니다 ... 다시 한번 감사드립니다! – rosa

관련 문제