当前位置:文档之家› java通讯录管理系统

java通讯录管理系统

//数据库book.mdb放在c盘,表user(字段:id name phone) admin(字段:id username password)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class book extends JFrame implements ActionListener {
static JTextField txtname = new JTextField(5);
static JTextField txtphone = new JTextField(11);
static JTextField txtuser = new JTextField(10);
static JTextField txtpass = new JTextField(10);
static List list = new List();
static Frame main = new Frame("通讯录");
static Frame login = new Frame("用户登陆");
public book() {

JButton add = new JButton("增加");
JButton edit = new JButton("修改");
JButton find = new JButton("查找");
JButton del = new JButton("删除");
JLabel labname = new JLabel("姓名:");
JLabel labphone = new JLabel("电话:");
main.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));// 居左对齐,间距10
main.add(list);
main.add(labname);
main.add(txtname);
main.add(labphone);
main.add(txtphone);
main.add(add);
add.addActionListener(this);
main.add(edit);
edit.addActionListener(this);
main.add(find);
find.addActionListener(this);
main.add(del);
del.addActionListener(this);
main.pack();
//main.setVisible(true);
main.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// list 鼠标事件
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {// 获取选中的列表,分割姓名和电话,分别赋值给文本框
if (list.getSelectedIndex() != -1) {
String info = list.getSelectedItem();
txtname.setText(info.subSequence(info.indexOf("姓") + 3,
info.indexOf("电")).toString());
txtphone.setText(info.substring(info.indexOf("电") + 3));
}
}
}
});
JButton buttlogin = new JButton("登陆");
JButton buttzhuce = new JButton("注册");


login.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));// 居左对齐,间距10
JLabel labuser = new JLabel("账号:");
JLabel labpass = new JLabel("密码:");
login.add(labuser);
login.add(txtuser);
login.add(labpass);
login.add(txtpass);
login.add(buttlogin);
buttlogin.addActionListener(this);
login.add(buttzhuce);
buttzhuce.addActionListener(this);
login.pack();
login.setVisible(true);
login.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

}

@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();

if (button.getText().equals("增加")) {
ExecuteSQL("insert into user(name,phone) values('"
+ txtname.getText() + "','" + txtphone.getText() + "')");
list.clear();
ExecuteSQL("select * from user");
txtname.setText("");

txtphone.setText("");
} else if (button.getText().equals("修改")) {

String info = list.getSelectedItem();// 获取当前所选的记录
String id = info.substring(0, info.indexOf("姓") - 1);// 提取id
System.out.print(id);
ExecuteSQL("update user set name='" + txtname.getText() + "'"
+ ",phone='" + txtphone.getText() + "' where id=" + id);
list.clear();
ExecuteSQL("select * from user");
} else if (button.getText().equals("查找")) {
list.clear();
ExecuteSQL("select * from user where name like'%"
+ txtname.getText() + "%' and phone like'%"
+ txtphone.getText() + "%'");
} else if (button.getText().equals("删除")) {

ExecuteSQL("delete * from user where name='" + txtname.getText()
+ "'");
list.clear();
ExecuteSQL("select * from user");
txtname.setText("");
txtphone.setText("");
} else if (button.getText().equals("登陆")) {
ExecuteSQL("select * from admin where username='" + txtuser.getText() + "' and password='"+txtpass.getText()+"'");
if(list.getItemCount()>0){
list.clear();
ExecuteSQL("select * from user");
main.setVisible(true);login.setVisible(false);
}

}
else if (button.getText().equals("注册")) {
ExecuteSQL("insert into admin (username,password)values('"+txtuser.getText()+"','"+txtpass.getText()+"')");
login.setTitle("注册成功!请登录");
}
}

public static void main(String args[]) {
new book();

}

public static void ExecuteSQL(String SQL) {
Connection con;
Statement sql; // 声明Statement对象
ResultSet rs;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.out.println("" + e);
}
try {
String dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\book.mdb";// 此为NO-DSN方式
con = DriverManager.getConnection(dburl);
sql = con.createStatement();
if (SQL.indexOf("select") == -1)// 如果不是查询语句执行即可
{
sql.execute(SQL);
con.close();// 必须关闭
return;
}
try {

rs = sql.executeQuery(SQL);
while (rs.next()) {
String id = rs.getString(1);
String name = rs.getString(2); // 获得数据库第2列
String phone = rs.getString(3);
list.setSize(200, 100);// 设置列表大小,必须放在这
list.add(id + " 姓名:" + name + "电话:" + phone);
}
con.close();
} catch (SQLException el) {
}

} catch (SQLException el) {
}
}
}

相关主题
文本预览
相关文档 最新文档