您的位置:首页 > 编程语言 > Java开发

telbook雏形

2014-03-25 09:01 302 查看
1.该作品实现的是由两个表(学生表、班级表)组成的通讯录的增删改查功能。

2.导入ojdbc5.jar包,建立package层

——util

                ——dao

                        ——entity

                        ——conf

                        ——servece

                        ——view

                        ——test

——util

package com.zpark.myr.util;

import java.io.IOException;

import java.io.InputStream;

import java.sql.*;

import java.util.Properties;

public class JdbcUtil {
//properties
private static Properties prop = new Properties();
//静态代码块
static{
//使用Class对象的方法从bin目录及其子目录中获取文件流
InputStream is = JdbcUtil.class.getResourceAsStream("/com/zpark/myr/conf/telbook.properties");
//利用properties属性读取文件
try {
prop.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//创建线程局部变量
private static final ThreadLocal<Connection> t = new ThreadLocal<Connection>();
//创建连接
public static Connection getConn()throws Exception{
//定义变量
String className=prop.getProperty("myDriver");
String url = prop.getProperty("myUrl");
String user = prop.getProperty("userName");
String psd = prop.getProperty("passWord");
Connection conn=null;
//获得conn
conn = t.get();
//判断conn是否为空
if(conn==null){
//为空  创建连接
//注册驱动类
Class.forName(className);
//创建连接
conn = DriverManager.getConnection(url,user,psd);
System.out.println("--------------util-----------"+conn);
//设置连接
t.set(conn);
}
//返回
return conn;
}
//释放资源
public static void release(ResultSet rs,Statement stm,Connection conn)throws Exception{
if(rs!=null) rs.close();
if(stm!=null) stm.close();
if(conn!=null){
conn.close();
t.remove();
}
}

}

——dao

——PersonDao

package com.zpark.myr.dao;

import java.util.List;

import com.zpark.myr.entity.Category;

import com.zpark.myr.entity.Person;

public interface PersonDao {
//增
public void insert(Person ps, int cid)throws Exception;
//删
public void delete(int id)throws Exception;
//改
public void update(Person ps, int cid)throws Exception;
//根据PK查
public Person selectId(int id)throws Exception;
//查所有方法
public List<Person> selectAll()throws Exception;
//根据联系人姓名模糊查找联系人的方法
public List<Person> selectName(String names)throws Exception;
//根据手机号码模糊查询相关的联系人的方法
public List<Person> selectMobile(String mobiles)throws Exception;

}

——PersonDaoImpl

package com.zpark.myr.dao;

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 com.zpark.myr.entity.Category;

import com.zpark.myr.entity.Person;

import com.zpark.myr.util.JdbcUtil;

public class PersonDaoImpl implements PersonDao{
//使用实体类,含增、删、改、根据PK查、查所有的方法
//增
public void insert(Person ps,int cid)throws Exception{
//创建连接
Connection conn = JdbcUtil.getConn();
System.out.println("----------------insert--------------"+conn);
//创建pstm
//select id,name,mobile,telphone,email,city,birthday from person
String sql = "insert into test_person values(person_seq.nextval,?,?,?,?,?,?,?)";
PreparedStatement pstm = conn.prepareStatement(sql);
//执行sql 
pstm.setString(1, ps.getName());
pstm.setString(2, ps.getMobile());
pstm.setString(3, ps.getTelphone());
pstm.setString(4,ps.getEmail());
pstm.setString(5,ps.getCity());
pstm.setDate(6,ps.getBirthday());
//查询cid是否存在,若存在,则直接插入
//若不存在,先创建对象,再插入
CategoryDaoImpl cDao=new CategoryDaoImpl();
//
System.out.println("---1------");
Category cate = cDao.selectOne(cid);
//
System.out.println("------2--------");
System.out.println(cate.getId());
pstm.setInt(7, cate.getId());

int row =pstm.executeUpdate();
if(row!=0){
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
//处理结果

//释放资源
JdbcUtil.release(null, pstm, null);
}
//删
public void delete(int id)throws Exception{
//创建连接
Connection conn = JdbcUtil.getConn();
//创建pstm
//select id,name,mobile,telphone,email,city,birthday from person
String sql = "delete from test_person where id=?";
PreparedStatement pstm = conn.prepareStatement(sql);
//执行sql
pstm.setInt(1, id);
int row=pstm.executeUpdate();
if(row==0){
System.out.println("该行咩存在");
}else{
System.out.println("删除成功");
}
//处理结果
//释放资源
JdbcUtil.release(null, pstm, null);
}
//改
public void update(Person ps,int cid)throws Exception{
//创建连接
Connection conn = JdbcUtil.getConn();
//创建pstm
//select id,name,mobile,telphone,email,city,birthday from person
String sql = "update  test_person set name=?, mobile=?,telphone=?,email=?,city=?,birthday=?,cid=? where id=?";
PreparedStatement pstm = conn.prepareStatement(sql);
//执行sql
pstm.setString(1, ps.getName());
pstm.setString(2, ps.getMobile());
pstm.setString(3, ps.getTelphone());
pstm.setString(4,ps.getEmail());
pstm.setString(5,ps.getCity());
pstm.setDate(6, ps.getBirthday());
//修改没有考虑给它改的班级存在不存在
ps.setCid(cid);
pstm.setInt(7, ps.getCid());
pstm.setInt(8, ps.getId());

int row = pstm.executeUpdate();
if(row!=0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
//处理结果

//释放资源
JdbcUtil.release(null, pstm, null);
}
//根据PK查
public Person selectId(int id)throws Exception{
Person ps = null;
//创建连接
Connection conn  = JdbcUtil.getConn();
//创建pstm
String sql = "select p.id,p.name,p.mobile,p.telphone,p.email,p.city,p.birthday,p.cid,c.cname from test_person p join test_category c on p.cid=c.cid where p.id=?";
PreparedStatement pstm = conn.prepareStatement(sql);
//执行sql
pstm.setInt(1,id);
ResultSet rs = pstm.executeQuery();
//处理结果
if(rs.next()){
String name = rs.getString(2);
String mobile = rs.getString(3);
String telphone=rs.getString(4);
String email=rs.getString(5);
String city =rs.getString(6);
String birthday = rs.getString(7);
int cid = rs.getInt(8);
String cname = rs.getString(9);

// java.sql.Date birthday = rs.getDate(7);
//封装数据库对象类
ps=new Person();
ps.setBirthday(birthday);
ps.setCity(city);
ps.setEmail(email);
ps.setId(id);
ps.setMobile(mobile);
ps.setName(name);
ps.setTelphone(telphone);

Category c =new Category();
c.setId(cid);
c.setName(cname);

ps.setCategory(c);
//测试输出是否正确

}else{
System.out.println("数据未找到");
}
//释放资源
JdbcUtil.release(rs, pstm, null);
return ps;
}
//查所有方法
public List<Person> selectAll()throws Exception{
List<Person> psList =new ArrayList<Person>();
//创建连接
Connection conn  = JdbcUtil.getConn();
//创建pstm
Statement stm = conn.createStatement();
//执行sql
String sql = "select p.id,p.name,p.mobile,p.telphone,p.email,p.city,p.birthday,c.cid,c.cname from test_person p join test_category c on p.cid=c.cid";
ResultSet rs = stm.executeQuery(sql);
//处理结果
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
String mobile = rs.getString(3);
String telphone=rs.getString(4);
String email=rs.getString(5);
String city =rs.getString(6);
String birthday = rs.getString(7);
int cid = rs.getInt(8);
String cname = rs.getString(9);
// java.sql.Date birthday = rs.getDate(7);
//测试是否获取成功 success 而且先执行 执行完后才会输出list集合
// System.out.println(id+" "+name+" " +mobile+" "+telphone+" "+email+" "+city+" "+birthday );
Person ps = new Person();
//封装数据库对象类
ps.setBirthday(birthday);
ps.setCity(city);
ps.setEmail(email);
ps.setId(id);
ps.setMobile(mobile);
ps.setName(name);
ps.setTelphone(telphone);
Category c = new Category();
c.setId(cid);
c.setName(cname);
ps.setCategory(c);

//将Person对象添加进List集合
psList.add(ps);
//测试输出是否正确
}
//释放资源
JdbcUtil.release(rs, stm, null);
return psList;
}
//根据联系人姓名模糊查找联系人的方法
public List<Person> selectName(String names)throws Exception{
List<Person> psList =new ArrayList<Person>();
//创建连接
Connection conn  = JdbcUtil.getConn();
//创建pstm
String sql = "select p.id,p.name,p.mobile,p.telphone,p.email,p.city,p.birthday,c.cid,c.cname from test_person p join test_category c on p.cid=c.cid where name like ?";
PreparedStatement pstm = conn.prepareStatement(sql);
//执行sql
pstm.setString(1, '%'+names+'%');
ResultSet rs = pstm.executeQuery();
//处理结果
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
String mobile = rs.getString(3);
String telphone=rs.getString(4);
String email=rs.getString(5);
String city =rs.getString(6);
String birthday = rs.getString(7);
int cid = rs.getInt(8);
String cname = rs.getString(9);
// java.sql.Date birthday = rs.getDate(7);
//测试是否获取成功 success 而且先执行 执行完后才会输出list集合
// System.out.println(id+" "+name+" " +mobile+" "+telphone+" "+email+" "+city+" "+birthday );
Person ps = new Person();
//封装数据库对象类
ps.setBirthday(birthday);
ps.setCity(city);
ps.setEmail(email);
ps.setId(id);
ps.setMobile(mobile);
ps.setName(name);
ps.setTelphone(telphone);

Category c = new Category();
c.setId(cid);
c.setName(cname);
ps.setCategory(c);
//将Person对象添加进List集合
psList.add(ps);
//测试输出是否正确
}
//释放资源
JdbcUtil.release(rs, pstm, null);
return psList;
}
//根据手机号码模糊查询相关的联系人的方法
public List<Person> selectMobile(String mobiles)throws Exception{
List<Person> psList =new ArrayList<Person>();
//创建连接
Connection conn  = JdbcUtil.getConn();
//创建pstm
String sql = "select p.id,p.name,p.mobile,p.telphone,p.email,p.city,p.birthday,c.cid,c.cname from test_person p join test_category c on p.cid=c.cid where mobile like ?";
PreparedStatement pstm = conn.prepareStatement(sql);
//执行sql
pstm.setString(1, '%'+mobiles+'%');
ResultSet rs = pstm.executeQuery();
//处理结果
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
String mobile = rs.getString(3);
String telphone=rs.getString(4);
String email=rs.getString(5);
String city =rs.getString(6);
String birthday = rs.getString(7);
int cid = rs.getInt(8);
String cname=rs.getString(9);
// java.sql.Date birthday = rs.getDate(7);
//测试是否获取成功 success 而且先执行 执行完后才会输出list集合
// System.out.println(id+" "+name+" " +mobile+" "+telphone+" "+email+" "+city+" "+birthday );
Person ps = new Person();
//封装数据库对象类
ps.setBirthday(birthday);
ps.setCity(city);
ps.setEmail(email);
ps.setId(id);
ps.setMobile(mobile);
ps.setName(name);
ps.setTelphone(telphone);

Category c = new Category();
c.setId(cid);
c.setName(cname);
ps.setCategory(c);
//将Person对象添加进List集合
psList.add(ps);
//测试输出是否正确
}
//释放资源
JdbcUtil.release(rs, pstm, null);
return psList;
}

}

——CategoryDao

package com.zpark.myr.dao;

import java.util.List;

import java.util.Set;

import com.zpark.myr.entity.Category;

import com.zpark.myr.entity.Person;

public interface CategoryDao {

  //添加类别,需要判断类别名字是否重复,不允许重复

  public void addCategory(String categoryName) throws Exception;

 

  //删除类别

  public void dropCategory(Integer categoryId) throws Exception;

 

  //修改类别,只能修改类别名,不能和其他类别重名

  public void changeCategoryName(Category c) throws Exception;

 

  //查询所有类别

  public List<Category> getAllCategory() throws Exception;

 
//查person
public List<Person>  getPersonByCategoryId(Integer categoryId) throws Exception;

}

——CategoryDaoImpl

package com.zpark.myr.dao;

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 com.zpark.myr.entity.Category;

import com.zpark.myr.entity.Person;

import com.zpark.myr.util.JdbcUtil;

public class CategoryDaoImpl implements CategoryDao {
//添加类名
@Override
public void addCategory(String categoryName) throws Exception {
// TODO Auto-generated method stub
//创建连接
Connection conn=JdbcUtil.getConn();
//创建pstm
String sql = "insert into test_category values(seq_category.nextval,?)";
PreparedStatement pstm = conn.prepareStatement(sql);
//设定?,执行sql语句
pstm.setString(1,categoryName);
int row =pstm.executeUpdate();
if(row==0){
System.out.println("插入失败");
}else{
System.out.println("插入成功");
}
//处理结果
//释放资源
JdbcUtil.release(null, pstm, null);

}
//删除类名
@Override
public void dropCategory(Integer categoryId) throws Exception {
CategoryDaoImpl cDao = new CategoryDaoImpl();
List<Person> psList = new ArrayList<Person>();
PersonDao psDao  = new PersonDaoImpl();
// TODO Auto-generated method stub
//创建连接
Connection conn = JdbcUtil.getConn();
//创建pstm
String sql = "delete from test_category where cid=?";
PreparedStatement pstm = conn.prepareStatement(sql);
//设定?,执行sql语句
pstm.setInt(1, categoryId);
//查询

psList=cDao.getPersonByCategoryId(categoryId);
if(psList!=null){
//将人们转到默认类别
//用到更改属性
for(Person ps :psList){
psDao.update(ps, 22);
}
}
int row = pstm.executeUpdate();
if(row==0){
System.out.println("删除失败");
}else{
System.out.println("删除成功");
}
//处理结果
//释放资源
JdbcUtil.release(null, pstm, null);

}
//修改类名
@Override
public void changeCategoryName(Category c) throws Exception {
//创建连接
Connection conn = JdbcUtil.getConn();
//创建pstm
String sql = "update test_category set cname=? where cid=? ";
PreparedStatement pstm = conn.prepareStatement(sql);
//设定?,执行sql语句
pstm.setString(1, c.getName());
pstm.setInt(2, c.getId());
int row = pstm.executeUpdate();
if(row==0){
System.out.println("删除失败");
}else{
System.out.println("删除成功");
}
//处理结果
//释放资源
JdbcUtil.release(null, pstm, null);
}
//查找所有类名
@Override
public List<Category> getAllCategory() throws Exception {
List<Category> cList = new ArrayList<Category>();
//创建连接
Connection conn = JdbcUtil.getConn();
//创建stm

Statement stm = conn.createStatement();
//设定?,执行sql语句
String sql = "select cid,cname from test_category ";
ResultSet rs= stm.executeQuery(sql);

//处理结果
while(rs.next()){
int cid = rs.getInt(1);
String cname=rs.getString(2);
/*//
System.out.println(cid+" "+cname);
//

*/
Category cate = new Category();
cate.setId(cid);
cate.setName(cname);
cList.add(cate);
}
//释放资源
JdbcUtil.release(null, stm, null);
return cList;
}
//根据类名查找所有人
@Override
public List<Person> getPersonByCategoryId(Integer categoryId)throws Exception {
// TODO Auto-generated method stub
List<Person> psList = new ArrayList<Person>();
//创建连接
Connection conn = JdbcUtil.getConn();
//创建stm
String sql = "select p.id,p.name,p.mobile,p.telphone,p.email,p.city,p.birthday ,p.cid,c.cname from test_person p join test_category c on p.cid=c.cid where c.cid=?";
PreparedStatement pstm = conn.prepareStatement(sql);
//执行sql语句
pstm.setInt(1, categoryId);
ResultSet rs = pstm.executeQuery();
//处理结果
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
String mobile = rs.getString(3);
String telphone = rs.getString(4);
String email = rs.getString(5);
String city = rs.getString(6);
String birthday = rs.getString(7);
int cid = rs.getInt(8);
String cname = rs.getString(9);
//

Category cate = new Category();
cate.setId(cid);
cate.setName(cname);

Person ps = new Person();
ps.setBirthday(birthday);
ps.setCity(city);
ps.setEmail(email);
ps.setId(id);
ps.setMobile(mobile);
ps.setName(name);
ps.setTelphone(telphone);
ps.setCategory(cate);

//
//
psList.add(ps);
}
//释放资源
JdbcUtil.release(rs, pstm, null);
return psList;
}
//根据类Id查询类名
public Category selectOne(int cid)throws Exception{
Category cate = new Category();
//
Connection conn = JdbcUtil.getConn();
System.out.println("--------------------------Cate selectOne---------------"+conn);
//
String sql = "select cid,cname from test_category where cid = ?";
PreparedStatement pstm = conn.prepareStatement(sql);
//
pstm.setInt(1, cid);
ResultSet rs = pstm.executeQuery();
//
if(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
System.out.println("--------------"+id+"------------"+name+"------------");
//封装

cate.setId(cid);
cate.setName(name);
System.out.println(cate.getName());
}else{
//cate = new Category();
cate.setId(22);
cate.setName("这是默认的");
// CategoryDao cDao = new CategoryDaoImpl();
System.out.println(cate.getName());
// cDao.addCategory(cate.getName());
System.out.println("----------"+cate.getId()+"--------"+cate.getName());
System.out.println(cate.getName());
/*System.out.println("不存在该类别");
return null;*/
}
//资源没有释放
JdbcUtil.release(rs, pstm, null);
//
return cate;
}

}

——entity

——Person

package com.zpark.myr.entity;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Person {
//select id,name,mobile,telphone,email,city,birthday from person
private Integer id;
private String name;
private String mobile;
private String telphone;
private String email;
private String city;
private String birthday;
private Category category=new Category();
//
private Integer cid;
//
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 getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
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 java.sql.Date getBirthday() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date utilDate = sdf.parse(birthday);
java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());
return sqlDate;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
//
/*@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", mobile=" + mobile
+ ", telphone=" + telphone + ", email=" + email + ", city="
+ city + ", birthday=" + birthday + "]";
}*/

/*
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
+ ", telphone=" + telphone + ", email=" + email + ", city="
+ city + ", birthday=" + birthday + ", cid=" + cid
+ "]";
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
//
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}

}

——Category

package com.zpark.myr.entity;

import java.util.ArrayList;

import java.util.List;

public class Category {
private Integer id;
private String name;
private List<Person> psList = new ArrayList<Person>();
//
public Category() {
super();
}
//
public Category(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
//
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;
}
//
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
//
public List<Person> getPsList() {
return psList;
}
public void setPsList(List<Person> psList) {
this.psList = psList;
}

}

——conf

myDriver=oracle.jdbc.OracleDriver

myUrl=jdbc:oracle:thin:@localhost:1521:xe

userName=****

passWord=****

——service

——TelbookService

package com.zpark.myr.service;

import java.util.List;

import com.zpark.myr.entity.Category;

import com.zpark.myr.entity.Person;

public interface TelbookService {
// 1.根据PK查询联系人
public Person getPersonById (int id)throws Exception;

// 2.显示所有联系人 
public List<Person> getAllPerson() throws Exception;

// 3.按姓名查找联系人 
public List<Person> getPersonByName(String name)throws Exception;

// 4.按号码查找联系人
public List<Person> getPersonByMobile(String mobile)throws Exception;
/*// 4.添加联系人 
public void regist(Person person) throws Exception;*/

// 5.添加联系人需要使用的业务方法
//两表关系
//该方法需要在Person端创建Category“一”的属性关系

    public void regist(Person person,int categoryId) throws Exception;

    // 6.删除联系人
public void dropPersonById(Integer id) throws Exception;
/*// 6.修改联系人信息
public void changePersonMessage(Person person)throws Exception;*/

// 7.修改联系人时需要调用的业务方法
//两表关系
//该方法需要在person端创建Category“一”的属性关系

  public void changePersonMessage(Person person,Integer cid )throws Exception;

 

  //8.根据类别查找联系人

  //两表关系

  //该方法需要在Category中创建person集合

  public List<Person>  getPersonByCategoryId(Integer categoryId) throws Exception;

 

  //9.查询所有类别

  public List<Category> getAllCategory() throws Exception;

 

  //10.添加类别,需要判断类别名字是否重复,不允许重复

  public void addCategory(String categoryName) throws Exception;

 

  //11.删除类别

  public void dropCategory(Integer categoryId) throws Exception;

 

  //12.修改类别,只能修改类别名,不能和其他类别重名

  public void changeCategoryName(Category c) throws Exception;

 

}

——TelbookServiceImpl

package com.zpark.myr.service;

import java.sql.Connection;

import java.util.ArrayList;

import java.util.List;

import com.zpark.myr.dao.CategoryDao;

import com.zpark.myr.dao.CategoryDaoImpl;

import com.zpark.myr.dao.PersonDao;

import com.zpark.myr.dao.PersonDaoImpl;

import com.zpark.myr.entity.Category;

import com.zpark.myr.entity.Person;

import com.zpark.myr.util.JdbcUtil;

public class TelbookServiceImpl implements TelbookService{
// 1.根据PK查询联系人
public Person getPersonById (int id)throws Exception{
PersonDao psd = new PersonDaoImpl();
Person ps =null;
//创建连接
Connection conn = JdbcUtil.getConn();
//事务手动转自动
conn.setAutoCommit(false);
//
try{
//Dao
ps =psd.selectId(id);
//关闭事务
conn.commit();
}catch(Exception e){
e.printStackTrace();
conn.rollback();
throw e;
}finally{
//释放资源
JdbcUtil.release(null, null, conn);
}
return ps;
}

// 2.显示所有联系人 
public List<Person> getAllPerson() throws Exception{
PersonDao psd = new PersonDaoImpl();
List<Person> psList = null;
//创建连接
Connection conn = JdbcUtil.getConn();
//事务手动转自动
conn.setAutoCommit(false);
//
try{
//Dao
psList = psd.selectAll();
//关闭事务
conn.commit();
}catch(Exception e){
e.printStackTrace();
conn.rollback();
throw e;
}finally{
//释放资源
JdbcUtil.release(null, null, conn);
}
return psList;
}

// 3.按姓名查找联系人 
public List<Person> getPersonByName(String name)throws Exception{
PersonDao psd = new PersonDaoImpl();
List<Person> psList = null;
//创建连接
Connection conn = JdbcUtil.getConn();
//事务手动转自动
conn.setAutoCommit(false);
//
try{
//Dao
psList = psd.selectName(name);
//关闭事务
conn.commit();
}catch(Exception e){
e.printStackTrace();
conn.rollback();
throw e;
}finally{
//释放资源
JdbcUtil.release(null, null, conn);
}
return psList;
}

// 4.按号码查找联系人
public List<Person> getPersonByMobile(String mobile)throws Exception{
PersonDao psd = new PersonDaoImpl();
List<Person> psList = null;
//创建连接
Connection conn = JdbcUtil.getConn();
//事务手动转自动
conn.setAutoCommit(false);
//
try{
//Dao
psList = psd.selectMobile(mobile);
//关闭事务
conn.commit();
}catch(Exception e){
e.printStackTrace();
conn.rollback();
throw e;
}finally{
//释放资源
JdbcUtil.release(null, null, conn);
}
return psList;
}
/*// 4.添加联系人 
public void regist(Person person) throws Exception;*/

// 5.添加联系人需要使用的业务方法

    public void regist(Person person,int categoryId) throws Exception{

    //创建连接

    //自动改手动

    //Dao

    //提交、回滚

    //释放资源

    PersonDao psDao = new PersonDaoImpl();

  //创建连接

  Connection conn = JdbcUtil.getConn();

  //手动改自动

  conn.setAutoCommit(false);

 

  try{

  //Dao

  psDao.insert(person, categoryId);

  //关闭事务

  conn.commit();

  }catch(Exception e){

  e.printStackTrace();

  //事务回滚

  conn.rollback();

  throw e;

  }

  //释放资源

  JdbcUtil.release(null, null, conn);

    }

    // 6.删除联系人
public void dropPersonById(Integer id) throws Exception{
PersonDao psd = new PersonDaoImpl();
//创建连接
Connection conn = JdbcUtil.getConn();
//事务手动转自动
conn.setAutoCommit(false);
//
try{
//Dao
psd.delete(id);
//关闭事务
conn.commit();
}catch(Exception e){
e.printStackTrace();
conn.rollback();
throw e;
}finally{
//释放资源
JdbcUtil.release(null, null, conn);
}
}
/*// 6.修改联系人信息
public void changePersonMessage(Person person)throws Exception;*/

// 7.修改联系人时需要调用的业务方法

  public void changePersonMessage(Person person,Integer cid )throws Exception{

  PersonDao psDao = new PersonDaoImpl();

  //创建连接

  Connection conn = JdbcUtil.getConn();

  //手动改自动

  conn.setAutoCommit(false);

 

  try{

  //Dao

  psDao.update(person, cid);

  //关闭事务

  conn.commit();

  }catch(Exception e){

  e.printStackTrace();

  //事务回滚

  conn.rollback();

  throw e;

  }

  //释放资源

  JdbcUtil.release(null, null, conn);

  }

 

  //8.根据类别查找联系人

  public List<Person>  getPersonByCategoryId(Integer categoryId) throws Exception{

  List<Person> psList = new ArrayList<Person>();

  CategoryDao cDao = new CategoryDaoImpl();

  //连接

  Connection conn = JdbcUtil.getConn();

  //手动改自动

  conn.setAutoCommit(false);

 

  try{

  psList=cDao.getPersonByCategoryId(categoryId);

  //

  conn.commit();

  }catch(Exception e){

  e.printStackTrace();

  //

  conn.rollback();

  throw e;

  }

  //

  JdbcUtil.release(null, null, conn);

  return psList;

  }

 

  //9.查询所有类别

  public List<Category> getAllCategory() throws Exception{

  List<Category> cateList = new ArrayList<Category>();

  CategoryDao cDao = new CategoryDaoImpl();

  //连接

  Connection conn = JdbcUtil.getConn();

  //手动改自动

  conn.setAutoCommit(false);

 

  try{

  cateList=cDao.getAllCategory();

  //

  conn.commit();

  }catch(Exception e){

  e.printStackTrace();

  //

  conn.rollback();

  throw e;

  }

  //

  JdbcUtil.release(null, null, conn);

  return cateList;

  }

 

  //10.添加类别,需要判断类别名字是否重复,不允许重复

  public void addCategory(String categoryName) throws Exception{

  CategoryDao cDao = new CategoryDaoImpl();

  //连接

  Connection conn = JdbcUtil.getConn();

  //手动改自动

  conn.setAutoCommit(false);

 

  try{

  cDao.addCategory(categoryName);

  //

  conn.commit();

  }catch(Exception e){

  e.printStackTrace();

  //

  conn.rollback();

  throw e;

  }

  //

  JdbcUtil.release(null, null, conn);

  }

 

  //11.删除类别

  public void dropCategory(Integer categoryId) throws Exception{

  CategoryDao cDao = new CategoryDaoImpl();

  //连接

  Connection conn = JdbcUtil.getConn();

  //手动改自动

  conn.setAutoCommit(false);

 

  try{

  cDao.dropCategory(categoryId);

  //

  conn.commit();

  }catch(Exception e){

  e.printStackTrace();

  //

  conn.rollback();

  throw e;

  }

  //

  JdbcUtil.release(null, null, conn);

  }

 

  //12.修改类别,只能修改类别名,不能和其他类别重名

  public void changeCategoryName(Category c) throws Exception{

  CategoryDao cDao = new CategoryDaoImpl();

 

  //连接

  Connection conn = JdbcUtil.getConn();

  //手动改自动

  conn.setAutoCommit(false);

 

  try{

  cDao.changeCategoryName(c);

  //

  conn.commit();

  }catch(Exception e){

  e.printStackTrace();

  //

  conn.rollback();

  throw e;

  }

  //

  JdbcUtil.release(null, null, conn);

  }

}

——view

package com.zpark.myr.view;

import java.util.List;

import java.util.Scanner;

import com.zpark.myr.entity.Category;

import com.zpark.myr.entity.Person;

import com.zpark.myr.service.TelbookService;

import com.zpark.myr.service.TelbookServiceImpl;

//import com.zpark.myr.service.impl.TelbookServiceImpl;

public class TelBookView {
private Scanner sc=new Scanner(System.in);
private TelbookService  service=new TelbookServiceImpl();

public  void showMainView(){
while(true){
System.out.println("***************欢迎访问通讯录***************");
System.out.println("1.显示所有联系人       2.按姓名查询联系人    3.按号码查询联系人");
System.out.println("4.添加联系人   5.删除联系人               6.修改联系人信息");
System.out.println("7.根据类别查找联系人      8.显示所有类别      9.添加类别  ");
System.out.println("10.删除类别      11.修改类别名称       12.退出");
System.out.println("请选择操作:");
int selected=sc.nextInt();
requestDispatcher(selected);
}
}
public void  requestDispatcher(int selected) {
try {
switch(selected){
case 1:{ showAllPerson(); break;}
case 2:{ showPersonsByName(); break;}
case 3:{ showPersonsByMobil();break;}
case 4:{ addPerson(); break;}
case 5:{ dropPersonById(); break;}
case 6:{changePersonMessageById(); break;}
case 7:{showPersonsByCategory(); break;}
case 8:{showAllCategory(); break;}
case 9:{addCategory(); break;}
case 10:{dropCategory(); break;}
case 11:{changeCategoryName(); break;}
case 12:{ 
System.out.println("--------------------谢谢使用,再见------------------");
System.exit(0); 
break;
}
default:{
throw new Exception("输入错误,请考虑重新选择!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

//只供本类其他方法是用,用表格形式显示list集合里的联系人信息
private void showPersons(List<Person> list) throws Exception{
System.out.println("Id\tName\t  Mobile  \t  Telphone  \t    Email    \t  City  \t  Birthday  \t  CategoryName");
for(Person p:list){
      System.out.println(p.getId()+"\t"+p.getName()+"\t"+p.getMobile()+"\t"+p.getTelphone()+"\t"+p.getEmail()+"\t"+p.getCity()+"\t"+p.getBirthday()+"\t"+p.getCategory().getName());
   }
}

//用户选择功能1,显示所有联系人
public void  showAllPerson() throws Exception{
//调用service的查询方法,查询所有的联系人

      List<Person>  list=service.getAllPerson();

      //调用本类的方法显示联系人信息

      showPersons(list);
}
//用户选择功能2,按姓名查询联系人,允许模糊查询

   public void showPersonsByName() throws Exception{
  System.out.println("请输入需要查询的联系人姓名(可以模糊查询):");
  String name=sc.next();
  //调用service方法查询相关数据
  List<Person>  list=service.getPersonByName(name);
   //调用本类的方法显示联系人信息

      showPersons(list);

   }

   //用户选择功能3,按手机查询联系人,允许模糊查询
public void showPersonsByMobil() throws Exception{
System.out.println("请输入需要查询的联系人手机号码(可以模糊查询):");
String mobile=sc.next();
//调用service方法查询相关数据
  List<Person>  list=service.getPersonByMobile(mobile);
   //调用本类的方法显示联系人信息

      showPersons(list);
}
//用户选择功能4,添加联系人,允许用户名重复

    public void addPerson() throws Exception{

    System.out.println("请输入联系人姓名:");

    String name=sc.next();

    System.out.println("请输入联系人手机号码:");

    String mobile=sc.next();

    System.out.println("请输入联系人座机号码:");

    String telphone=sc.next();

    System.out.println("请输入联系人email:");

    String email=sc.next();

    System.out.println("请输入联系人地址:");

    String city=sc.next();

    System.out.println("请输入生日(1980-6-23):");

    String date=sc.next();

    //调用本类的方法,显示所有类别信息

    showAllCategory();

    System.out.println("请选择联系人所属类别:");

    int  cid=sc.nextInt();

   

    //调用service的regist()

    Person ps = new Person();

    ps.setBirthday(date);

    ps.setCid(cid);

    ps.setCity(city);

    ps.setEmail(email);

    ps.setMobile(mobile);

    ps.setName(name);

    ps.setTelphone(telphone);

    service.regist(ps, cid);

   

    System.out.println("添加联系人成功!!!!");

    }

    //用户选择功能5,删除联系人,需要输入联系人的id

    public void dropPersonById() throws Exception{

       System.out.println("请输入需要删除的联系人的id:");
  int id=sc.nextInt();

  //调用service的方法删除联系人
  service.dropPersonById(id);
  System.out.println("-----------删除成功----------");

    }

    //首先需要用户输入被修改用户的id,将该用户信息显示在屏幕上.

    //目前修改联系人只能全表修改,必须给定除id以外的所有值

    public void changePersonMessageById() throws Exception{

    System.out.println("请输入需要修改的联系人的编号(id):
");
 int id=sc.nextInt();

 //调用service的根据id查询联系人方法,获得联系人具体信息并且显示
 Person  p=service.getPersonById(id);
 System.out.println("您要修改的联系人具体信息如下:");
 System.out.println("Id\tName\t  Mobile  \t  Telphone  \t    Email    \t  City  \t  Birthday  \t  CategoryName ");

      System.out.println(p.getId()+"\t"+p.getName()+"\t"+p.getMobile()+"\t"+p.getTelphone()+"\t"+p.getEmail()+"\t"+p.getCity()+"\t"+p.getBirthday()+"\t"+p.getCategory().getName());

      //用户输入修改信息

      System.out.println("请输入联系人姓名:");

    String name=sc.next();

    System.out.println("请输入联系人手机号码:");

    String mobile=sc.next();

    System.out.println("请输入联系人座机号码:");

    String telphone=sc.next();

    System.out.println("请输入联系人email:");

    String email=sc.next();

    System.out.println("请输入联系人地址:");

    String city=sc.next();

    System.out.println("请输入生日(1980-6-23):");

    String date=sc.next();

    //调用本类的方法,显示所有类别信息

    showAllCategory();

    System.out.println("请选择联系人所属类别:");

    int  cid=sc.nextInt();

    

    p.setBirthday(date);

    p.setCity(city);

    p.setEmail(email);

    p.setTelphone(telphone);

    p.setMobile(mobile);

    p.setName(name);

    //调用service里的修改联系人的方法

    service.changePersonMessage(p, cid);

   

      System.out.println("-----------修改成功----------");

    }

   //修改类别名,不能和已有类别重名

    public void changeCategoryName() throws Exception {

    //调用本类的方法,显示所有类别信息

    showAllCategory();

    System.out.println("请选择需要修改的类别:");

    int  cid=sc.nextInt();
System.out.println("请输入修改后的类别名");
String  cname=sc.next();

//调用service方法修改类别名
Category c = new Category();
c.setId(cid);
c.setName(cname);
service.changeCategoryName(c);

}

    //删除类别,删除成功后,原有类别下所有联系人划分到默认类别
public void dropCategory() throws Exception {
//调用本类的方法,显示所有类别信息

    showAllCategory();

    System.out.println("请选择需要删除的类别:");

    int  cid=sc.nextInt();

    System.out.println("确定删除吗?删除后当前类别下的所有联系人统一划分到默认类别下.");

    System.out.println("请选择'y'or'n':");

        String str=sc.next();

        //如果选择n则放弃本次函数调用.

        if(str=="n") return ;

        //选择y继续执行

        service.dropCategory(cid);

        System.out.println("----------------删除成功---------------------");
}
//添加类别,类别名不能重复

    public void addCategory() throws Exception {

    //调用本类的方法,显示所有类别信息

    showAllCategory();

    System.out.println("请输入添加的类别名,注意不能与已有类别重名");
String  cname=sc.next();
service.addCategory(cname);
}

    //显示所有类别
public void showAllCategory() throws Exception {
//调用service的getAllCategory方法,显示所有的category,供用户选择

    List<Category>  cs=service.getAllCategory();

        //显示类别信息

    for(Category c: cs){
      System.out.print(c.getId()+"."+c.getName()+"\t");
   }
System.out.println();
}

  //按类别显示联系人

   public void showPersonsByCategory() throws Exception {
//调用本类的方法,显示所有类别信息

    showAllCategory();

    System.out.println("请输入查询的类别Id");
int cid=sc.nextInt();
List<Person> list=service.getPersonByCategoryId(cid);
this.showPersons(list);
}

}

——test测试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  通讯录 java