电话薄程序
Posted 张好好
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电话薄程序相关的知识,希望对你有一定的参考价值。
//该程序需要连接数据库。根据word文档要求所有功能均已实现。
//大部分方法基本差不多,
//在查询修改的时候能输出 最大ID号 和最小ID号,并且可以对输入的ID号进行判断是否存在(具体方法请查看 修改电话号码, 其他修改方法暂时没有相对完善,
只针对修改电话号码的方法进行了修改,方便后期对编写程序思路的查看)。
package 电话薄1; public class Menu { //主菜单 public void mainMenu() { for(int i = 0 ; i >= 0 ; i ++ ) { System.out.println("***********************\\n** 1.添加记录 **\\n" + "** 2.查找记录 **\\n" + "** 3.修改记录 **\\n" + "** 4.删除记录 **\\n" + "** 5.排序记录 **\\n" + "** 6.退出系统 ** \\n" + "***********************"); System.out.println("请输入正确的数字,最小是:1 最大是:6"); TelNoteRegex rtn = new TelNoteRegex(); Operate a = new Operate(); switch(rtn.menuRegex(1, 6)) { case 1: a.addLogic(); break; case 2: a.searchLogic(); break; case 3: a.modifyLogicLogic(); break; case 4: a.deleteLogic(); break; case 5: a.orderLogic(); break; case 6: System.out.println("程序已关闭,欢迎下次使用"); i=-2; break; } } } //添加用户菜单 public void addMenu() { System.out.println("***********************\\n" + "** 1.添加新记录 **\\n" + "** 2.查看全记录 **\\n" + "** 3.返回上一级 **\\n" + "***********************"); System.out.println("请输入正确的数字,最小是:1 最大是:3"); } //查找用户菜单 void searchMenu() { System.out.println("***********************\\n" + "** 1.按姓名查找 **\\n" + "** 2.按年龄查找 **\\n" + "** 3.按性别查找 **\\n" + "** 4.按号码查找 **\\n" + "** 5.按住址查找 **\\n" + "** 6.查看全记录 **\\n" + "** 7.返回上一级 **\\n" + "***********************"); System.out.println("请输入正确的数字,最小是:1 最大是:7"); } //修改用户信息菜单 void modifyMenu() { System.out.println("***********************\\n" + "** 1.查看全记录 **\\n" + "** 2.修改指定记录 **\\n" + "** 3.返回上一级 **\\n" + "***********************"); System.out.println("请输入正确的数字,最小是:1 最大是:3"); } //修改用户信息子菜单 void subModifyMenu () { System.out.println("***********************\\n" + "** 1.修改姓名 **\\n" + "** 2.修改年龄 **\\n" + "** 3.修改性别 **\\n" + "** 4.修改号码 **\\n" + "** 5.修改住址 **\\n" + "** 6.返回上一级 **\\n" + "***********************"); System.out.println("请输入正确的数字,最小是:1 最大是:6"); } //删除用户信息菜单 void deleteMenu () { System.out.println("***********************\\n" + "** 1.查看全记录 **\\n" + "** 2.删除指定记录 **\\n" + "** 3.删除全部记录 **\\n" + "** 4.返回上级 **\\n" + "************************"); System.out.println("请输入正确的数字,最小是:1 最大是:6"); } //排序用户信息菜单 void orderMenu () { System.out.println("***********************\\n" + "** 1.按姓名排序 **\\n" + "** 2.按年龄排序 **\\n" + "** 3.按性别排序 **\\n" + "** 4.查看全部记录 **\\n" + "** 5.返回上一级 **\\n" + "***********************"); System.out.println("请输入正确的数字,最小是:1 最大是:6"); } }
package 电话薄1; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import com.mchange.v2.c3p0.ComboPooledDataSource; public class Operate { // 连接池 ComboPooledDataSource cp = new ComboPooledDataSource("helloc3p0"); // 用户添加信息业务逻辑控制 public void addLogic() { TelNoteRegex rtn = new TelNoteRegex(); Operate i = new Operate(); Menu m = new Menu(); m.addMenu(); switch (rtn.menuRegex(1, 3)) { case 1: addOperation(); // break; case 2: List<Person> lb = i.showAll(); // break; } } // 用户查询信息业务逻辑控制 public void searchLogic() { TelNoteRegex rtn = new TelNoteRegex(); Menu m = new Menu(); m.searchMenu(); switch (rtn.menuRegex(1, 7)) { case 1: searchByName(); break; case 2: searchByAge(); break; case 3: searchBySex(); break; case 4: searchByTelNum(); break; case 5: searchByAdd(); break; case 6: showAll(); break; } } // 修改信息业务逻辑控制 public void modifyLogicLogic() { TelNoteRegex rtn = new TelNoteRegex(); Menu m = new Menu(); m.modifyMenu(); switch (rtn.menuRegex(1, 3)) { case 1: showAll(); case 2: modify(); break; } } // 修改指定记录信息 public void modify() { TelNoteRegex rtn = new TelNoteRegex(); Menu m = new Menu(); m.subModifyMenu(); switch (rtn.menuRegex(1, 6)) { case 1: modName(); break; case 2: modAge(); break; case 3: modSex(); break; case 4: modTelNum(); break; case 5: modAdd(); break; case 6: modifyLogicLogic(); break; } } // 删除信息业务逻辑控制 public void deleteLogic() { TelNoteRegex rtn = new TelNoteRegex(); Menu m = new Menu(); m.deleteMenu(); switch (rtn.menuRegex(1, 4)) { case 1: showAll(); break; case 2: delete(); break; case 3: deleteAll(); break; } } // 排序信息业务逻辑控制 public void orderLogic() { TelNoteRegex rtn = new TelNoteRegex(); Menu m = new Menu(); m.orderMenu(); switch (rtn.menuRegex(1, 5)) { case 1: orderName(); break; case 2: orderAge(); break; case 3: orderSex(); break; case 4: showAll(); break; } } // 添加新用户信息 public List<Person> addOperation() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 输入姓名并判断 TelNoteRegex name1 = new TelNoteRegex(); String name = name1.nameRegex(); // 输入年龄并判断 TelNoteRegex age1 = new TelNoteRegex(); String age = age1.ageRegex(); // 输入性别并判断 TelNoteRegex sex1 = new TelNoteRegex(); String sex = sex1.sexRegex(); // 输入电话号码并判断 TelNoteRegex telnum1 = new TelNoteRegex(); String telnum = telnum1.telnumRegex(); // 输入地址 TelNoteRegex address1 = new TelNoteRegex(); String address = address1.addressRegex(); String sql = "insert into person values(sq_person.nextval,?,?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, age); ps.setString(3, sex); ps.setString(4, telnum); ps.setString(5, address); ps.executeUpdate(); System.out.println("添加成功"); ps.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return rtn; } // 查询全部用户信息 public List<Person> showAll() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select * from person"); // 遍历结果集 while (rs.next()) { Person pe = new Person(); pe.setId(rs.getInt("id")); pe.setName(rs.getString("name")); pe.setAge(rs.getString("age")); pe.setSex(rs.getString("sex")); pe.setTelNum(rs.getString("telnum")); pe.setAddress(rs.getString("address")); rtn.add(pe); System.out.println(pe); } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return rtn; } // 按姓名查找 public List<Person> searchByName() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 输入姓名并判断 TelNoteRegex name1 = new TelNoteRegex(); String name = name1.nameRegex(); String sql = "select * from person where name = \'" + name + "\'"; ResultSet rs = st.executeQuery(sql); // 遍历结果集 if (!rs.next()) { System.out.println("没有相关数据"); } else { rs = st.executeQuery(sql); while (rs.next()) { Person pe = new Person(); pe.setId(rs.getInt("id")); pe.setName(rs.getString("name")); pe.setAge(rs.getString("age")); pe.setSex(rs.getString("sex")); pe.setTelNum(rs.getString("telnum")); pe.setAddress(rs.getString("address")); rtn.add(pe); System.out.println(pe); } } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return rtn; } // 按年龄查找 public List<Person> searchByAge() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 输入年龄并判断 TelNoteRegex age1 = new TelNoteRegex(); String age = age1.ageRegex(); String sql = "select * from person where age = \'" + age + "\'"; ResultSet rs = st.executeQuery(sql); // 遍历结果集 if (!rs.next()) { System.out.println("没有相关数据"); } else { rs = st.executeQuery(sql); while (rs.next()) { Person pe = new Person(); pe.setId(rs.getInt("id")); pe.setName(rs.getString("name")); pe.setAge(rs.getString("age")); pe.setSex(rs.getString("sex")); pe.setTelNum(rs.getString("telnum")); pe.setAddress(rs.getString("address")); rtn.add(pe); System.out.println(pe); } } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return rtn; } // 按性别查询 public List<Person> searchBySex() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 输入性别并判断 TelNoteRegex sex1 = new TelNoteRegex(); String sex = sex1.sexRegex(); String sql = "select * from person where sex = \'" + sex + "\'"; ResultSet rs = st.executeQuery(sql); // 遍历结果集 if (!rs.next()) { System.out.println("没有相关数据"); } else { rs = st.executeQuery(sql); while (rs.next()) { Person pe = new Person(); pe.setId(rs.getInt("id")); pe.setName(rs.getString("name")); pe.setAge(rs.getString("age")); pe.setSex(rs.getString("sex")); pe.setTelNum(rs.getString("telnum")); pe.setAddress(rs.getString("address")); rtn.add(pe); System.out.println(pe); } } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return rtn; } // 按号码查找 public List<Person> searchByTelNum() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 输入号码并判断 TelNoteRegex telnum1 = new TelNoteRegex(); String telnum = telnum1.telnumRegex(); String sql = "select * from person where telnum = \'" + telnum + "\'"; ResultSet rs = st.executeQuery(sql); // 遍历结果集 if (!rs.next()) { System.out.println("没有相关数据"); } else { rs = st.executeQuery(sql); while (rs.next()) { Person pe = new Person(); pe.setId(rs.getInt("id")); pe.setName(rs.getString("name")); pe.setAge(rs.getString("age")); pe.setSex(rs.getString("sex")); pe.setTelNum(rs.getString("telnum")); pe.setAddress(rs.getString("address")); rtn.add(pe); System.out.println(pe); } } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return rtn; } // 按号码查找 public List<Person> searchByAdd() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 输入地址并判断 TelNoteRegex address1 = new TelNoteRegex(); String address = address1.addressRegex(); String sql = "select * from person where address = \'" + address + "\'"; ResultSet rs = st.executeQuery(sql); // 遍历结果集 if (!rs.next()) { System.out.println("没有相关数据"); } else { rs = st.executeQuery(sql); while (rs.next()) { Person pe = new Person(); pe.setId(rs.getInt("id")); pe.setName(rs.getString("name")); pe.setAge(rs.getString("age")); pe.setSex(rs.getString("sex")); pe.setTelNum(rs.getString("telnum")); pe.setAddress(rs.getString("address")); rtn.add(pe); System.out.println(pe); } } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return rtn; } // 修改姓名 public void modName() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 获取ID的最小值 ResultSet rs1 = st.executeQuery("select min(id) from person"); int min = 0; rs1.next(); min = rs1.getInt(1); // 获取ID的最大值 ResultSet rs2 = st.executeQuery("select max(id) from person"); int max = 0; rs2.next(); max = rs2.getInt(1); System.out.println("请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max); Scanner scan = new Scanner(System.in); int id = scan.nextInt(); // 输入姓名并判断 TelNoteRegex name1 = new TelNoteRegex(); String name = name1.nameRegex(); String sql = "update person set name = \'" + name + "\' where id = \'" + id + "\'"; ResultSet rs = st.executeQuery(sql); ResultSet rs5 = st.executeQuery("select * from person where id = \'" + id + "\'"); // 遍历结果集 if (!rs5.next()) { System.out.println("没有相关数据,系统退回到主菜单"); } else { rs = st.executeQuery(sql); System.out.println("修改成功"); } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return; } // 修改年龄 public void modAge() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 获取ID的最小值 ResultSet rs1 = st.executeQuery("select min(id) from person"); int min = 0; rs1.next(); min = rs1.getInt(1); // 获取ID的最大值 ResultSet rs2 = st.executeQuery("select max(id) from person"); int max = 0; rs2.next(); max = rs2.getInt(1); System.out.println("请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max); Scanner scan = new Scanner(System.in); int id = scan.nextInt(); // 输入姓名并判断 TelNoteRegex age1 = new TelNoteRegex(); String age = age1.ageRegex(); String sql = "update person set age = \'" + age + "\' where id = \'" + id + "\'"; ResultSet rs = st.executeQuery(sql); ResultSet rs5 = st.executeQuery("select * from person where id = \'" + id + "\'"); // 遍历结果集 if (!rs5.next()) { System.out.println("没有相关数据,系统退回到主菜单"); } else { rs = st.executeQuery(sql); System.out.println("修改成功"); } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return; } // 修改性别 public void modSex() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 获取ID的最小值 ResultSet rs1 = st.executeQuery("select min(id) from person"); int min = 0; rs1.next(); min = rs1.getInt(1); // 获取ID的最大值 ResultSet rs2 = st.executeQuery("select max(id) from person"); int max = 0; rs2.next(); max = rs2.getInt(1); System.out.println("请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max); Scanner scan = new Scanner(System.in); int id = scan.nextInt(); // 输入性别并判断 TelNoteRegex sex1 = new TelNoteRegex(); String sex = sex1.sexRegex(); String sql = "update person set sex = \'" + sex + "\' where id = \'" + id + "\'"; ResultSet rs = st.executeQuery(sql); ResultSet rs5 = st.executeQuery("select * from person where id = \'" + id + "\'"); // 遍历结果集 if (!rs5.next()) { System.out.println("没有相关数据,系统退回到主菜单"); } else { rs = st.executeQuery(sql); System.out.println("修改成功"); } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return; } // 修改号码 public void modTelNum() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 获取ID的最小值 ResultSet rs1 = st.executeQuery("select min(id) from person"); int min = 0; rs1.next(); min = rs1.getInt(1); // 获取ID的最大值 ResultSet rs2 = st.executeQuery("select max(id) from person"); int max = 0; rs2.next(); max = rs2.getInt(1); System.out.println("请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max); Scanner scan = new Scanner(System.in); int id = scan.nextInt(); ResultSet rs5 = st.executeQuery("select * from person where id = \'" + id + "\'"); if (!rs5.next()) { System.out.println("没有相关数据,系统退回到主菜单"); // 遍历结果集 } else { // 输入号码并判断 TelNoteRegex telnum1 = new TelNoteRegex(); String telnum = telnum1.telnumRegex(); String sql = "update person set telnum = \'" + telnum + "\' where id = \'" + id + "\'"; ResultSet rs = st.executeQuery(sql); rs = st.executeQuery(sql); System.out.println("修改成功"); } st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return; } // 修改地址 public void modAdd() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); // 获取ID的最小值 ResultSet rs1 = st.executeQuery("select min(id) from person"); int min = 0; rs1.next(); min = rs1.getInt(1); // 获取ID的最大值 ResultSet rs2 = st.executeQuery("select max(id) from person"); int max = 0; rs2.next(); max = rs2.getInt(1); System.out.println("请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max); Scanner scan = new Scanner(System.in); int id = scan.nextInt(); // 输入地址并判断 TelNoteRegex add1 = new TelNoteRegex(); String add = add1.addressRegex(); String sql = "update person set address = \'" + add + "\' where id = \'" + id + "\'"; ResultSet rs = st.executeQuery(sql); ResultSet rs5 = st.executeQuery("select * from person where id = \'" + id + "\'"); // 遍历结果集 if (!rs5.next()) { System.out.println("没有相关数据,系统退回到主菜单"); } else { rs = st.executeQuery(sql); System.out.println("修改成功"); } rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return; } // 删除指定用户信息 public void delete() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); ResultSet rs1 = st.executeQuery("select min(id) from person"); ResultSet rs2 = st.executeQuery("select max(id) from person"); System.out.println(rs1); System.out.println("请输入要删除的ID"); Scanner scan = new Scanner(System.in); int id = scan.nextInt(); String sql = "delete from Person where id = \'" + id + "\'"; ResultSet rs = st.executeQuery(sql); rs = st.executeQuery(sql); System.out.println("删除成功"); rs.close(); st.close(); cp.close(); } catch (Exception e) { e.printStackTrace(); } return; } // 删除全部记录 public void deleteAll() { List<Person> rtn = new ArrayList<>(); try { Connection conn = cp.getConnection(); Statement st = conn.createStatement(); String sql = "truncate table person"; ResultSet rs = st.executeQuery(sql); rs = st.executeQuery(sql); System.out.println("全部数据已经清空"android小知识点代码片段