电话簿管理系统设计与实现
-
功能需求
电话号码查询 增加 删除 修改 统计窗体增加记录到数据表。与数据库建立连接。
界面设计:
登录界面
public class Login extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
// TODO 自动生成的方法存根
JPanel jp1,jp2,jp3;
JLabel jl1,jl2;
JPasswordField jpw1;
JTextField jtf1;
JRadioButton jr1,jr2;
ButtonGroup group;
JButton jb1,jb2;
Login(){
init();
this.setSize(400,300);
this.setLocation(500,300);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init() {
this.setLayout(new GridLayout(3,1));
jl1=new JLabel("用户名:");
jtf1=new JTextField(10);
jp1= new JPanel();
jp1.add(jl1);
jp1.add(jtf1);
this.add(jp1);
jl2=new JLabel("密码:");
jpw1=new JPasswordField(10);
jpw1.setEchoChar('*');
jp2= new JPanel();
jp2.add(jl2);
jp2.add(jpw1);
this.add(jp2);
jb1=new JButton("登录",new ImageIcon("D:\\Test\\png\\b.png"));
jb1.setBackground(Color.pink);
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent agr0) {
String s1,s2;
s1=jtf1.getText();
s2=new String(jpw1.getPassword());
if(s1.equals("dldx") && s2.equals("123456")) {
Phone ph=new Phone();//跳转到Phone界面
}
else {
JOptionPane.showMessageDialog(null, "登录错误!");
}
}
});
jb2=new JButton("取消");
jb2.setBackground(Color.pink);
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent agr0) {
System.exit(0);
}
});//重写关闭窗体的方法
jp3=new JPanel();
jp3.add(jb1);
jp3.add(jb2);
this.add(jp3);
}
}
数据管理窗口
public class Phone extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar mb1;
JMenu m1;
JMenuItem item1,item2,item3,item4,item5,item6;
ActionListener ls1=new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getSource()==item6) {//事件处理
System.exit(0);
}
if(e.getSource()==item1) {//事件处理
@SuppressWarnings("unused")
FindPhone fp=new FindPhone();
}
if(e.getSource()==item2) {//事件处理
@SuppressWarnings("unused")
AddPhone fp=new AddPhone();
}
if(e.getSource()==item3) {
@SuppressWarnings("unused")
UpdatePhone fp=new UpdatePhone();
}
if(e.getSource()==item4) {
@SuppressWarnings("unused")
DeletePhone fp=new DeletePhone();
}
if(e.getSource()==item5) {
@SuppressWarnings("unused")
AllPhone fp=new AllPhone();
}
}
};
Phone(){
init();
this.setLocation(300,300);
this.setSize(400,300);
this.setTitle("数据管理窗口");
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init() {
mb1=new JMenuBar();
m1=new JMenu("电话薄管理系统");
item1=new JMenuItem("电话号码查询",new ImageIcon("D:\\Test\\png\\a.png"));//添加按钮小图标
item1.setBackground(Color.pink);
item1.addActionListener(ls1);
item2=new JMenuItem("电话号码增加",new ImageIcon("D:\\Test\\png\\a.png"));
item2.setBackground(Color.pink);
item2.addActionListener(ls1);
item3=new JMenuItem("电话号码修改",new ImageIcon("D:\\Test\\png\\a.png"));
item3.setBackground(Color.pink);
item3.addActionListener(ls1);
item4=new JMenuItem("电话号码删除",new ImageIcon("D:\\Test\\png\\a.png"));
item4.setBackground(Color.pink);
item4.addActionListener(ls1);
item5=new JMenuItem("电话号码统计",new ImageIcon("D:\\Test\\png\\a.png"));
item5.setBackground(Color.pink);
item5.addActionListener(ls1);
item6=new JMenuItem("退 出",new ImageIcon("D:\\Test\\png\\a.png"));
item6.setBackground(Color.pink);
item6.addActionListener(ls1);
m1.add(item1);
m1.addSeparator();
m1.add(item2);
m1.addSeparator();
m1.add(item3);
m1.addSeparator();
m1.add(item4);
m1.addSeparator();
m1.add(item5);
m1.addSeparator();
m1.add(item6);
mb1.add(m1);
this.setJMenuBar(mb1);
ImageIcon backbround = new ImageIcon("D:\\Test\\png\\c.jpg");
//将图片添加到JLable标签
JLabel jlabel = new JLabel(backbround);
//设置标签的大小
jlabel.setBounds(0,0, getWidth(),getHeight() );
//将图片添加到窗口
add(jlabel);
}
}
查询窗口
文本框空白时
查询结果:
public class FindPhone extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel jl1;
JTextField jtf1;
JButton jb1;
JTextArea jta1;
FindPhone(){
init();
this.setSize(400,300);
this.setLocation(500, 300);
this.setTitle("电话号码查询");
this.setVisible(true);//设置窗口可见性
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init() {
this.setLayout(new FlowLayout());//设置窗口布局
jl1=new JLabel("请输入姓名:");//创建标签
jtf1=new JTextField(10);//创建文本框对象
jb1=new JButton("查询",new ImageIcon("D:\\Test\\png\\b.png"));//为按钮添加图标
jb1.setBackground(Color.pink);//设置按钮颜色
jta1=new JTextArea(30,25);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
Connection con;//桥
Statement sql=null;//收费员
ResultSet rs;//前端数据集
con=GetDBConnection.connectDB("mysql", "root", "123456");
if(con==null) {
return;
}
String strsql,str1;
str1=jtf1.getText().trim();
if(str1.equals("")) {
strsql="select * from Telephone_info";
}
else {
strsql="select phonenumber from Telephone_info where name='"+str1+"'";
}
try {
sql=con.createStatement();
rs=sql.executeQuery(strsql);
jta1.setText(" ");
while(rs.next()) {
int phonenumber=rs.getInt(1);
jta1.append(String.valueOf(phonenumber)+"\n");
//String name=rs.getString(1);
String sex=rs.getString(2);
int phonenumber=rs.getInt(3);
String unit=rs.getString(4);
String address=rs.getString(5);
jta1.append(name+" "+sex+" "+String.valueOf(phonenumber)+" "+unit+" "+address+"\n");
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});//重写
this.add(jl1);
this.add(jtf1);
this.add(jb1);
this.add(new JScrollPane(jta1));
}
}
修改信息窗口
public class UpdatePhone extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel jl1,jl2,jl3,jl4,jl5;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5;
JButton jb1,jb2;
JTextArea jta1;
Connection con;
java.sql.Statement sql;
java.sql.ResultSet rs;
UpdatePhone(){
init();
this.setTitle("电话号码修改");
this.setSize(600,300);
this.setLocation(600,400);
this.setVisible(true);//窗体可见性
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init() {
this.setLayout(new FlowLayout());//设置窗体布局
jl1=new JLabel("请输入(已有)姓名:");
jtf1=new JTextField(10);
jl2=new JLabel("请输入要修改的性别:");
jtf2=new JTextField(10);
jl3=new JLabel("请输入要修改的电话号码:");
jtf3=new JTextField(10);
jl4=new JLabel("请输入要修改的单位:");
jtf4=new JTextField(10);
jl5=new JLabel("请输入要修改的家庭地址:");
jtf5=new JTextField(10);
jta1=new JTextArea(300,200);
jb1=new JButton("更新",new ImageIcon("D:\\Test\\png\\f.png"));//为按钮添加小图标
jb1.setBackground(Color.pink);//添加按钮颜色
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
con=(Connection) GetDBConnection.connectDB("mysql","root","123456");
if(con==null) return;
try {
sql=con.createStatement();
String str="Update Telephone_info set sex='"+jtf2.getText().trim()+"',phonenumber='"+jtf3.getText().trim()+"',unit='"+jtf4.getText().trim()+"',address='"+jtf5.getText().trim()+"'where name='"+jtf1.getText().trim()+"";
int k=sql.executeUpdate(str);
if(k>0)
System.out.println("修改成功");
else
System.out.println("修改不成功");
} catch (Exception e) {
e.printStackTrace();
}
}
});
jb2=new JButton("查询",new ImageIcon("D:\\Test\\png\\b.png"));//为按钮添加小图标
jb2.setBackground(Color.pink);//添加按钮颜色
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
Connection con;//桥
Statement sql;//收费员
ResultSet rs;//前端数据集
con=(Connection) GetDBConnection.connectDB("mysql", "root", "123456");
if(con==null) {
return;
}
String strsql,str1;
str1=jtf1.getText().trim();
if(str1.equals("")) {
strsql="select * from Telephone_info";
}
else {
strsql="select *from Telephone_info where name='"+str1+"'";
}
try {
sql=(Statement) con.createStatement();
rs=(ResultSet) sql.executeQuery(strsql);
jta1.setText(" ");
while(rs.next()) {
String name=rs.getString(1);
String sex=rs.getString(2);
int phonenumber=rs.getInt(3);
String unit=rs.getString(4);
String address=rs.getString(5);
jta1.append(name+" "+sex+" "+String.valueOf(phonenumber)+" "+unit+" "+address+"\n");
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});//重写窗体方法
this.add(jl1);//添加到容器
this.add(jtf1);
this.add(jl2);
this.add(jtf2);
this.add(jl3);
this.add(jtf3);
this.add(jl4);
this.add(jtf4);
this.add(jl5);
this.add(jtf5);
this.add(jb1);
this.add(jb2);//添加到容器
this.add(new JScrollPane(jta1));
}
}
删除窗口:
class DeletePhone extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel jl1;
JTextField jtf1;
JButton jb1,jb2;
JTextArea jta1;
Connection con;
Statement sql;
public DeletePhone(){
init();
this.setSize(400,300);
this.setLocation(300,500);
this.setTitle("电话号码删除");
this.setVisible(true);//窗体可见性
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init() {
setLayout(new FlowLayout());//设置布局
jl1=new JLabel("请输入要删除的姓名:");
jtf1=new JTextField(10);
jta1=new JTextArea(10,20);
jb1=new JButton("删除",new ImageIcon("D:\\Test\\png\\f.png"));//为按钮添加小图标
jb1.setBackground(Color.pink);//添加按钮颜色
jb1.addActionListener(new ActionListener() {
@SuppressWarnings("null")
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
con=(Connection) GetDBConnection.connectDB("mysql","root","123456");
if(con==null) return;
try {
sql=(Statement) con.createStatement();
String str="Delete from Telephone_info where name='"+jtf1.getText().trim()+"'";
jta1.setText(" ");
System.out.println(str);
int k=sql.executeUpdate(str);
if(k>0)
System.out.println("删除成功");
else
System.out.println("删除不成功");
} catch (Exception e) {
e.printStackTrace();
}
}
});
this.add(jl1);
this.add(jtf1);
this.add(jb1);
this.add(new JScrollPane(jta1));
}
}
增加信息窗口
public class AddPhone extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel jl1,jl2,jl3,jl4,jl5;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5;
JButton jb1,jb2;
JTextArea jta1,jta2;
Connection con;
java.sql.Statement sql;
java.sql.ResultSet rs;
public AddPhone(){
init();
this.setSize(600,300);
this.setLocation(600,400);
this.setTitle("电话号码添加");
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init() {
this.setLayout(new FlowLayout());
jl1=new JLabel("请输入要增加的姓名:");
jtf1=new JTextField(10);
jl2=new JLabel("请输入要增加的性别:");
jtf2=new JTextField(10);
jl3=new JLabel("请输入要增加的电话号码:");
jtf3=new JTextField(10);
jl4=new JLabel("请输入要增加的单位:");
jtf4=new JTextField(10);
jl5=new JLabel("请输入要增加的家庭地址:");
jtf5=new JTextField(10);
jta1=new JTextArea(100,20);
jb1=new JButton("增加",new ImageIcon("D:\\Test\\png\\f.png"));
jb1.setBackground(Color.pink);
jb1.addActionListener(new ActionListener() {
@SuppressWarnings("null")
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
con=(Connection) GetDBConnection.connectDB("mysql","root","123456");
if(con==null) return;
try {
sql=con.createStatement();
String str="insert into Telephone_info values('"+jtf1.getText().trim()+"','"+jtf2.getText().trim()+"',"+jtf3.getText().trim()+"','"+jtf4.getText().trim()+"','"+jtf5.getText().trim()+"')";
System.out.println(str);
int k=sql.executeUpdate(str);
if(k>0)
System.out.println("增加成功");
else
System.out.println("增加不成功");
} catch (Exception e) {
e.printStackTrace();
}
}
});
jb2=new JButton("查询",new ImageIcon("D:\\Test\\png\\b.png"));//按钮小图标
jb2.setBackground(Color.pink);
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
Connection con;//桥
Statement sql;//收费员
ResultSet rs;//前端数据集
con=(Connection) GetDBConnection.connectDB("mysql", "root", "123456");
if(con==null) {
return;
}
String strsql,str1;
str1=jtf1.getText().trim();
if(str1.equals("")) {
strsql="select * from Telephone_info";
}
else {
strsql="select * from Telephone_info where name='"+str1+"'";
}
try {
sql=(Statement) con.createStatement();
rs=(ResultSet) sql.executeQuery(strsql);
jta1.setText(" ");
while(rs.next()) {
String name=rs.getString(1);
String sex=rs.getString(2);
int phonenumber=rs.getInt(3);
String unit=rs.getString(4);
String address=rs.getString(5);
jta1.append(name+" "+sex+" "+String.valueOf(phonenumber)+" "+unit+" "+address+"\n");
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
this.add(jl1);
this.add(jtf1);
this.add(jl2);
this.add(jtf2);
this.add(jl3);
this.add(jtf3);
this.add(jl4);
this.add(jtf4);
this.add(jl5);
this.add(jtf5);
this.add(jb1);
this.add(jb2);
this.add(new JScrollPane(jta1));
}}
统计窗口(作业要求统计男女生人数)
public class AllPhone extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
protected static final int NULL = 0;
JLabel jl1;
JTextField jtf1;
JButton jb1;
JTextArea jta1;
Connection con;
AllPhone(){
this.setTitle("电话号码统计");
this.setSize(600,300);
this.setLocation(600,400);
this.setVisible(true);//设置窗口可见性
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init() {
this.setLayout(new FlowLayout());//设置窗口布局
jl1=new JLabel("人数统计");//使用文本创建标签
jtf1=new JTextField(0);//创建文本框对象
jb1=new JButton("统计",new ImageIcon("D:\\Test\\png\\f.png"));//为按钮添加小图标
jb1.setBackground(Color.pink);//添加按钮颜色
jta1=new JTextArea(100,25);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
Connection con;//桥
java.sql.Statement sql;//收费员
java.sql.ResultSet rs;//前端数据集
con=(Connection) GetDBConnection.connectDB("mysql","root","123456");
if(con==null)
return;
try {
sql=con.createStatement();
String strsql;
strsql="SELECT COUNT(*) num,CASE sex WHEN '1' THEN '男' WHEN '0' THEN '女' END AS name FROM Telephone_info GROUP BY sex";
rs=sql.executeQuery(strsql);
jta1.setText(" ");
while(rs.next()) {
String sex=rs.getString(1);
jta1.append(sex+'\n');
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});//通过内部类重写关闭窗体的方法
this.add(jl1);//添加到容器
this.add(jtf1);//添加到容器
this.add(jb1);//添加到容器
this.add(new JScrollPane(jta1));
}
}
基本窗口功能运行完毕。
实验注意:
主键name为字符串型,应用SQL语句字符串索引应加上引号,不加引号,索引失败。一开始SQL语句strsql="select * from student where name="+str1;为注意name是字符串型,导致代码运行错误,经查询修改语句strsql="select phonenumber from Telephone_info where name='"+str1+"'";后代码运行成功。
添加窗体背景和按钮图片时,注意图片绝对路径应与项目包同一路径,否则背景添加失败。