文章目录

  • 效果展示
  • 整体架构
    • sql语句(Person表)
    • (1)entity实体类(Person.java)
    • (2)rowmapper封装结果集(RowMapper.java)
    • (3)service层
    • (4)view视图层

效果展示

这可能是最后一次写在控制台上跑的东西了,感觉很low,以后都投身web端了。仅仅用来测试一下连接数据库。

整体架构

sql语句(Person表)

create table person(id int(7) primary key auto_increment,name varchar(20) not null,mobile varchar(13) check( mobile like '___%-_______%'),telephone varchar(11) not null unique check( length(telphone)=11 ),email varchar(30) unique check( email like '_%@%_' ),city varchar(20),birthday date
);
Insert into person values(null,'wxd', '111-11111111', '13051800687','wxd@zzu.com','zz','2020-01-10');
Insert into person values(null,'周冬雨', '123-12580000', '18572136217','zdy@zzu.com','sh','2020-01-11');
Insert into person values(null,'周董', '124-12372300', '15572136217','zd@zzu.com','bj','2010-02-21');

(1)entity实体类(Person.java)

使用ORM思想,类中的属性,映射表中的字段。

package entiry;import java.sql.Date;/*** 类说明*        Person实体类* @author qianliangguo*/
public class Person {private Integer id;private String name;private String mobile;private String telephone;private String email;private String city;private Date birthday;public Person() {super();// TODO Auto-generated constructor stub}public Person(Integer id, String name, String mobile, String telephone,String email, String city, Date birthday) {super();this.id = id;this.name = name;this.mobile = mobile;this.telephone = telephone;this.email = email;this.city = city;this.birthday = birthday;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public String getTelephone() {return telephone;}public void setTelephone(String telephone) {this.telephone = telephone;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", mobile="+ mobile + ", telephone=" + telephone + ",email"+email+",city" + city + ",birthday" + birthday + "]";}
}

(2)rowmapper封装结果集(RowMapper.java)

封装结果集接口RowMapper.java

package rowmapper;import java.sql.ResultSet;/*** 类说明:*       封装结果集接口* * @author qianliangguo*/
public interface RowMapper<T> {public T mapperRow(ResultSet rs);}

封装结果集类PersonRowmapper.java

package rowmapper;import java.sql.ResultSet;
import java.sql.SQLException;import entiry.Person;/*** 类说明:*        将ResultSet封装成一个对象* @author qianliangguo*/
public class PersonRowMapper implements RowMapper {@Overridepublic Object mapperRow(ResultSet rs) {Person person = new Person();try {person.setId(rs.getInt(1));person.setName(rs.getString(2));person.setMobile(rs.getString(3));person.setTelephone(rs.getString(4));person.setEmail(rs.getString(5));person.setCity(rs.getString(6));person.setBirthday(rs.getDate(7));} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return person;}
}

(3)service层

service接口

package service;import entity.Person;/*** 类说明:*      Service层接口* @author qianliangguo*/
public interface PersonService {//查询所有联系人信息public void queryAllPerson();//根据姓名查找联系人信息public Person QueryPersonByName(String name);//根据电话号查找联系人信息public Person QueryPersonByMobile(String mobile);//插入联系人信息public void insertPerson(Person person);//删除联系人public void deletePerson(String name);//修改联系人信息public void updatePerson(Person person);
}

service实现类

package service;import java.sql.Connection;
import java.util.List;import util.JdbcUtil3;import dao.PersonDao;
import dao.PersonDaoImp;
import entity.Person;/*** 类说明:*         Service类* @author qianliangguo*/
public class PersonServiceImp implements PersonService {PersonDao persondao = new PersonDaoImp();Connection conn = null;@Overridepublic void queryAllPerson() {try {conn = JdbcUtil3.getConnection();List<Person> person = persondao.queryAllPerson();for (Person p : person) {System.out.println(p);}} catch (Exception e) {System.out.println("数据库出现异常");}finally{try {JdbcUtil3.release(null, null, conn);} catch (Exception e) {throw new RuntimeException("释放资源发生异常");}}}@Overridepublic Person QueryPersonByName(String name) {Person person = persondao.queryPersonByName(name);/*异常情况:*        该姓名不存在*/if(person == null){throw new RuntimeException("该姓名不存在");}/*正常情况:*       加载驱动,获取连接...*/try {conn = JdbcUtil3.getConnection();   } catch (Exception e) {throw new RuntimeException("数据库异常");}finally{try {JdbcUtil3.release(null, null, conn);} catch (Exception e) {throw new RuntimeException("释放资源异常");}}return person;}@Overridepublic Person QueryPersonByMobile(String mobile) {Person person = persondao.queryPersonByMobile(mobile);/*异常情况:*       该电话号不存在*/if(person == null){throw new RuntimeException("该电话号码不存在");}/*正常情况:*        加载驱动,获取连接...*/try {conn = JdbcUtil3.getConnection();   } catch (Exception e) {throw new RuntimeException("数据库异常");}finally{try {JdbcUtil3.release(null, null, conn);} catch (Exception e) {throw new RuntimeException("释放资源异常");}}return person;}@Overridepublic void insertPerson(Person person) {try {conn = JdbcUtil3.getConnection();persondao.insertPerson(person);} catch (Exception e) {throw new RuntimeException("数据库异常");}finally{try {JdbcUtil3.release(null, null, conn);} catch (Exception e) {throw new RuntimeException("释放资源异常");}}}@Overridepublic void deletePerson(String name) {try {conn= JdbcUtil3.getConnection();persondao.deletePerson(name);} catch (Exception e) {throw new RuntimeException("数据库异常");}finally{try {JdbcUtil3.release(null, null, conn);} catch (Exception e) {throw new RuntimeException("释放资源异常");}}}@Overridepublic void updatePerson(Person person) {try {conn = JdbcUtil3.getConnection();persondao.updatePerson(person);} catch (Exception e) {throw new RuntimeException("数据库异常");}finally{try {JdbcUtil3.release(null, null, conn);} catch (Exception e) {throw new RuntimeException("释放资源异常");}}}
}

(4)view视图层

package view;/*** 类说明:*      视图层* @author qianliangguo*/import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;import service.PersonService;
import service.PersonServiceImp;import entity.Person;public class TellBookView {private static Scanner in = new Scanner(System.in);private static PersonService ps = new PersonServiceImp();public static void main(String[] args) throws Exception {showMain();}public static void showMain() throws Exception {System.out.println("***************欢迎访问通讯录**************");System.out.println("1.显示所有联系人      2.按姓名查找联系人      3.按号码查找联系人");System.out.println("4.添加联系人              5.删除联系人                  6.修改联系人信息    ");System.out.println("7.退出");while (true) {System.out.println("请给出你的选择:");int n = in.nextInt();switch (n) {case 1:showPerson();break;case 2:showPersonByName();;break;case 3:showPersonByMobile();break;case 4:insertPerson();break;case 5:deletePerson();break;case 6:updatePerson();break;case 7:System.out.println("您已经退出系统");System.exit(0);break;default:throw new RuntimeException("您输入有误");}}}/*** 方法说明: *       void showPerson():显示所有联系人* */public static void showPerson() {ps.queryAllPerson();}/*** 方法说明: *         showPersonByName():根据姓名查找联系人*/public static void showPersonByName() throws Exception {String name = null;System.out.println("请输入姓名:");name = in.next();Person p = ps.QueryPersonByName(name);System.out.println(p);}/*** 方法说明: *       showPersonByMobile():根据号码查找联系人*/public static void showPersonByMobile() {String mobile = null;System.out.println("请输入电话:");mobile = in.next();Person p = ps.QueryPersonByMobile(mobile);System.out.println(p);}/*** 方法说明: *        insertPerson():插入联系人*/public static void insertPerson() {try {System.out.println("请输入添加联系人的姓名:");String name = in.next();System.out.println("请输入添加联系人的mobile:");String mobile = in.next();System.out.println("请输入添加联系人的telphone:");String telphone = in.next();System.out.println("请输入添加联系人的email:");String emil = in.next();System.out.println("请输入添加联系人的city:");String city = in.next();System.out.println("请输入添加联系人的birthday:");String next = in.next();// 设置指定的日期格式SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 将String转化为指定格式的java.uti.Datejava.util.Date utildate = sdf.parse(next);// 将util.date转化为longlong time = utildate.getTime();// 创建java.sql.date(long time)以便于存入数据库java.sql.Date birthday = new java.sql.Date(time);Person person = new Person(null, name, telphone, mobile, emil,city, birthday);ps.insertPerson(person);System.out.println("插入完成");} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 方法说明: *         deletePerson():删除联系人*/public static void deletePerson() {String name = null;System.out.println("请输入要删除联系人的姓名:");name = in.next();ps.deletePerson(name);System.out.println("删除成功");}/*** 方法说明: *       updatePerson():修改联系人信息*/public static void updatePerson() {try {System.out.println("请输入需要修改信息的联系人姓名:");String name = in.next();System.out.println("请输入修改后联系人的mobile:");String mobile = in.next();System.out.println("请输入修改后联系人的telphone:");String telphone = in.next();System.out.println("请输入修改后联系人的email:");String emil = in.next();System.out.println("请输入修改后联系人的city:");String city = in.next();System.out.println("请输入修改后联系人的birthday:");String next = in.next();// 设置指定的日期格式SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 将String转化为指定格式的java.uti.Datejava.util.Date utildate = sdf.parse(next);// 将util.date转化为longlong time = utildate.getTime();// 创建java.sql.date(long time)以便于存入数据库java.sql.Date birthday = new java.sql.Date(time);Person person = new Person(null, name, telphone, mobile, emil,city, birthday);ps.updatePerson(person);System.out.println("修改完成");} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}

手机通讯录系统(三层架构+JDBC+MySQL)相关推荐

  1. 物流快递系统前、后端+Java语言+SpringBoot项目+MVC三层架构+maven+Mysql+Tomcat+可以用于学习SpringBoot项目入门

    物流快递系统前.后端+Java语言+SpringBoot项目+MVC三层架构+Mysql+Tomcat+可以用于学习SpringBoot项目入门 可以用于课程设计.毕业设计的知识点入门学习 提示:此资 ...

  2. C语言手机通讯录系统

    C语言手机通讯录系统 任务3 题目:手机通讯录系统 任务描述 (1)设计一个对手机通讯录信息进行查询.编辑.添加.删除等操作的管理程序. (2)通讯录的基本信息包括姓名.年龄.联系电话.类别(朋友.家 ...

  3. c语言项目手机通讯录系统

    刚学不久c语言,本来这个是学校的大作业,也是花了我差不多三天左右的时间,写了一个通讯录系统,这也是我第一次发博客,以后还会发自己学习编程的一些学习心得啊这样子. 1,主要实现功能: 设计一个手机通讯录 ...

  4. 基于javaweb的在线蛋糕商城系统(java+jsp+jdbc+mysql)

    基于javaweb的在线蛋糕商城系统(java+jsp+jdbc+mysql) 运行环境 Java≥8.MySQL≥5.7.Tomcat≥8 开发工具 eclipse/idea/myeclipse/s ...

  5. 在qt实现手机通讯录系统_通讯录管理系统的设计与实现(QT,SQlite)

    通讯录管理系统的设计与实现(QT,SQlite)(任务书,外文翻译,毕业论文15000字,程序代码,数据库,答辩PPT) 摘 要 现今社会随着通讯以及交通的发展,人与人之间的联系越来越多,越来越紧密, ...

  6. python 通讯录系统_Python基础项目:手机通讯录系统

    完成简易手机通讯录管理系统,包括以下功能: 能够循环接收客户端输入的功能编号,并根据编号选择对应的功能操作 用户输入"1"时,执行增加姓名和手机号码的操作 用户输入"2& ...

  7. Java项目:家居购物商城系统(java+html+jdbc+mysql)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 功能: Java Web精品项目源码,家居商城分类展示,商品展示, 商品下单,购物车,个人中心,后台管理,用户管理,商品管理, ...

  8. Java项目:在线蛋糕商城系统(java+jsp+jdbc+mysql)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 功能: 主页显示热销商品:所有蛋糕商品展示,可进行商品搜 索:点击商品进入商品详情页,具有立即购买和加入购物 车功能,可增减购 ...

  9. 在qt实现手机通讯录系统_Qt编写自定义控件55-手机通讯录

    一.前言 前面几篇文章中的控件基本上难度系数接近0,甚至有凑控件数量的嫌疑,这次必须来一个强悍的控件,本控件难度系数在所有控件中排前五,代码量也不少,头文件都550行,实现文件1600行,为什么这么多 ...

最新文章

  1. 使用OpenCV,Python进行图像哈希(差分哈希 dHash)处理
  2. org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters
  3. /lib64/libc.so.6 is not a symbolic link 解决方法
  4. OpenJudge 2739 计算对数
  5. 两台电脑可以用同一IP上网吗?
  6. 在IBM Cloud中运行Fabric
  7. html5--6-50 动画效果-变形
  8. html按键变色,按键变色.html
  9. SQL Server 日期转换格式
  10. web 前端绘制折线_html5绘制折线图
  11. 基于 POI 封装 ExcelUtil 精简的 Excel 导入导出
  12. SQL Server 2008无日志文件附加数据库
  13. ElementUI:设置导航栏选中文字样式
  14. 手机QQ空间装逼代码收集
  15. C# 如何在Excel表格中插入、编辑和删除批注
  16. 火车采集器V2010免费版下载
  17. python matplotlib plt 画图总结
  18. 【PMP】学习笔记 第6章 时间管理
  19. 移动端微信、QQ浏览器 web 用 rem 单位适配不生效问题
  20. 纳什均衡-- 硬币正反

热门文章

  1. Mac下Android studio搭建Android开发环境【新手】
  2. linux驱动系列学习之DRM(十)
  3. sap系统中的batch_SAP 批次管理(Batch management)配置介绍
  4. 仿和牛的3D打印肉,动物干细胞「生长」而成,你会吃么?
  5. 【宽创案例】青海牦牛文化馆:走进牦牛之都!
  6. HDU 6078 Wavel Sequence【动态规划】
  7. 关于个人目标的一篇博客
  8. 后端开发规范(持续更新中...)
  9. 【Verilog数字系统设计——完成如下公式所表示的逻辑功能模块】
  10. 在虚拟机里安装windows