自己编了一个程序,程序的功能是在三个JComboBox 对象里面,选择对应的项目,但是总是不能达到我的目的;就是依次选择
县,乡,村,程序的代码,是;
import javax.swing.*; 
import java.awt.event.*;
import java.sql.*; 
public class Example12_2 {
public static void main(String args[]){
 try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
         }
         catch(ClassNotFoundException e1){
               System.out.println(""+e1);
         }
           new Mysql();
}
}
class Mysql extends JFrame implements ItemListener {
 JComboBox box1;
 JComboBox box2;
JComboBox box3;
Connection con;
 Statement sql;
 ResultSet rs;
   Connection con1;
 Statement sql1;
 ResultSet rs1;
 
Mysql(){
     box1=new  JComboBox();
     box2=new  JComboBox();
     box3=new  JComboBox();
    setBounds(100,100,380,200);
        setVisible(true);  
        setLayout(null);
        box1.addItem("请选择县");
        box1.addItem("垫江县");
        box1.addItem("梁平县");
        box2.addItem("请选择乡");
        box3.addItem("请选择村");
        add(box1);
        add(box2);
        add(box3);
        box1.setBounds(20, 50, 100, 50);
        box2.setBounds(140, 50, 100, 50);
        box3.setBounds(260, 50, 100, 50);
        validate();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        box1.addItemListener(this);
        box2.addItemListener(this);
         
         
 }
 public void itemStateChanged(ItemEvent e){
          if(box1==e.getSource()){
          box2.removeAllItems();
                box3.removeAllItems();
              box2.addItem("请选择乡");
            box3.addItem("请选择村");
           
            String  str=box1.getSelectedItem().toString();
               
            String  str1="SELECT * FROM "+str;
                
              
               try{ con=DriverManager.getConnection("jdbc:odbc:eleven","","");
               sql= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
               rs=sql.executeQuery(str1);
                    
                       while(rs.next()){
                       box2.addItem( rs.getString(1).trim());
                      
                       }
                      
                      con.close();
                       getContentPane().removeAll();
                       add(box1);
                       add(box2);
                       add(box3);
                       validate();
                      
               }
              
               catch(SQLException e2){
                          System.out.println(e2);
              
               }
           
           }
               
          
        
          else if(box2==e.getSource()){
         
               box3.removeAllItems();
               box3.addItem("请选择村");
              String  str=box2.getSelectedItem().toString();
              
              String  str1="SELECT * FROM "+str;
               
             
              try{ con=DriverManager.getConnection("jdbc:odbc:eleven","","");
              sql= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
              rs=sql.executeQuery(str1);
                      while(rs.next()){
                      box3.addItem( rs.getString(1));
                     
                      }
                   
                      con.close();
                      
              }
              catch(SQLException e2){
                         System.out.println(e2);
                   }
             
               }
        
}
}
相应的数据库是,里面的表是;表名;垫江县;晓兴乡,周家乡
表名;垫江县;属性列;县,晓兴乡,周家乡
表名;梁平县;属性列;县,大岩乡,阴平乡表名;晓兴乡;属性列;乡,努力村,幸福村 
表名;周家乡;属性列;乡,建国村,努力村表名;大岩乡;属性列;乡,和谐村,美好村
表名;阴平乡;属性列;乡,我们村,他们村

解决方案 »

  1.   

    if(box1==e.getSource()){ 
    ...
     getContentPane().removeAll();
     add(box1);
     add(box2);
     add(box3); 为什么要removeAll()再add()?
      

  2.   


    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;
    public class Example12_2 {
    public static void main(String args[]){
    try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
            }
            catch(ClassNotFoundException e1){
                  System.out.println(""+e1);
            }
              new Mysql();
    }
    }
    class Mysql extends JFrame implements ItemListener {
    JComboBox box1;
    JComboBox box2;
    JComboBox box3;
    Connection con;
    Statement sql;
    ResultSet rs;
    Connection con1;
    Statement sql1;
    ResultSet rs1;Mysql(){
      box1=new  JComboBox();
      box2=new  JComboBox();
      box3=new  JComboBox();
      setBounds(100,100,380,200);
            setVisible(true); 
            setLayout(null);
            box1.addItem("请选择县");
            box1.addItem("垫江县");
            box1.addItem("梁平县");
            box2.addItem("请选择乡");
            box3.addItem("请选择村");
            add(box1);
            add(box2);
            add(box3);
            box1.setBounds(20, 50, 100, 50);
            box2.setBounds(140, 50, 100, 50);
            box3.setBounds(260, 50, 100, 50);
            validate();
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            box1.addItemListener(this);
            box2.addItemListener(this);
           
           
    }
    public void itemStateChanged(ItemEvent e){
            if(box1==e.getSource()){
            box2.removeAllItems();
                  box3.removeAllItems();
                box2.addItem("请选择乡");
              box3.addItem("请选择村");
             
              String  str=box1.getSelectedItem().toString();
                 
              String  str1="SELECT * FROM "+str;
                 
                 
                  try{ con=DriverManager.getConnection("jdbc:odbc:eleven","","");
                  sql= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
                  rs=sql.executeQuery(str1);
                       
                          while(rs.next()){
                          box2.addItem( rs.getString(1).trim());
                         
                          }
                         
                          con.close();
                          getContentPane().removeAll();
                          add(box1);
                          add(box2);
                          add(box3);
                          validate();
                         
                  }
                 
                  catch(SQLException e2){
                              System.out.println(e2);
                 
                  }
           
            }
               
           
           
            else if(box2==e.getSource()){
           
                  box3.removeAllItems();
                  box3.addItem("请选择村");
                String  str=box2.getSelectedItem().toString();
                 
                String  str1="SELECT * FROM "+str;
               
               
                try{ con=DriverManager.getConnection("jdbc:odbc:eleven","","");
                sql= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
                rs=sql.executeQuery(str1);
                          while(rs.next()){
                        box3.addItem( rs.getString(1));
                       
                          }
                     
                          con.close();
                         
                }
                catch(SQLException e2){
                            System.out.println(e2);
                      }
               
                }
           
    }