日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2024/3/13 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手机通讯录系统(三层架构+JDBC+MySQL) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 效果展示
  • 整體架構
    • 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。