java入门编程之个人通讯录管理系统-数据库

s

一、实验目的:

熟悉数据库开发的基本操作,实现数据库的连接,动态增、删、改、查功能

项目性质:设计型

二、实验内容:

1、实现一个小型通讯录

1)能够实现个人通讯录列表的显示,可通过表格显示

2)能够实现通讯录信息的动态增加、修改和删除

三、类的设计

在项目中创建User类,属性包括name,phone,address,email

在数据库连接中实现八个方法:CommunicationApp(刷新连接)、findUserByName(查询1)、findUserByPhone(查询2)、deleteUser(基础删除)、init(从数据库加载数据)、addUserToDB(向数据库中添加user)、updateUserInDB(更新数据库中user)、deleteUserFromDB(删除数据库中user)

在页面文件中设计七个类:一个通讯类(tongxun)、一个主方法(Test)、一个查询类(Select)、一个新增类(Insert)、一个展示类(Display)、一个修改类(dfds)、一个删除类(Delete)

四、项目创建

在项目中创建maven项目,在pom.xml文件中添加以下内容

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.33</version>
    </dependency>
public class User {
    String name;
    String phone;
    String address;
    String email;

    public User(String name, String phone, String address, String email) {
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    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;
    }
}
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommunicationApp {
    private static final String URL = "jdbc:mysql://localhost:3306/database-cclg";
    private static final String USER = "project_java";
    private static final String PASSWORD = "87654321";

    public List<User> users = new ArrayList<>();

    public CommunicationApp() {
        init();
    }

    public User findUserByName(String name) {
        for (User user : users) {
            if (user.name.equals(name)){
                return user;
            }
        }
        return null;
    }

    public User findUserByPhone(String phone) {
        for (User user : users) {
            if (user.phone.equals(phone)){
                return user;
            }
        }
        return null;
    }

    public void deleteUser(String name) throws SQLException {
        User user = findUserByName(name);
        if (user != null) {
            users.remove(user);
            deleteUserFromDB(user);
        }
    }

    public void init() {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM User");
            while (rs.next()) {
                for (User user1 : users){
                    if (user1.getName().equals(rs.getString("name"))) rs.close();
                }
                String name = rs.getString("name");
                String phone = rs.getString("phone");
                String address = rs.getString("address");
                String email = rs.getString("email");
                User user = new User(name, phone, address, email);
                users.add(user);
            }
            rs.close();
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addUserToDB(User user) throws SQLException {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO User (name, phone, address, email) VALUES (?, ?, ?, ?)");
            pstmt.setString(1, user.name);
            pstmt.setString(2, user.phone);
            pstmt.setString(3, user.address);
            pstmt.setString(4, user.email);
            pstmt.executeUpdate();
        }
    }

    public void updateUserInDB(User user,String name) throws SQLException {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            PreparedStatement pstmt = conn.prepareStatement("UPDATE User SET name = ? , phone = ? , address = ? , email = ? WHERE name = ?");
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getPhone());
            pstmt.setString(3, user.getAddress());
            pstmt.setString(4, user.getEmail());
            pstmt.setString(5, name);
            pstmt.executeUpdate();
        }
    }

    private void deleteUserFromDB(User user) throws SQLException {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM User WHERE name = ?");
            pstmt.setString(1, user.name);
            pstmt.executeUpdate();
        }
    }
}
package org.example;
import org.example.实验八.CommunicationApp;
import org.example.实验八.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

class BookView extends JFrame implements ActionListener {
    public BookView() {

        JFrame mainFrame = new JFrame("通讯录");
        mainFrame.setLocation(800, 600);
        mainFrame.setSize(250, 220);
        mainFrame.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Button bt1 = new Button("新增联系人");
        mainFrame.add(bt1);
        bt1.setLocation(10, 30);
        bt1.setSize(80, 25);
        bt1.addActionListener(this);

        Button bt2 = new Button("删除联系人");
        mainFrame.add(bt2);
        bt2.setLocation(120, 30);
        bt2.setSize(80, 25);
        bt2.addActionListener(this);

        Button bt3 = new Button("显示所有记录");
        mainFrame.add(bt3);
        bt3.setLocation(120, 65);
        bt3.setSize(80, 25);
        bt3.addActionListener(this);

        Button bt4 = new Button("查询个人信息");
        mainFrame.add(bt4);
        bt4.setLocation(10, 65);
        bt4.setSize(80, 25);
        bt4.addActionListener(this);

        Button bt6 = new Button("退出");
        mainFrame.add(bt6);
        bt6.setLocation(65, 135);
        bt6.setSize(80, 25);
        bt6.addActionListener(this);

        Button bt13 = new Button("修改联系人");
        mainFrame.add(bt13);
        bt13.setLocation(10, 100);
        bt13.setSize(80, 25);
        bt13.addActionListener(this);
        mainFrame.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        String bt = e.getActionCommand();
        if (bt.equals("新增联系人")) {
            Insert m = new Insert();
        }
        if (bt.equals("删除联系人")) {
            Delete n = new Delete();
        }
        if (bt.equals("查询个人信息")) {
            Select a = new Select();
        }
        if (bt.equals("显示所有记录")) {
            Display b = new Display();
        }
        if(bt.equals("修改联系人")){
            dfds c  = new dfds();
        }
        if (bt.equals("退出")) {
            System.exit(0);
        }
    }
}
class Select extends JFrame implements ActionListener{
    public TextField text_1;
    public TextField text_2;
    public TextField text_3;
    public TextField text_4;
    public TextField text_5;
    Select() {
        setTitle("查询个人信息");
        setSize(400,300);
        setLocation(600, 400);
        setLayout(new GridLayout(6, 2));

        text_1 = new TextField();
        text_2 = new TextField();
        text_3 = new TextField();
        text_4 = new TextField();
        text_5 = new TextField();

        Label lab_1 = new Label("请输入要查找人的姓名:");
        Label lab_2 = new Label("该联系人手机号码是:");
        Label lab_3 = new Label("该联系人地址是:");
        Label lab_4 = new Label("该联系人邮箱是:");
        Label lab_5 = new Label("备注:");

        Button bt11 = new Button("确定");
        Button bt12 = new Button("清空");

        bt11.addActionListener(this);
        bt12.addActionListener(this);

        add(lab_1);
        add(text_1);
        add(lab_2);
        add(text_2);
        add(lab_3);
        add(text_3);
        add(lab_4);
        add(text_4);
        add(lab_5);
        add(text_5);
        add(bt11);
        add(bt12);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String bt = e.getActionCommand();
        if(bt.equals("确定")){
            CommunicationApp controller = new CommunicationApp();
            controller.init();
            User user = controller.findUserByName(text_1.getText());
            if(user != null){
                text_2.setText(user.getPhone());
                text_3.setText(user.getAddress());
                text_4.setText(user.getEmail());
                text_5.setText("无备注");
            }
            if(user == null){
                text_5.setText("" + "查无此人");
            }
        }
        if (bt.equals("清空")){
            text_1.setText("");
            text_2.setText("");
            text_3.setText("");
            text_4.setText("");
            text_5.setText("");
        }
    }
}

class Insert extends JFrame implements ActionListener{
    public TextField text_1;
    public TextField text_2;
    public TextField text_3;
    public TextField text_4;
    Insert(){
        setTitle("新增联系人");
        setSize(300,300);
        setLocation(600,400);
        setLayout(new GridLayout(6,2));

        text_1=new TextField();
        text_2=new TextField();
        text_3=new TextField();
        text_4=new TextField();

        Label lab_1=new Label("请输入联系人姓名:");
        Label lab_2=new Label("请输入联系人手机号码  :");
        Label lab_3=new Label("请输入联系人地址:");
        Label lab_4=new Label("请输入联系人邮箱:");
        Button bt7=new Button("确定");
        Button bt8=new Button("清空");

        bt7.addActionListener(this);
        bt8.addActionListener(this);

        add(lab_1);add(text_1);
        add(lab_2);add(text_2);
        add(lab_3);add(text_3);
        add(lab_4);add(text_4);
        add(bt7);  add(bt8);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        CommunicationApp communicationApp = new CommunicationApp();
        String bt = e.getActionCommand();
        if (bt.equals("确定")) {
            User user = communicationApp.findUserByName(text_1.getText());
            if (user == null) {
                user = new User(text_1.getText(), text_2.getText(), text_3.getText(), text_4.getText());
                try {
                    communicationApp.addUserToDB(user);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                text_2.setText("已添加");
                text_3.setText(" ");
                text_4.setText(" ");
            }else {
                text_2.setText("此用户已添加");
                text_3.setText(" ");
                text_4.setText(" ");
            }
        }
    }
}

class Display extends JFrame{
    Display() {
        setTitle("显示所有记录");
        setSize(800, 300);
        setLocation(600, 400);
        setVisible(true);
    }
    public void paint(Graphics g) {
        CommunicationApp controller = new CommunicationApp();
        controller.init();
        super.paint(g);
        int i = 1;
        for (User user : controller.users){
            g.drawString("姓名:" + user.getName(), 90, 40 * i);
            g.drawString("电话:" + user.getPhone(), 185, 40 * i);
            g.drawString("地址:" + user.getAddress(), 315, 40 * i);
            g.drawString("邮箱:" + user.getEmail(), 415, 40 * i);
            i++;
        }
    }
}

class Delete extends JFrame implements ActionListener{
    public TextField text_1;
    public TextField text_2;
    Delete() {
        setTitle("删除联系人");
        setSize(350, 150);
        setLocation(600, 400);
        setLayout(new GridLayout(3, 2));

        text_1 = new TextField();
        text_2 = new TextField();

        Label lab_3 = new Label("请输入要删除联系人的电话号码是:");
        Label lab_4 = new Label("备注:");

        Button bt9 = new Button("确定");
        Button bt10 = new Button("清空");

        bt9.addActionListener(this);
        bt10.addActionListener(this);

        add(lab_3);
        add(text_1);
        add(lab_4);
        add(text_2);
        add(bt9);
        add(bt10);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        String bt = e.getActionCommand();
        if (bt.equals("确定")) {
            CommunicationApp controller = new CommunicationApp();
            controller.init();
            User user = controller.findUserByPhone(text_1.getText());
            if (user != null) {
                try {
                    controller.deleteUser(user.getName());
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                text_2.setText("" + "删除成功");
            }else text_2.setText("" + "没有此用户,请重新输入");
        }
        if (bt.equals("清空")) {
            text_1.setText("");
            text_2.setText("");
        }
    }
}

class dfds extends JFrame implements ActionListener {
    public TextField text_1;
    public TextField text_2;
    public TextField text_3;
    public TextField text_4;
    public TextField text_5;
    public TextField text_6;
    public TextField text_7;
    public TextField text_8;
    public Button button1;
    public Button button2;
    public Button button3;
    dfds(){
        setTitle("修改联系人");
        setSize(600, 300);
        setLocation(600, 400);
        setLayout(new GridLayout(6, 3));

        text_1 = new TextField();
        text_2 = new TextField();
        text_3 = new TextField();
        text_4 = new TextField();
        text_5 = new TextField();
        text_6 = new TextField();
        text_7 = new TextField();
        text_8 = new TextField();

        Label lab_1 = new Label("请输入要查找人的姓名:");
        Label lab_2 = new Label("该联系人手机号码是:");
        Label lab_3 = new Label("该联系人地址是:");
        Label lab_4 = new Label("该联系人邮箱是:");
        Label lab_5 = new Label("注释");
        Label lab_6 = new Label("原数据");
        Label lab_7 = new Label("修改后数据");

        button1 = new Button("显示该名字联系人信息");
        button1.addActionListener(this);

        button2 = new Button("清空全部信息");
        button2.addActionListener(this);

        button3 = new Button("确定修改该数据");
        button3.addActionListener(this);

        add(lab_5);add(lab_6);add(lab_7);
        add(lab_1);add(text_1);add(text_5);
        add(lab_2);add(text_2);add(text_6);
        add(lab_3);add(text_3);add(text_7);
        add(lab_4);add(text_4);add(text_8);
        add(button1);add(button2);add(button3);
        setVisible(true);
    }
    public void actionPerformed (ActionEvent e){
        String bt = e.getActionCommand();
        if (bt.equals("显示该序号联系人信息")) {
            CommunicationApp communication = new CommunicationApp();
            communication.init();
            if (text_1.getText() != null){
                User user = communication.findUserByName(text_1.getText());
                if (user != null){
                    text_2.setText(user.getPhone());
                    text_3.setText(user.getAddress());
                    text_4.setText(user.getEmail());
                }else{
                    text_2.setText("查无此人");
                }
            }else {
                text_1.setText("请输入:");
            }
        }
        if (bt.equals("清空全部信息")){
            text_1.setText("");
            text_2.setText("");
            text_3.setText("");
            text_4.setText("");
            text_5.setText("");
            text_6.setText("");
            text_7.setText("");
            text_8.setText("");
        }
        if (bt.equals("确定修改该数据")) {
            CommunicationApp communication = new CommunicationApp();
            User user;
            User user2;
            communication.init();
            if (text_1.getText() != null){
                user = communication.findUserByName(text_1.getText());
                if (user != null){
                    text_2.setText(user.getPhone());
                    text_3.setText(user.getAddress());
                    text_4.setText(user.getEmail());

                    user2 = new User(text_5.getText(), text_6.getText(), text_7.getText(), text_8.getText());
                    try {
                        communication.updateUserInDB(user2,user.getName());
                        text_1.setText("修改完成");
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                }else{
                    text_2.setText("查无此人");
                }
            }else {
                text_1.setText("请输入:");
            }
        }
    }
}
public class AddressBook {
    public static void main(String[] args) {
        new BookView();
    }
}

代码运行如下:

猜你喜欢

转载自blog.csdn.net/lazy_88/article/details/135016049