效果展示
这可能是最后一次写在控制台上跑的东西了,感觉很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;
}
@Override
public 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 {
@Override
public 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 block
e.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;
@Override
public 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("释放资源发生异常");
}
}
}
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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("释放资源异常");
}
}
}
@Override
public 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("释放资源异常");
}
}
}
@Override
public 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.Date
java.util.Date utildate = sdf.parse(next);
// 将util.date转化为long
long 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 block
e.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.Date
java.util.Date utildate = sdf.parse(next);
// 将util.date转化为long
long 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 block
e.printStackTrace();
}
}
}
转载:https://blog.csdn.net/weixin_43691058/article/details/103929408
查看评论