public void cInit(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:myDB","sa",""); sta = con.createStatement(1005,1008); sql = "select * from city"; res = sta.executeQuery(sql);
res.last(); int n = res.getRow(); sCity = new String[n]; res.first(); for(int i=0;i<n;i++){ sCity[i] = res.getString(2); res.next(); } city.setListData(sCity); city.setSelectedIndex(0); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } } public void tInit(String ss){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:myDB","sa",""); sta = con.createStatement(1005,1008); sql = "select * from tech where cid=(select cid from city where city='"+ss+"')"; res = sta.executeQuery(sql);
res.last(); int n = res.getRow(); sTech = new String[n]; res.first(); for(int i=0;i<n;i++){ sTech[i] = res.getString(3); res.next(); } tech.setListData(sTech); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } } public void jtaInit(String ss){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:myDB","sa",""); sta = con.createStatement(1005,1008); sql = "select * from tech where tc='"+ss+"'"; res = sta.executeQuery(sql);
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class MyC extends JFrame implements MouseListener{
JList city = new JList();
JList tech = new JList();
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
String[] sCity;
String[] sTech;
Connection con;
Statement sta;
ResultSet res;
String sql;
public MyC() {
this.setLayout(new GridLayout(1,3));
this.add(city);
this.add(tech);
this.add(jsp);
city.setBorder(BorderFactory.createTitledBorder("请选择城市"));
tech.setBorder(BorderFactory.createTitledBorder("请选择特产"));
jsp.setBorder(BorderFactory.createTitledBorder("特产简介"));
cInit();
tInit(city.getSelectedValue().toString());
city.addMouseListener(this);
tech.addMouseListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(320,240);
this.setVisible(true);
}
public void cInit(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:myDB","sa","");
sta = con.createStatement(1005,1008);
sql = "select * from city";
res = sta.executeQuery(sql);
res.last();
int n = res.getRow();
sCity = new String[n];
res.first();
for(int i=0;i<n;i++){
sCity[i] = res.getString(2);
res.next();
}
city.setListData(sCity);
city.setSelectedIndex(0);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
} public void tInit(String ss){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:myDB","sa","");
sta = con.createStatement(1005,1008);
sql = "select * from tech where cid=(select cid from city where city='"+ss+"')";
res = sta.executeQuery(sql);
res.last();
int n = res.getRow();
sTech = new String[n];
res.first();
for(int i=0;i<n;i++){
sTech[i] = res.getString(3);
res.next();
}
tech.setListData(sTech);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
} public void jtaInit(String ss){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:myDB","sa","");
sta = con.createStatement(1005,1008);
sql = "select * from tech where tc='"+ss+"'";
res = sta.executeQuery(sql);
while(res.next()){
jta.setText(res.getString(4));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
public void mouseExited(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
Object obj = e.getSource();
if(obj==city){
tInit(city.getSelectedValue().toString());
}else if(obj==tech){
jtaInit(tech.getSelectedValue().toString());
}
}
public void mouseReleased(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public static void main(String[] args) {
new MyC();
}
}
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class MyCity extends JFrame{
JComboBox cmbCity = new JComboBox();
JComboBox cmbTech = new JComboBox();
JPanel p = new JPanel();
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
Connection con;
Statement sta;
ResultSet res;
ResultSet res1;
ResultSet res2;
public MyCity() {
p.add(cmbCity);
p.add(cmbTech);
this.add(p,BorderLayout.NORTH);
this.add(jsp);
cityInit();
techInit(cmbCity.getSelectedItem().toString());
cmbCity.addActionListener(new AAA());
cmbTech.addActionListener(new AAA());
this.setSize(320,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void cityInit(){
try{
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con = DriverManager.getConnection("jdbc:odbc:wang","sa","");
// sta = con.createStatement(1005,1008);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs",
"sa",
"");
Statement sta=con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String s = "select * from City";
res = sta.executeQuery(s);
while(res.next()){
cmbCity.addItem(res.getString(2));
}
res.close();
sta.close();
con.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
public void jtaInit(String ss){
try{
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con = DriverManager.getConnection("jdbc:odbc:wang","sa","");
// sta = con.createStatement(1005,1008);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs",
"sa",
"");
Statement sta=con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE); String s = "select * from Tech where tc='"+ss+"'";
res2 = sta.executeQuery(s);
while(res2.next()){
jta.setText(res2.getString(4));
}
res2.close();
sta.close();
con.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
public void techInit(String ss){
try{
int n=cmbTech.getItemCount();
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con = DriverManager.getConnection("jdbc:odbc:wang","sa","");
// sta = con.createStatement(1005,1008);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs",
"sa",
"");
Statement sta=con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE); String s = "select * from Tech where Cid=(select Cid from City where City='"+ss+"')" ;
res1 = sta.executeQuery(s);
while(res1.next()){
cmbTech.addItem(res1.getString(3));
}
for(int i=0;i<n;i++){
cmbTech.removeItemAt(0);
}
res1.close();
sta.close();
con.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
} public static void main(String[] args) {
new MyCity();
}
class AAA implements ActionListener{
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj==cmbCity){
techInit(cmbCity.getSelectedItem().toString());
}
else if(obj==cmbTech){
if(cmbTech.getSelectedItem().toString()==null){
}
else{
jtaInit(cmbTech.getSelectedItem().toString());
}
}
}
}
}
import mseries.ui.*;
导入这两个包,然后调用对其中的日期控件类MDateEntryField进行封装即可