基于JDBC的通讯录管理系统

文章目录

  • 1.设计阶段
    • 1.1需求分析
    • 1.2通讯录管理系统的功能图
    • 1.3通讯录管理系统的用例图
    • 1.4通讯录管理系统的E-R图
  • 2.数据库设计
  • 3.程序设计
    • 2.1function
      • 2.1.1设计ContactPerson类
      • 2.1.2设计DBUtils类
      • 2.1.3设计ContactHandle接口
      • 2.1.4设计ContactFunction类
    • 2.2ContactTest

1.设计阶段

1.1需求分析

通讯录中需要存储姓名,地址,电话号码,邮政编码,Email,家庭电话等信息。
程序应提供的基本管理功能有:
1、添加:增加一个人的记录(包括姓名,地址,电话号码,邮政编码,Email,家庭电话等信息)到通信录中。
2、显示:在屏幕上显示所有通信录中的联系人的全部信息(包括姓名,地址,电话号码,邮政编码,Email,家庭电话等信息)。
3、存储:将通讯录信息保存在数据库表中。
4、查询:可根据姓名查找某人的相关信息,若找到显示该联系人的其他信息(包括姓名,地址,电话号码,邮政编码,Email,家庭电话等信息)。
5、修改:输入一个人的姓名,若姓名存在,则对其他内容进行修改,若不存在则显示修改失败
6、排序:可以根据据条目的某个项对所有条目进行排序,如姓名。

1.2通讯录管理系统的功能图

基于JDBC的通讯录管理系统_第1张图片

1.3通讯录管理系统的用例图

基于JDBC的通讯录管理系统_第2张图片

1.4通讯录管理系统的E-R图

基于JDBC的通讯录管理系统_第3张图片

2.数据库设计

基于JDBC的通讯录管理系统_第4张图片

3.程序设计

2.1function

2.1.1设计ContactPerson类

package function;

//联系人类
public class ContactPerson {
    private String name;
    private String address;
    private String num;
    private String postal_code; //邮政编码
    private String e_mail;
    private String home_phon; //家庭电话

    public void setName(String name) {
        this.name = name;
    }

    public void setAdd(String address) {
        this.address = address;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public void setPostal_code(String postal_code) {
        this.postal_code = postal_code;
    }

    public void setE_mail(String e_mail) {
        this.e_mail = e_mail;
    }

    public void setHome_phon(String home_phon) {
        this.home_phon = home_phon;
    }

    public String getName() {
        return name;
    }

    public String getAdd() {
        return address;
    }

    public String getNum() {
        return num;
    }

    public String getPostal_code() {
        return postal_code;
    }

    public String getE_mail() {
        return e_mail;
    }

    public String getHome_phon() {
        return home_phon;
    }

    @Override
    public String toString() {
        return "ContactPerson{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", num='" + num + '\'' + ", postal_code='" + postal_code + '\'' + ", e_mail='" + e_mail + '\'' + ", home_phon='" + home_phon + '\'' + '}';
    }
}

2.1.2设计DBUtils类

package function;

import java.sql.*;

public class DBUtils {
    private static String url = "jdbc:mysql://127.0.0.1:3306/text?useSSL=false&useUnicode=yes&characterEncoding=UTF-8";
    private static String user = "root"; //数据库用户名
    private static String password = "123456"; //数据库密码

    public static Connection getConn() throws SQLException, ClassNotFoundException {
        //1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        //2.获得数据库的连接
        return DriverManager.getConnection(url, user, password);
    }

    public static void close(Statement stat, Connection conn, ResultSet res) {
        //关闭结果集
        if( stat != null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //关闭命令
        if( conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //关闭连接
        if( res != null){
            try {
                res.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

2.1.3设计ContactHandle接口

package function;//通讯录功能接口

import java.sql.SQLException;

public interface ContactHandle {
    public abstract void add(String name,String address,String num,String postal_code,String e_mail,String home_phon);
    //添加
    public abstract void remove(String name) throws SQLException, ClassNotFoundException;
    //删除
    public abstract void update(String name,String address,String num,String postal_code,String e_mail,String home_phon);
    //修改
    public abstract ContactPerson selectName(String name);
    //根据姓名查询
    public abstract ContactPerson selectNum(String num);
    //根据电话查询
    public abstract void sort() throws SQLException;
    //排序
    public abstract void show();
    //显示所有联系人
    public void save();
    //保存到本地
}

2.1.4设计ContactFunction类

package function;

import com.mysql.jdbc.Connection;
import java.io.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContactFunction implements ContactHandle {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet res;
    @Override
    public void add(String name, String address, String num, String postal_code, String e_mail, String home_phon) {
        try {
            conn = (Connection) DBUtils.getConn();
            String sql = "insert into conperson(name,address,num,postal_code,e_mail,home_phon) values (?,?,?,?,?,?)";
            pst=conn.prepareStatement(sql); // 预编译sql语句
            //repareStatement这个方法会将SQL语句加载到驱动程序conn集成程序中
            //但是并不直接执行,而是当它调用execute()方法的时候才真正执行
            pst.setString(1,name); //给第1个占位符赋值
            pst.setString(2,address);
            pst.setString(3,num);
            pst.setString(4,postal_code);
            pst.setString(5,e_mail);
            pst.setString(6,home_phon);
            pst.executeUpdate(); // 执行SQL语句
        } catch (SQLException e) {
        } catch (ClassNotFoundException e) {
        }finally {
            DBUtils.close(pst,conn,res);
        }
    }

    @Override
    public void remove(String name)  {
        try {
            conn = (Connection) DBUtils.getConn();
            String sql = "delete from conperson where name=?";
            pst=conn.prepareStatement(sql);// 预编译sql语句
            pst.setString(1,name);
            pst.executeUpdate();// 执行SQL语句
        } catch (SQLException e) {
        } catch (ClassNotFoundException e) {
        }finally {
            DBUtils.close(pst,conn,res);
        }
    }

    @Override
    public void update(String name,String address,String num,String postal_code,String e_mail,String home_phon) {
        ContactFunction contactFunction = new ContactFunction();
        contactFunction.remove(name);
        contactFunction.add(name,address,num,postal_code,e_mail,home_phon);
    }

    @Override
    public ContactPerson selectName(String name)  {
        ContactPerson person = null;
       try{
           conn = (Connection) DBUtils.getConn();
           String sql = "select * from conperson where name = ?";
           pst=conn.prepareStatement(sql);// 预编译sql语句
           pst.setString(1,name);
           res = pst.executeQuery();// 执行SQL语句
           if(res.next()){
               person = new ContactPerson();
               person.setName(res.getString("name"));
               person.setAdd(res.getString("address"));
               person.setNum(res.getString("num"));
               person.setPostal_code(res.getString("postal_code"));
               person.setE_mail(res.getString("e_mail"));
               person.setHome_phon(res.getString("home_phon"));
           }
       }catch (SQLException e){
           e.printStackTrace();
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       } catch (Throwable e) {
           e.printStackTrace();

       }finally {
            DBUtils.close(pst,conn,res);
            return person;
        }
    }

    @Override
    public ContactPerson selectNum(String num) {
        ContactPerson person = null;
        try{
            conn = (Connection) DBUtils.getConn();
            String sql = "select * from conperson where num = ?";
            pst=conn.prepareStatement(sql);// 预编译sql语句
            pst.setString(1,num);
            res = pst.executeQuery();// 执行SQL语句
            if(res.next()) {
                person = new ContactPerson();
                person.setName(res.getString("name"));
                person.setAdd(res.getString("address"));
                person.setNum(res.getString("num"));
                person.setPostal_code(res.getString("postal_code"));
                person.setE_mail(res.getString("e_mail"));
                person.setHome_phon(res.getString("home_phon"));
            }
        }catch (SQLException e){
        } catch (ClassNotFoundException e) {
        }finally {
            DBUtils.close(pst,conn,res);
            return person;
        }
    }
    @Override
    public void sort() {
        List<ContactPerson> list = new ArrayList<>();
        try {
            conn = (Connection) DBUtils.getConn();
            String sql = "SELECT * FROM conperson ORDER BY CONVERT(NAME USING GB2312)";
            pst = conn.prepareStatement(sql);
            res = pst.executeQuery();
            while(res.next()){
                ContactPerson person = new ContactPerson();
                person.setName(res.getString("name"));
                person.setAdd(res.getString("address"));
                person.setNum(res.getString("num"));
                person.setPostal_code(res.getString("postal_code"));
                person.setE_mail(res.getString("e_mail"));
                person.setHome_phon(res.getString("home_phon"));
                list.add(person);
            }
            for(ContactPerson c:list){
                System.out.println(c.toString());
            }
        } catch (SQLException e) {
        } catch (ClassNotFoundException e) {
        }finally {
            DBUtils.close(pst,conn,res);
        }
    }

    @Override
    public void show() {
        List<ContactPerson> list = new ArrayList<>();
        try {
            conn = (Connection) DBUtils.getConn();
            String sql = "select * from conperson";
            pst=conn.prepareStatement(sql);
            res = pst.executeQuery();
            while(res.next()){
                ContactPerson person = new ContactPerson();
                person.setName(res.getString("name"));
                person.setAdd(res.getString("address"));
                person.setNum(res.getString("num"));
                person.setPostal_code(res.getString("postal_code"));
                person.setE_mail(res.getString("e_mail"));
                person.setHome_phon(res.getString("home_phon"));
                list.add(person);
            }
            for(ContactPerson c:list){
                System.out.println(c.toString());
            }
        } catch (SQLException e) {
        } catch (ClassNotFoundException e) {
        }finally {
            DBUtils.close(pst,conn,res);
        }
    }

    @Override
    public void save(){
        try {
            conn = (Connection) DBUtils.getConn();
            List<ContactPerson> list = new ArrayList<>();
            String sql = "select * from conperson";
            pst=conn.prepareStatement(sql);
            res = pst.executeQuery();
            while(res.next()){
                ContactPerson person = new ContactPerson();
                person.setName(res.getString("name"));
                person.setAdd(res.getString("address"));
                person.setNum(res.getString("num"));
                person.setPostal_code(res.getString("postal_code"));
                person.setE_mail(res.getString("e_mail"));
                person.setHome_phon(res.getString("home_phon"));
                list.add(person);
            }
            File writename = new File("data\\test.text");
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));
            //BufferedWriter字符缓冲流
            //为了提高字符流读写的效率,引入了缓冲机制,进行字符批量的读写,提高了单个字符读写的效率
            for(ContactPerson c:list){
                out.write(c.toString());
                out.write("\r\n");  // \r\n即为换行
            }
            out.flush(); // 把缓存区内容压入文件
            out.close(); // 最后记得关闭文件
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(pst,conn,res);
        }
    }
}

2.2ContactTest

import function.ContactFunction;
import function.ContactPerson;

import java.sql.SQLException;
import java.util.Scanner;

public class ContactTest {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        ContactFunction address = new ContactFunction();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("***********[A]添加          [B]修改***********");
            System.out.println("***********[C]删除          [D]显示***********");
            System.out.println("***********[E]根据名字查询  [F]根据手机号查询*");
            System.out.println("***********[G]根据姓名排序  [H]保存通讯录*****");
            System.out.println("********************[I]退出*******************");
            System.out.println("**********请输入您的选择 不区分大小写*********");
                String strFountion = sc.nextLine();

                if ("a".equalsIgnoreCase(strFountion)) { // 添加
                    System.out.print("请输入您要添加的名字:");
                    String name = sc.nextLine();
                    System.out.print("请输入您要添加的住址:");
                    String adr = sc.nextLine();
                    System.out.print("请输入您要添加的电话号码:");
                    String num = sc.nextLine();
                    System.out.print("请输入您要添加的邮政编码:");
                    String post = sc.nextLine();
                    System.out.print("请输入您要添加的电子邮箱:");
                    String e = sc.nextLine();
                    System.out.print("请输入您要添加的家庭电话:");
                    String phone= sc.nextLine();
                    address.add(name,adr,num,post,e,phone);
                } else if ("b".equalsIgnoreCase(strFountion)) {// 修改
                    System.out.println("请输入您要修改的联系人的姓名是?");
                    String name = sc.nextLine();
                    if(address.selectName(name)==null){
                        System.out.println("修改的"+name+"不存在\r\n"+"修改失败!");
                    }else {
                        System.out.print("请您输入修改后的住址:");
                        String adr = sc.nextLine();
                        System.out.print("请您输入修改后的电话号码:");
                        String num = sc.nextLine();
                        System.out.print("请您输入修改后的邮政编码:");
                        String post = sc.nextLine();
                        System.out.print("请您输入修改后的电子邮箱:");
                        String e = sc.nextLine();
                        System.out.print("请您输入修改后的家庭电话:");
                        String phone = sc.nextLine();
                        address.update(name, adr, num, post, e, phone);
                    }
                } else if ("c".equalsIgnoreCase(strFountion)) {// 删除
                    System.out.println("请输入您要删除的联系人的姓名是?");
                    String name1 = sc.nextLine();
                    System.out.println("您确定要删除的联系人的姓名是" + name1 + "确定为Y,不确定为N");
                    String name2 = sc.nextLine();
                    if ("y".equalsIgnoreCase(name2)) {
                        if(address.selectName(name1) == null){
                            System.out.println("查无此人!!!删除失败!!");
                        }else{
                            address.remove(name1);
                        }
                    } else if ("n".equalsIgnoreCase(name2)) {
                        System.out.println("退出删除");
                        break;
                    } else {
                        System.out.println("您输入的不是Y和N,退出删除功能");
                        break;
                    }
                } else if ("d".equalsIgnoreCase(strFountion)) {// 显示
                    address.show();
                } else if ("e".equalsIgnoreCase(strFountion)) {// 查询
                    System.out.println("请输入您要查询的名字");
                    String name = sc.nextLine();
                    ContactPerson contactPerson = address.selectName(name);
                    if (contactPerson == null) {
                        System.out.println("查询无此人");
                    } else {
                        System.out.println("查询有此人");
                        System.out.println(contactPerson);
                    }
                } else if ("f".equalsIgnoreCase(strFountion)) {// 根据手机号查询
                    System.out.println("请输入您要查询的手机号");
                    String num = sc.nextLine();
                    ContactPerson contactPerson= address.selectNum(num);
                    if (contactPerson != null) {
                        System.out.println("查询有此人");
                        System.out.println(contactPerson);
                    } else {
                        System.out.println("查询无此人");
                    }
                } else if ("g".equalsIgnoreCase(strFountion)) {//排序
                    address.sort();
                } else if("h".equalsIgnoreCase(strFountion)){
                    address.save();
                System.out.println("保存成功");
            } else if ("i".equalsIgnoreCase(strFountion)){//退出
                System.out.println("退出成功");
                break;
            }else {// 删除
                System.out.println("您输入的选项有误,请重新输入");
            }
        }
        sc.close();
    }
}

你可能感兴趣的:(项目,JavaSE)