跪求,解决方案

dengkuilin 2009-12-10 09:38:11
自己编了一个程序,程序的功能是在三个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);
}

}

}
}
相应的数据库是,里面的表是;表名;垫江县;晓兴乡,周家乡
表名;垫江县;属性列;县,晓兴乡,周家乡
表名;梁平县;属性列;县,大岩乡,阴平乡

表名;晓兴乡;属性列;乡,努力村,幸福村
表名;周家乡;属性列;乡,建国村,努力村

表名;大岩乡;属性列;乡,和谐村,美好村
表名;阴平乡;属性列;乡,我们村,他们村
...全文
103 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
小梁_unique 2009-12-11
  • 打赏
  • 举报
回复
捡分
ln110119 2009-12-11
  • 打赏
  • 举报
回复
JF
qingzhe2008 2009-12-11
  • 打赏
  • 举报
回复

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

}

}
}
crazylaa 2009-12-11
  • 打赏
  • 举报
回复
解决了?jf
qingzhe2008 2009-12-11
  • 打赏
  • 举报
回复
捡分!
dengkuilin 2009-12-11
  • 打赏
  • 举报
回复
那个是老师的要求,现在解决了,谢谢了哈?
caili314 2009-12-11
  • 打赏
  • 举报
回复
if(box1==e.getSource()){
...
getContentPane().removeAll();
add(box1);
add(box2);
add(box3);

为什么要removeAll()再add()?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧