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();
    }
}

代码运行如下:

  • 20
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现了以下要求说明的功能: 1.要求使用GUI,采用 Swing 技术数据存储采用文件系统(可以是文本 文件、xml 文件、vCard 文件或其他自定义格式文件,但不能是 Excel 文件)。不 使用数据库。 2. 通讯录中每个项目包括: ( 1)基本信息:姓名、手机号码、电子邮箱、生日、所属组、备注、像片。 ( 2)工作单位:国家、省份、城市、地址、工作单位、所属部门、职位、 邮编、电话、传真 ( 3)网络信息:个人主页、即时通信工具及号码、备用邮箱 ( 4)家庭资料:国家、省份、城市、地址、邮编、电话 3 .通讯录数据的录入功能:输入并存储新的通讯录数据,输入时如果有同 名或同电话号码时,应该给出提示。 4 .通讯录数据的查询功能: ( 1)默认列出所有条目。 ( 2)模糊查询,可以按:姓名、电话号码、手机、姓名的汉语拼音的声母 或全拼查询。支持模糊查询即输入部分数据后,可以查询出符合条件的全部条目。 如:输入“张”,列出所有名字中有“张”的条目;输入“ 2645”列出电话中有 该 4个数字的所有条目;输入“xl ”,列出声母为该 2 个字母的所有条目 ( 3)指定分组后,列出某分组的所有条目 说明:查询结果如果有多个条目,应按照姓名排序,并使用姓或姓的第一 个汉语拼音的字母分类。 5 .删除和修改功能:查询到一个条目后,可以删除该条目或修改条目内容。 6 .通信录内容的导入和导出功能。要求能够以CSV 格式和 vCard 格式导入 和导出通讯录的全部内容。并能够与主流手机或其他通讯录软件交换数据。 7 .通讯录显示设置,可以设置在显示查询结果时,一个条目可以显示或不 显示哪些内容。 注意:本系统实现了读取和保存csv文件,同时csv文件中只保存了图片的路径。
目 录 一、开发背景……………………………………………………………………….1 二、可行分析……………………………………………………………………….1 三、设计内容……………………………………………………………………….2 四、设计要求……………………………………………………………………….2 五、具体设计……………………………………………………………………….2 六、具体分工………………………………………………………………….……4 七、程序设计……………………………………………………………………….4 1、主程序模块…………………………………………………….....…........14 1、添加模块…………………………………………………….....…........14 2、删除模块……………………………………………….........………....22 4、背景框格设计……………………………………………...………......32 八、实习心得……………………………………………………………………….35 一、开发背景 在信息化不断发展的今天,社会成员相互之间联系越来越紧密,联系方式也越来越 多。我们除了手机,移动电话等常规联系方式外,现在还有了电子邮箱、MSN、QQ等信息 化的联系方式,那么为了方便我们将每个联系人的多个联系方式保存在一起,方便由于 一种联系方式联系不到某人时,可查询其他联系方式,以前的"纸质"通讯录已经越来越 赶不上信息化的速度,势必被淘汰,所以我们用所学过的知识实现一个"非纸质"通讯录 。 二、可行分析 介绍了一个简单实用的个人通讯录管理系统java工程,详细描述了本人在编程时候的编 程思想和在MyEclipse 6.5中调试运行的步骤,简明扼要地介绍了整个工程的功能和实用方法。这个个人通讯录 管理系统含有添加、删除、查找等基本功能,同时也添加了一些有趣的小功能-- 支持背景图片的更换。在程序中完成了与数据库的通信,并同样可以对数据库进行添加 、查找、删除等功能。 三、设计内容 设计GUI界面的个人通讯录,用户可以添加,删除,修改,查询,打印通讯录中的记 录! 四,设计要求 用图形用户界面实现: 1、能实现增加记录,删除记录,显示所有记录,查询记录,文件备份。 2、通讯录的功能包括:姓名,电话,Email等。 3、使用数据库做后台连接! 五、具体设计 文件分别编译生成相应的字节码(class)文件。然后,用java解释器运 行主类: 主界面 六、具体分工 我参与设计添加页面,添加方法,删除页面,查询部分操作资以及源管理模块,背景 框格布局操作。 添加界面 删除界面 七、 程序设计 1. 主程序设计模块 package com.zzk.frame; import java.awt.BorderLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JToolBar; import javax.swing.SwingConstants; import javax.swing.WindowConstants; import com.swtdesigner.SwingResourceManager; import com.zzk.background.BackgroundPanel; import com.zzk.typestate.SaveTypeState; public class MainFrame extends JFrame { public MainFrame() { super(); setTitle("通讯录管理系统"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,60,800,600); URL url = LoginFrame.class.getResource("/image/main.jpg"); // 获得图片的URL Image image=new ImageIcon(url).getImage(); // 创建图像对
个人通讯录管理系统是一种用于管理个人联系人信息的软件系统。数据库连接是指系统通过特定的方式连接到数据库,以实现数据的存取和操作。 在Eclipse中实现个人通讯录管理系统数据库连接,可以通过以下步骤进行: 1. 确定数据库类型:首先需要确定所使用的数据库类型,常见的数据库类型有MySQL、Oracle、SQL Server等。 2. 导入数据库驱动:根据确定的数据库类型,从相应的官方网站或第三方厂商处下载对应的数据库驱动,并将其导入到Eclipse的工程中。 3. 创建数据库连接:在Eclipse中选择适当的视图,如Data Source Explorer,点击右键选择“New”->“Database Connection”。 4. 配置数据库连接参数:根据使用的数据库类型,在弹出的窗口中填写相应的数据库连接参数,如数据库地址、用户名、密码等信息。 5. 进行连接测试:填写完连接参数后,可以点击“Test Connection”按钮,测试连接是否成功。 6. 创建数据库操作对象:连接成功后,根据所使用的数据库类型,创建相应的数据库操作对象,如通过JDBC创建Connection、Statement或者Hibernate创建Session。 7. 执行数据库操作:通过数据库操作对象,可以执行一系列的数据库操作,包括插入、查询、更新、删除等操作。 8. 关闭数据库连接:在操作完成后,需要及时关闭数据库连接,以释放系统资源和确保数据的安全性。 通过以上步骤,可以在Eclipse中成功实现个人通讯录管理系统数据库连接,从而实现对联系人信息的存取和操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值