Java语言实现通讯录,联系人信息存在数据库里

通讯录管理
问题描述
编写一个简单的通讯录管理程序。通讯录中需要存储姓名,地址,电话号码,邮政编码四项。还可以存储Email,家庭电话等信息。
基本要求
程序应提供的基本管理功能有:
1)添加:即增加一个人的记录到通信录中。
2) 显示:即在屏幕上显示所有通信录中的人员信息。
3)存储:即将通讯录信息保存在数据库表中。
4)查询:可根据姓名查找某人的相关信息,若找到显示其姓名、地址、电话号码和邮政编码。
5)修改:可修改一个人的除姓名外其它信息。
6)排序:可以根据条目的某个项对所有条目进行排序,如姓名,或是邮政编码等。

联系人类

package contact;
 
public class Person { 
    private String namee; 
    private String phone;
    private String address;
    private String email;
    private int post;
    public Person(String namee,String phone,String address,String email,int post){
        this.namee=namee;
        this.phone=phone;
        this.address=address;
        this.email=email;
        this.post=post;
    }
    public Person(){

    }
    @Override
    public String toString(){
        System.out.println("namee"+'\t'+"phone"+'\t'+"address"+'\t'+"email"+'\t'+"post"+'\t');
        return namee+'\t'+phone+'\t'+address+'\t'+email+'\t'+post;
    }
    public String getName() { //get 和 set 方法实现封装
        return namee;
    }

    public void setName(String namee) {
        this.namee = namee;
    }
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    public int getPost() {
        return post;
    }

    public void setPost(int post) {
        this.post = post;
    }
}

导入jar包,JDBC连接数据库

package contact;

import com.mysql.jdbc.Driver;
import java.sql.*;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

public class MySQLUtils {
    private static String url = "jdbc:mysql://127.0.0.1:3306/address?useSSL=false";//useSSL=false 不进行SSL连接    数据库名:address
    private static String user = "root";
    private static String password = "";
    private static Connection con = null;

    //获取连接
    public static Connection getConn() {
        Connection connection=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/address?useSSL=false";
            String username = "root";
            String password = "";
            connection = DriverManager.getConnection(url, username, password);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }


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

    //关闭连接(无结果集)
    public static void closeConn(Connection conn, Statement stmt){
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }


    //测试连接数据库
    public static void main(String[] args) {
        System.out.print(getConn());
    }

}

Manage类和用户交互

package contact;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Manage {
    Scanner scanner = new Scanner(System.in);
    List<Person> list = new ArrayList<>();

    void menu() {
        System.out.println("||**************************************************||");
        System.out.println("||                  通讯录                          ||");
        System.out.println("||**************************************************||");
        System.out.println("||     1.查找联系人               5.按电话排序      ||");
        System.out.println("||**************************************************||");
        System.out.println("||     2.修改联系人               6.打印联系人      ||");
        System.out.println("||**************************************************||");
        System.out.println("||     3.删除联系人               7.清空联系人      ||");
        System.out.println("||**************************************************||");
        System.out.println("||      4.增加联系人               0.退出           ||");
        System.out.println("||**************************************************||");
    }

    public void manage(int choice,Person person, Dao dao)throws Exception {
        switch (choice) {

            case 1:
                search(person, dao);
                break;
            case 2:
                update(person, dao);
                break;
            case 3:
                delete(person, dao);
                break;
            case 4:
                add(person, dao);
                break;
            case 5:
                order(dao);
                break;
            case 6:
                show(person, dao);
                break;
            case 7:
                clear(dao);
                break;
            case 0:
                throw new Exception("再见");
            default:
                System.out.println("系统不能识别您的指令,请重新输入:");
                break;
        }
    }

    void search(Person person, Dao dao) throws Exception {//根据姓名查找联系人
        System.out.println("请输入联系人姓名:");
        person.setName(scanner.next());
        dao.judgeExist(person, person.getName());
    }

    void update(Person person, Dao dao) {  //修改联系人信息
        System.out.println("请输入要修改的联系人的姓名:");
        person.setName(scanner.next());
        System.out.println("请问要修改什么信息:请输入电话,地址,邮件,邮编");
        String m = scanner.next();
        if (m.equals("电话")) {
            System.out.println("请输入新的电话");
            person.setPhone(scanner.next());
            dao.update(person,person.getPhone(),m);

        }
        if (m.equals("地址")) {
            System.out.println("请输入新的地址");
            person.setAddress(scanner.next());
            dao.update(person, person.getAddress(),m);

        }
        if (m.equals("邮件")) {
            System.out.println("请输入新的邮件");
            person.setEmail(scanner.next());
            dao.update(person, person.getEmail(),m);

        }
        if (m.equals("邮编")) {
            System.out.println("请输入新的邮编");
            person.setPost(scanner.nextInt());
            dao.updatePost(person, person.getPost());
        }
    }

    void delete(Person person, Dao dao) throws Exception { //删除联系人
        System.out.println("请选择要删除的姓名:");
        person.setName(scanner.next());
        dao.deletePerson(person.getName());
    }

    void add(Person person, Dao dao) throws Exception { //添加联系人
        System.out.println("请输入:姓名,电话,地址,邮件,邮编");
        person.setName(scanner.next());
        person.setPhone(scanner.next());
        person.setAddress(scanner.next());
        person.setEmail(scanner.next());
        person.setPost(scanner.nextInt());
        dao.addPerson(person);

    }

    void order(Dao dao) throws SQLException {  //按电话排序
        dao.orderPerson();
    }

    void show(Person person, Dao dao) throws SQLException {  //打印联系人
        dao.showPerson(person);
    }

    void clear(Dao dao) throws SQLException { //清空联系人
        dao.clearPerson();
    }

    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        Manage manage = new Manage();
        Person person = new Person();
        Dao dao = new Dao();
        while (true) {
            manage.menu();  //展示操作界面
            System.out.println("请按数字选择您所需的服务");
            int select = scanner.nextInt();
            manage.manage(select, person, dao);
        }
    }
}

Dao类和数据库交互

package contact;

import java.sql.*;

public class Dao {
    public void judgeExist(Person person, String name0) {  //查找联系人
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Statement statement=null;
        try {
            connection = MySQLUtils.getConn();
            statement = connection.createStatement();
            System.out.println("1");
            person.setName(name0);
            String sql4 = "select * from renqian_address where namee='" + person.getName() + "';";
            preparedStatement = connection.prepareStatement(sql4);
            System.out.println("2");
            resultSet = preparedStatement.executeQuery(sql4);
            System.out.println("3");
                while (resultSet.next()) {
                    System.out.println("4");
                    person.setName(resultSet.getString("namee"));
                    person.setPhone(resultSet.getString("phone"));
                    person.setAddress(resultSet.getString("address"));
                    person.setEmail(resultSet.getString("email"));
                    person.setPost(resultSet.getInt("post"));
                    System.out.printf("姓名:%s   电话:%s   地址:%s   邮箱:%s   邮编:%d   ", person.getName(), person.getPhone(), person.getAddress(), person.getEmail(), person.getPost());
                    System.out.println();
                    System.out.println("请选择其他服务:");
                }
            } catch (SQLException e) {
            System.out.println("没有这个联系人");
        } finally {
            MySQLUtils.closeConn(connection, preparedStatement, resultSet);
        }
    }

    public void update(Person person,String x,String y) {
        Connection connection = null;
        Statement statement = null;
        String sql=null;
        try {
            connection = MySQLUtils.getConn();
            statement = connection.createStatement();
            if(y.equals("电话")) {
                sql = "update renqian_address set phone='" + x + "' where namee='" + person.getName() + "';";
            }else if(y.equals("地址")){
                sql = "update renqian_address set address='" + x + "' where namee='" + person.getName() + "';";
            }else if(y.equals("邮件")) {
                sql = "update renqian_address set email='" + x + "' where namee='" + person.getName() + "';";
            }
            statement.executeUpdate(sql);
                System.out.println("修改成功");
                System.out.println("请选择其他服务:");
                return;

            } catch (SQLException e) {
        System.out.println("修改失败");
        } finally {
            MySQLUtils.closeConn(connection, statement);
        }
    }

    public void updatePost(Person person, int post) {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = MySQLUtils.getConn();
            statement = connection.createStatement();
            String sql56 = "update renqian_address set address='" + post + "' where namee='" + person.getName() + "';";
            statement.executeUpdate(sql56);
                System.out.println("修改成功");
                System.out.println("请选择其他服务:");
                return;

            } catch (SQLException e) {
            System.out.println("修改失败");
        } finally {
            MySQLUtils.closeConn(connection, statement);
        }
    }

    public void deletePerson(String name2) {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = MySQLUtils.getConn();
            statement = connection.createStatement();
            String sql3 = "delete from renqian_address where namee='" + name2 + "';";
            statement.executeUpdate(sql3);
                System.out.println("删除成功!");
                System.out.println("请选择其他服务:");
            } catch (SQLException e) {
            System.out.println("删除失败");
        } finally {
            MySQLUtils.closeConn(connection, statement);
        }
    }

    public void addPerson(Person person) {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = MySQLUtils.getConn();
            statement = connection.createStatement(); //person.getName()     person.getPhone()   person.getAddress()   person.getEmail()   person.getPost()
            String sql2 = "INSERT INTO renqian_address(namee,phone,address,email,post) VALUES('" + person.getName() + "','" + person.getPhone() + "','" + person.getAddress() + "','" + person.getEmail() + "',"+person.getPost()+");";
            statement.executeUpdate(sql2);
                System.out.println("添加成功!");
                System.out.println("请选择其他服务:");
        } catch (SQLException e) {
            System.out.println("添加失败");
        } finally {
            MySQLUtils.closeConn(connection, statement);
        }
    }

    public void orderPerson() {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = MySQLUtils.getConn();
            statement = connection.createStatement();
            String sql7 = "select * from renqian_address order by phone";
           statement.executeQuery(sql7);
                System.out.println("排序成功");
                System.out.println("请选择其他服务:");
        } catch (SQLException e) {
            System.out.println("排序失败");
        } finally {
            MySQLUtils.closeConn(connection, statement);
        }
    }

    public void showPerson(Person person) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            connection = MySQLUtils.getConn();
            statement = connection.createStatement();
            String sql6 = "select * from renqian_address;";
            resultSet = statement.executeQuery(sql6);
            while (resultSet.next()) {
                person.setName(resultSet.getString("namee"));
                person.setPhone(resultSet.getString("phone"));
                person.setAddress(resultSet.getString("address"));
                person.setEmail(resultSet.getString("email"));
                person.setPost(resultSet.getInt("post"));
                System.out.println("姓名:" +person.getName()+ "       电话:" + person.getPhone() + "       地址:" + person.getAddress()+ "        邮箱:" + person.getEmail() + "        邮编:" + person.getPost());
            }
            System.out.println("请选择其他服务:");
        } catch (SQLException e) {
            System.out.println("打印失败");;
        } finally {
            MySQLUtils.closeConn(connection, statement);
        }
    }

    public void clearPerson() {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = MySQLUtils.getConn();
            statement=connection.createStatement();
            String sql = "delete from renqian_address";
            statement = connection.createStatement();
            statement.executeUpdate(sql);
            System.out.println("成功清空通讯录");
            System.out.println("请选择其他服务:");
        } catch (SQLException e) {
        System.out.println("清空失败");
        } finally {
            MySQLUtils.closeConn(connection, statement);
        }
    }
}
  • 2
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值