通讯录管理系统

目录

一、题目

二、创建通讯录所需的功能

1.查询联系人

(1)通过名称查询

(2)通过电话查询

2.插入联系人

3.删除联系人

4.修改联系人

(1)修改名称

(2)修改年龄

(3)修改性别

(4)修改手机号

5.联系人列表

三、代码实现

1.数据库连接类:JDBCUtils.java

2.测试类:tonxunlu.java

四、数据库所需要的表

五、 结语


一、题目

完成一个通讯录,需求:

1.添加联系人(联系人:编号,姓名,年龄,性别,手机号)

2.联系​​​​​​​人查询(输入姓名或电话查询)

3.显示联系人列表

4.根据编号删除指定编号的联系人

5.修改联系人信息

二、创建通讯录所需的功能

1.查询联系人

(1)通过名称查询

/**
	 * 通过名称查询
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void selectName(Statement xs) throws SQLException {
		System.out.println("请输入你需要查询的联系人名称:");
		String b = new Scanner(System.in).nextLine();
		String sql = "SELECT * FROM tonxunlu WHERE name like '" + b+"';";
		ResultSet gk = xs.executeQuery(sql);
		if (gk.next()) {
			int id = gk.getInt("id");
			String name = gk.getString("name");
			int age = gk.getInt("age");
			String sex = gk.getString("sex");
			String phone = gk.getString("phone");
			System.out.println("[编号:" + id + " ;姓名:" + name + " ;年龄:" + age + " ;性别:" + sex + " ;联系方式:" + phone + "]");
		} else {
			System.out.println("查询失败!");
		}
		gk.close();
	}

(2)通过电话查询

/**
	 * 通过电话查询
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void selectPhone(Statement xs) throws SQLException {
		System.out.println("请输入你需要查询的联系人电话:");
		String b = new Scanner(System.in).nextLine();
		String sql = "SELECT * FROM tonxunlu WHERE phone like '" + b+"';";
		ResultSet gk = xs.executeQuery(sql);
		if (gk.next()) {
			int id = gk.getInt("id");
			String name = gk.getString("name");
			int age = gk.getInt("age");
			String sex = gk.getString("sex");
			String phone = gk.getString("phone");
			System.out.println("[编号:" + id + " ;姓名:" + name + " ;年龄:" + age + " ;性别:" + sex + " ;联系方式:" + phone + "]");
		} else {
			System.out.println("查询失败!");
		}
		gk.close();
	}

2.插入联系人

/**
	 * 插入联系人
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void insert(Statement xs) throws SQLException {
		boolean isTrue=true;
		System.out.println("请输入需要插入的联系人编号:");
		int ca = new Scanner(System.in).nextInt();
		System.out.println("请输入需要插入的名字:");
		Scanner ok = new Scanner(System.in);
		String cm = ok.nextLine();
		System.out.println("请输入需要插入的年龄:");
		int cn = sc.nextInt();
		System.out.println("请输入需要插入的性别:");
		String cx = ok.nextLine();
		System.out.println("请输入需要插入的联系方式:");
		String cl = ok.nextLine();
		String sql1 = "INSERT INTO tonxunlu(id, name, age, sex, phone) VALUES(" + ca + ", '" + cm + "'," + cn + ",'" + cx
				+ "','" + cl + "')";
		String sql2="select id from tonxunlu;";
		ResultSet rs=xs.executeQuery(sql2);
		while(rs.next()) {
			if(ca==rs.getInt(1)) {
				isTrue=false;
				break;
			}
		}
		rs.close();
		if(isTrue) {
			if (xs.executeUpdate(sql1) >= 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		}else {
			System.out.println("编号已存在");
		}

	}

3.删除联系人

/**
	 * 删除联系人
	 * @param xs
	 * @throws SQLException
	 */
	public static void delete(Statement xs) throws SQLException {
		System.out.println("请输入你需要删除的联系人编号:");
		int sx = sc.nextInt();
		String sql = "DELETE FROM tonxunlu WHERE id = " + sx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("删除成功");
		} else {
			System.out.println("删除失败");
		}
	}

4.修改联系人

(1)修改名称

/**
	 * 修改名称
	 * @param xx
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void alterName(int xx, Statement xs) throws SQLException {
		System.out.println("请输入修改后的名称:");
		String name = new Scanner(System.in).nextLine();
		String sql = "UPDATE tonxunlu SET name = '" + name + " 'WHERE id = " + xx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}

(2)修改年龄

/**
	 * 修改年龄
	 * @param xx
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void alterAge(int xx, Statement xs) throws SQLException {
		System.out.println("请输入修改后的年龄:");
		int age = new Scanner(System.in).nextInt();
		String sql = "UPDATE tonxunlu SET age = " + age + " WHERE id = " + xx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}

(3)修改性别

/**
	 * 修改性别
	 * @param xx
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void alterSex(int xx, Statement xs) throws SQLException {
		System.out.println("请输入修改后的性别:");
		String sex = new Scanner(System.in).nextLine();
		String sql = "UPDATE tonxunlu SET sex = '" + sex + "' WHERE id = " + xx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}

(4)修改手机号

/**
	 * 修改手机号
	 * @param xx
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void alterTel_number(int xx, Statement xs) throws SQLException {
		System.out.println("请输入修改后的电话:");
		String phone = new Scanner(System.in).nextLine();
		String sql = "UPDATE tonxunlu SET phone = '" + phone + " 'WHERE id = " + xx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}

5.联系人列表

/**
	 * 查询所有人的信息
	 * @param xs
	 * @throws SQLException
	 */
	public static void selectAll(Statement xs) throws SQLException {
		String si = "SELECT * FROM tonxunlu";
		ResultSet gk = xs.executeQuery(si);
		while (gk.next()) {
			int id = gk.getInt("id");
			String name = gk.getString("name");
			int age = gk.getInt("age");
			String sex = gk.getString("sex");
			String phone = gk.getString("phone");
			System.out.println("[编号:" + id + " ;姓名:" + name + " ;年龄:" + age + " ;性别:" + sex + " ;联系方式:" + phone + "]");
		}
		gk.close();
	}

三、代码实现

1.数据库连接类:JDBCUtils.java

import java.sql.*;

/**
 * 连接JDBC类
 */
public class JDBCUtils {
    //加载驱动,并建立数据库连接

    /**
     * 加载驱动建立数据库链接
     *
     * @return 返回数据库Connection连接对象
     * @throws SQLException           抛出SQLException
     * @throws ClassNotFoundException 抛出ClassNotFoundException
     */
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        // 加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 连接数据库
        String url = "jdbc:mysql://localhost:3306/test01";
        // 数据库登录用户名
        String username = "root";
        // 数据库登录密码
        String password = "123456";
        // 获取数据库连接对象并返回Connection对象
        return DriverManager.getConnection(url, username, password);
    }

    /**
     * 关闭数据库连接,释放资源
     *
     * @param stmt Statement对象
     * @param conn Connection对象
     */
    public static void release(Statement stmt, Connection conn) {
        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;
        }
    }

    /**
     * 关闭数据库连接,释放资源
     *
     * @param rs   ResultSet对象
     * @param stmt Statement对象
     * @param conn Connection对象
     */
    public static void release(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        release(stmt, conn);
    }

}

2.测试类:tonxunlu.java

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class tonxunlu {
	static final Scanner sc = new Scanner(System.in);

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		Connection conn = null;
		Statement xs = null;
		try {
			conn = JDBCUtils.getConnection();
			System.out.println("连接成功!");
			System.out.println("欢迎进入通讯录管理系统");
		} catch (Exception e) {
			System.out.println("连接失败,请确定连接参数是否正确");
			e.printStackTrace();
		}
		boolean falg = true;
		while (falg) {
			System.out.println("------通讯录------");
			System.out.println("   1.查询联系人     ");
			System.out.println("   2.插入联系人     ");
			System.out.println("   3.修改联系人     ");
			System.out.println("   4.删除联系人     ");
			System.out.println("   5.联系人列表     ");
			System.out.println("   6.退出         ");
			System.out.println("  请输入选择<1-6>   ");
			System.out.println("-----------------");
			xs = conn.createStatement();
			int a = sc.nextInt();
			switch (a) {
			case 1:
				boolean falg2 = true;
				while (falg2) {
					System.out.println("------查询------");
					System.out.println("   1.名字查询     ");
					System.out.println("   2.电话查询     ");
					System.out.println("   3.退出         ");
					System.out.println("  请输入选择<1-5>   ");
					System.out.println("-----------------");
					int b1 = sc.nextInt();
					switch (b1) {
					case 1:
						selectName(xs);
						break;
					case 2:
						selectPhone(xs);
						break;
					case 3:
						falg2 = false;
						break;
					default:
						System.out.println("输入错误" + '\n' + "请输入正确数字");
						break;
					}
				}
				break;
			case 2:
				insert(xs);
				break;
			case 3:
				System.out.println("请输入你需要修改的联系人编号:");
				int xx = sc.nextInt();
				boolean falg3 = true;
				while (falg3) {
					System.out.println("------修改------");
					System.out.println("   1.名字     ");
					System.out.println("   2.年龄     ");
					System.out.println("   3.性别     ");
					System.out.println("   4.电话     ");
					System.out.println("   5.退出         ");
					System.out.println("  请输入选择<1-5>   ");
					System.out.println("-----------------");
					int b1 = sc.nextInt();
					switch (b1) {
					case 1:
						alterName(xx, xs);
						break;
					case 2:
						alterAge(xx, xs);
						break;
					case 3:
						alterSex(xx, xs);
						break;
					case 4:
						alterTel_number(xx, xs);
						break;
					case 5:
						falg3 = false;
						break;
					default:
						System.out.println("输入错误" + '\n' + "请输入正确数字");
						break;
					}
				}
				break;
			case 4:
				delete(xs);
				break;
			case 5:
				selectAll(xs);
				break;
			case 6:
				releaseConnnection(conn, xs);
				falg = false;
				break;
			default:
				System.out.println("输入错误" + '\n' + "请输入正确数字");
				break;
			}
		}
		sc.close();
	}
	/**
	 * 关闭连接
	 * @param conn
	 * @param xs
	 */
	public static void releaseConnnection(Connection conn, Statement xs) {
		JDBCUtils.release(xs, conn);
		System.out.println("关闭连接");
	}
	/**
	 * 插入联系人
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void insert(Statement xs) throws SQLException {
		boolean isTrue=true;
		System.out.println("请输入需要插入的联系人编号:");
		int ca = new Scanner(System.in).nextInt();
		System.out.println("请输入需要插入的名字:");
		Scanner ok = new Scanner(System.in);
		String cm = ok.nextLine();
		System.out.println("请输入需要插入的年龄:");
		int cn = sc.nextInt();
		System.out.println("请输入需要插入的性别:");
		String cx = ok.nextLine();
		System.out.println("请输入需要插入的联系方式:");
		String cl = ok.nextLine();
		String sql1 = "INSERT INTO tonxunlu(id, name, age, sex, phone) VALUES(" + ca + ", '" + cm + "'," + cn + ",'" + cx
				+ "','" + cl + "')";
		String sql2="select id from tonxunlu;";
		ResultSet rs=xs.executeQuery(sql2);
		while(rs.next()) {
			if(ca==rs.getInt(1)) {
				isTrue=false;
				break;
			}
		}
		rs.close();
		if(isTrue) {
			if (xs.executeUpdate(sql1) >= 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		}else {
			System.out.println("编号已存在");
		}

	}
	/**
	 * 修改名称
	 * @param xx
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void alterName(int xx, Statement xs) throws SQLException {
		System.out.println("请输入修改后的名称:");
		String name = new Scanner(System.in).nextLine();
		String sql = "UPDATE tonxunlu SET name = '" + name + " 'WHERE id = " + xx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}
	/**
	 * 修改性别
	 * @param xx
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void alterSex(int xx, Statement xs) throws SQLException {
		System.out.println("请输入修改后的性别:");
		String sex = new Scanner(System.in).nextLine();
		String sql = "UPDATE tonxunlu SET sex = '" + sex + "' WHERE id = " + xx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}
	/**
	 * 修改年龄
	 * @param xx
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void alterAge(int xx, Statement xs) throws SQLException {
		System.out.println("请输入修改后的年龄:");
		int age = new Scanner(System.in).nextInt();
		String sql = "UPDATE tonxunlu SET age = " + age + " WHERE id = " + xx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}
	/**
	 * 修改手机号
	 * @param xx
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void alterTel_number(int xx, Statement xs) throws SQLException {
		System.out.println("请输入修改后的电话:");
		String phone = new Scanner(System.in).nextLine();
		String sql = "UPDATE tonxunlu SET phone = '" + phone + " 'WHERE id = " + xx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}
	/**
	 * 通过名称查询
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void selectName(Statement xs) throws SQLException {
		System.out.println("请输入你需要查询的联系人名称:");
		String b = new Scanner(System.in).nextLine();
		String sql = "SELECT * FROM tonxunlu WHERE name like '" + b+"';";
		ResultSet gk = xs.executeQuery(sql);
		if (gk.next()) {
			int id = gk.getInt("id");
			String name = gk.getString("name");
			int age = gk.getInt("age");
			String sex = gk.getString("sex");
			String phone = gk.getString("phone");
			System.out.println("[编号:" + id + " ;姓名:" + name + " ;年龄:" + age + " ;性别:" + sex + " ;联系方式:" + phone + "]");
		} else {
			System.out.println("查询失败!");
		}
		gk.close();
	}

	/**
	 * 通过电话查询
	 * @param xs
	 * @throws SQLException
	 */
	@SuppressWarnings("resource")
	public static void selectPhone(Statement xs) throws SQLException {
		System.out.println("请输入你需要查询的联系人电话:");
		String b = new Scanner(System.in).nextLine();
		String sql = "SELECT * FROM tonxunlu WHERE phone like '" + b+"';";
		ResultSet gk = xs.executeQuery(sql);
		if (gk.next()) {
			int id = gk.getInt("id");
			String name = gk.getString("name");
			int age = gk.getInt("age");
			String sex = gk.getString("sex");
			String phone = gk.getString("phone");
			System.out.println("[编号:" + id + " ;姓名:" + name + " ;年龄:" + age + " ;性别:" + sex + " ;联系方式:" + phone + "]");
		} else {
			System.out.println("查询失败!");
		}
		gk.close();
	}
	/**
	 * 查询所有人的信息
	 * @param xs
	 * @throws SQLException
	 */
	public static void selectAll(Statement xs) throws SQLException {
		String si = "SELECT * FROM tonxunlu";
		ResultSet gk = xs.executeQuery(si);
		while (gk.next()) {
			int id = gk.getInt("id");
			String name = gk.getString("name");
			int age = gk.getInt("age");
			String sex = gk.getString("sex");
			String phone = gk.getString("phone");
			System.out.println("[编号:" + id + " ;姓名:" + name + " ;年龄:" + age + " ;性别:" + sex + " ;联系方式:" + phone + "]");
		}
		gk.close();
	}
	/**
	 * 删除联系人
	 * @param xs
	 * @throws SQLException
	 */
	public static void delete(Statement xs) throws SQLException {
		System.out.println("请输入你需要删除的联系人编号:");
		int sx = sc.nextInt();
		String sql = "DELETE FROM tonxunlu WHERE id = " + sx;
		if (xs.executeUpdate(sql) >= 1) {
			System.out.println("删除成功");
		} else {
			System.out.println("删除失败");
		}
	}
}

四、数据库所需要的表

创建一个test01的数据库,在里面创建一个表tonxunlu

 sql文件链接:https://download.csdn.net/download/qq_62631097/87370635

五、 结语

自己第一次写,还存在许多问题,希望大家多多指教!

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值