用Java编写的图形用户界面通讯录package myaddressbody;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
public class Demonstrationacction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (((JButton) e.getSource()).getText().equals("显示查询结果")) {
String dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=main.mdb";
String sql;
Connection con;
Statement stmt;
String nm, sx, mp, ph, em, ca, ha, nb, gp, lbMQ, all;
boolean bdr;
Date bd;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (java.lang.ClassNotFoundException e2) {
System.err.print("ClassNotFoundException:");
System.err.println(e2.getMessage());
}
try {
con = DriverManager.getConnection(dburl, "", "");
stmt = con.createStatement();
sql = "SELECT*FROM address";
ResultSet ws = stmt.executeQuery(sql);
while (ws.next()) {
nm = ws.getString(1);
sx = ws.getString(2);
bd = ws.getDate(3);
mp = ws.getString(4);
ph = ws.getString(5);
ca = ws.getString(6);
em = ws.getString(7);
ha = ws.getString(8);
gp = ws.getString(9);
nb = ws.getString(10);
lbMQ = ws.getString(11);
all = "姓名:" + nm + " 性别:" + sx + " 生日:" + bd
+ " 移动电话:" + mp + " 固定电话:" + ph + " MSN/QQ:"
+ lbMQ + " 公司地址:" + ca + " EMail:" + em
+ " 家庭地址:" + ha + " 组别" + gp + " 备注:" + nb
+ "\n";
Demonstrationaframe.lbdem.append(all);
}
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println("Exception:" + ex.getMessage());
}
}
if (((JButton) e.getSource()).getText().equals("退出全部查询")) {
Demonstrationaframe.lbdem.setText("");
Demonstrationaframe.demFrame.setVisible(false);
}
}
}
1