package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.util.LinkedList;
public class inquiryAddress extends JDialog {
//省,市县下拉选择框
JComboBox province =new JComboBox();
JComboBox city=new JComboBox();
JComboBox county=new JComboBox();
Box box1=Box.createHorizontalBox();
//确定地址信息
JButton ok=new JButton();
Dimension d=new Dimension(20,13);
Dimension dd=new Dimension(60,40);
Box box2=Box.createHorizontalBox();
//组装所有按键
Box box=Box.createVerticalBox();
LinkedList inquiry(String name) throws SQLException { //得到所有省信息
//获取地址信息
Connection conn = null;PreparedStatement ps=null; ResultSet rs=null;
LinkedList list=new LinkedList();
//从MySQL中读取表格内容
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/farmer", "root", "123456");
//获取执行mysql语句的执行对象
ps= conn.prepareStatement("select name from province ;");
// ps.setString(1,name);
rs=ps.executeQuery();
while (rs.next()) {
list.add(rs.getString("name"));
}
return list;
}
LinkedList inquiry2(String type) throws SQLException {
//获取地址信息
Connection conn = null;PreparedStatement ps=null; ResultSet rs=null;
LinkedList list=new LinkedList();
//从MySQL中读取表格内容
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/farmer", "root", "123456");
//获取执行mysql语句的执行对象
ps= conn.prepareStatement("select name from city where parent_code_id = (select no from poverty where name=?) ;");
ps.setString(1,type);
rs=ps.executeQuery();
while (rs.next()) {
list.add(rs.getString("name"));
}
return list;
}
LinkedList inquiry3(String type) throws SQLException {
//获取地址信息
Connection conn = null;PreparedStatement ps=null; ResultSet rs=null;
LinkedList list=new LinkedList();
//从MySQL中读取表格内容
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/farmer", "root", "123456");
//获取执行mysql语句的执行对象
ps= conn.prepareStatement("select name from county where parent_code_id = (select no from city where name=?) ;");
ps.setString(1,type);
rs=ps.executeQuery();
while (rs.next()) {
list.add(rs.getString("name"));
}
return list;
}
void setCombBox(){
province.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
try {
city.removeAllItems();
JComboBox com =(JComboBox)e.getSource();
for (String temp : inquiry2(com.getSelectedItem().toString())) {
city.addItem(temp);
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
});
city.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
try {
county.removeAllItems();
for (String temp:inquiry3(city.getSelectedItem().toString()))
county.addItem(temp);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
});
}
public inquiryAddress(JFrame jFrame,String title,boolean isMoel) throws SQLException {
super(jFrame,title,isMoel);
for (String temp:inquiry("province")) {
province.addItem(temp);
}
setCombBox();
//设置下拉框大小
province.setSize(d);city.setSize(d);county.setSize(d);
//组装视图
box1.add(province);box1.add(city);box1.add(county);
ok.setPreferredSize(dd);
box2.add(ok);
box.add(box1);box.add(box2);
add(box);
//设置窗口大小
this.setSize(500,500);
this.setName("农村人口统计系统");
this.setVisible(true);
}
}