Java实验项目六——使用DAO模式实现对职工表的操作
Program:
利用JDBC访问职工信息表,实现对职工信息的添加、更新、删除、按照职工号查找、查找全部职工的功能。
Description:在这里我采用了DAO设计模式完成对职工表的操作,下面介绍一下我的项目目录的组成(包.类)
com.vo.Worker:定义职工类,其中的Field名字和数据库中表的属性名字对应
com.db.DBConnection:实现控制数据库的连接和关闭,
com.dao.WorkerDao:定义接口,接口中定义与实际业务相对应的的数据库操作方法,具体操作由实现它的类完成
com.daoimpl.WorkerDaoImpl:数据层,实现WorkerDao接口,并覆写接口中的方法,实现对数据库的操作
com.daoimpl.WorkerService:业务逻辑层,实现WorkerDao接口,结合WorkerDaoImpl完成相应的业务操作
com.factory.Factory:定义工厂类,获得业务操作的对象,即WorkerService的实例化对象
main.TestDemo:测试类,测试业务逻辑操作
具体代码如下:
com.vo.Worker
1 /* 2 * Description:定义职工类 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-19 7 * 8 * */ 9 10 package com.vo; //vo即为(Value Object 值对象 ) 11 12 import java.util.Date; 13 14 public class Worker { 15 16 String workerId; //职工号 17 String workerName; //职工姓名 18 String workerSex; //职工性别 19 String workerPartment; //职工部门 20 Date workerBirthday; //职工出生日期 21 22 23 //定义构造方法 24 public Worker() { 25 26 } 27 28 public Worker(String id,String name,String sex,String partment,Date birthday) { 29 30 this.workerId = id; 31 this.workerName = name; 32 this.workerSex = sex; 33 this.workerPartment = partment; 34 this.workerBirthday = birthday; 35 } 36 37 38 //定义setter()和getter()方法 39 public String getWorkerId() { 40 return workerId; 41 } 42 43 public void setWorkerId(String workerId) { 44 this.workerId = workerId; 45 } 46 47 public String getWorkerName() { 48 return workerName; 49 } 50 51 public void setWorkerName(String workerName) { 52 this.workerName = workerName; 53 } 54 55 public String getWorkerSex() { 56 return workerSex; 57 } 58 59 public void setWorkerSex(String workerSex) { 60 this.workerSex = workerSex; 61 } 62 63 public String getWorkerPartment() { 64 return workerPartment; 65 } 66 67 public void setWorkerPartment(String workerPartment) { 68 this.workerPartment = workerPartment; 69 } 70 71 public Date getWorkerBirthday() { 72 return workerBirthday; 73 } 74 75 public void setWorkerBirthday(Date workerBirthday) { 76 this.workerBirthday = workerBirthday; 77 } 78 79 //覆写toString方法 80 @Override 81 public String toString() { 82 return "Worker [workerId=" + workerId + ", workerName=" + workerName 83 + ", workerSex=" + workerSex + ", workerPartment=" 84 + workerPartment + ", workerBirthday=" + workerBirthday 85 + ", workerBeginDate=" + "]"; 86 } 87 88 }
com.db.DBConnection
1 /* 2 * Description:定义数据库连接类。只负责数据库的连接和关闭 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-19 7 * 8 * */ 9 10 package com.db; 11 12 import java.sql.Connection; 13 import java.sql.DriverManager; 14 15 public class DBConnection { 16 17 //定义数据库驱动类 18 private static final String DBDRIVER = "com.mysql.jdbc.Driver"; 19 //定义数据库URL 20 private static final String DBURL = "jdbc:mysql://localhost:3306/Workers?characterEncoding=utf8&useSSL=true"; 21 //定义数据库连接用户名 22 private static final String DBUSER = "root"; 23 //定义数据库连接指令 24 private static final String DBPASS = "Cz123"; 25 26 //声明数据库连接对象 27 Connection con = null; 28 29 //定义构造方法,并实例化数据库连接对象 30 public DBConnection() throws Exception { 31 32 try { 33 34 Class.forName(DBDRIVER); 35 this.con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); 36 }catch(Exception e) { 37 38 throw e; 39 } 40 } 41 42 43 //取得数据库连接对象 44 public Connection getConnection() throws Exception { 45 46 return this.con; 47 } 48 49 //关闭数据库连接 50 public void close() throws Exception{ 51 52 if( this.con != null ) { 53 54 try { 55 56 con.close(); 57 }catch(Exception e) { 58 59 throw e; 60 } 61 } 62 } 63 64 }
com.dao.WorkerDao
1 /* 2 * Description:定义DAO接口,在该接口中只是定义对员工的操作, 3 * 具体操作需要实现该接口的类完成 4 * 5 * Written By:Cai 6 * 7 * Date Written:2017-10-19 8 * 9 * */ 10 11 12 package com.dao; 13 14 import java.util.List; 15 16 import com.vo.Worker; 17 18 public interface WorkerDao { 19 20 //向职工表中添加职工信息 21 public boolean add(Worker worker) throws Exception; 22 23 //根据职工号删除职工表中对应的职工 24 public boolean remove(String id) throws Exception; 25 26 //按照职工号查找职工 27 public Worker search(String id) throws Exception; 28 29 //查找全部职工 30 public List<Worker> getWorkers() throws Exception; 31 32 //更新职工信息 33 public boolean update(Worker worker) throws Exception; 34 35 }
com.daoimpl.WorkerDaoImpl
1 /* 2 * Description:实现DAO接口,完成具体数据库操作 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-19 7 * 8 * */ 9 10 package com.daoimpl; 11 12 import java.sql.Connection; 13 import java.sql.PreparedStatement; 14 import java.sql.ResultSet; 15 import java.util.ArrayList; 16 import java.util.List; 17 import java.util.Date; 18 import com.dao.WorkerDao; 19 import com.vo.Worker; 20 21 public class WorkerDaoImpl implements WorkerDao { 22 23 private Connection con = null; //定义数据库连接对象 24 private PreparedStatement state = null; //当以数据库操作对象 25 26 //定义构造方法,并实例化数据路连接对象 27 public WorkerDaoImpl(Connection con) { 28 29 this.con = con; 30 } 31 32 //覆写插入方法 33 @Override 34 public boolean add(Worker worker) throws Exception { 35 36 boolean flag = false; 37 //如果数据库中不存在相同id的员工,则可插入数据 38 if( worker != null ) { 39 40 //定义插入的sql语句 41 String insertSql = " insert into worker(workerId,workerName,workerSex,workerPartment,workerBirthday) " 42 + " values(?,?,?,?,?) "; 43 44 //转换日期类型:util.Date -> sql.Date 45 Date date = worker.getWorkerBirthday(); 46 java.sql.Date d = new java.sql.Date(date.getTime()); 47 48 //取得操作数据库的对象 49 this.state = this.con.prepareStatement(insertSql); 50 51 this.state.setString(1, worker.getWorkerId()); 52 this.state.setString(2, worker.getWorkerName()); 53 this.state.setString(3, worker.getWorkerSex()); 54 this.state.setString(4, worker.getWorkerPartment()); 55 this.state.setDate(5, d); 56 57 if( this.state.executeUpdate() > 0 ) { //成功插入数据 58 59 flag = true; 60 } 61 62 this.state.close(); //关闭数据库操作对象 63 } 64 65 return flag; //返回判断标志 66 } 67 68 //覆写删除方法 69 @Override 70 public boolean remove(String id) throws Exception { 71 72 boolean flag = false; //判断是否删除成功 73 74 //定义用于删除的sql语句 75 String removeSql = " delete from worker where workerId = ? "; 76 77 this.state = this.con.prepareStatement(removeSql); 78 this.state.setString(1, id); 79 80 if( this.state.executeUpdate() > 0 ) { //删除成功 81 82 flag = true; 83 } 84 85 this.state.close(); //关闭连接 86 87 return flag; 88 89 } 90 91 //覆写查询方法 92 @Override 93 public Worker search(String id) throws Exception { 94 95 Worker worker = null; //接受查询返回的对象 96 ResultSet rs = null; //接受查询结果 97 98 //id不为空,且不为"" 99 if( id != null && !"".equals(id) ) { 100 101 //定义用于查询的sql语句 102 String selectSql = "select workerId,workerName,workerSex,workerPartment,workerBirthday" 103 +" from worker where workerId=? "; 104 105 this.state = this.con.prepareStatement(selectSql); 106 this.state.setString(1, id); 107 rs = this.state.executeQuery(); 108 109 //查询成功 110 if( rs.next() ) { 111 112 worker = new Worker(); //实例化Worker类对象 113 worker.setWorkerId(rs.getString(1)); 114 worker.setWorkerName(rs.getString(2)); 115 worker.setWorkerSex(rs.getString(3)); 116 worker.setWorkerPartment(rs.getString(4)); 117 worker.setWorkerBirthday(new Date( rs.getDate(5).getTime() )); 118 } 119 120 this.state.close(); //关闭连接 121 } 122 123 return worker; 124 } 125 126 //覆写取得所有Worker类对象的方法 127 @Override 128 public List<Worker> getWorkers() throws Exception { 129 130 //保存所有职工对象 131 List<Worker> list = new ArrayList<Worker>(); 132 //保存返回的的查询结果 133 ResultSet rs = null; 134 //定义用于查询的sql语句 135 String selectSql = " select * from worker "; 136 137 this.state = this.con.prepareStatement(selectSql); 138 rs = this.state.executeQuery(); 139 140 141 while( rs.next() ) { 142 143 Worker worker = new Worker(); //实例化职工对象 144 145 worker.setWorkerId(rs.getString(1)); 146 worker.setWorkerName(rs.getString(2)); 147 worker.setWorkerSex(rs.getString(3)); 148 worker.setWorkerPartment(rs.getString(4)); 149 worker.setWorkerBirthday(new Date( rs.getDate(5).getTime() )); 150 151 list.add(worker); //加入集合 152 } 153 154 this.state.close(); //关闭连接 155 156 return list; //返回集合 157 158 } 159 160 //覆写更新员工信息方法 161 @Override 162 public boolean update(Worker worker) throws Exception { 163 164 boolean flag = false; //标记是否更新成功 165 166 if( worker != null ) { 167 168 //定义更新语句 169 String updateSql = " update worker set workerName = ?,workerSex = ?,workerPartment = ?, " 170 + " workerBirthday = ? where workerId = ?"; 171 172 //转换日期类型:util.Date -> sql.Date 173 Date date = worker.getWorkerBirthday(); 174 java.sql.Date d = new java.sql.Date(date.getTime()); 175 176 this.state = this.con.prepareStatement(updateSql); 177 this.state.setString(1, worker.getWorkerName()); 178 this.state.setString(2, worker.getWorkerSex()); 179 this.state.setString(3, worker.getWorkerPartment()); 180 this.state.setDate(4, d); 181 this.state.setString(5, worker.getWorkerId()); 182 183 if( this.state.executeUpdate() > 0 ) { //更新成功 184 185 flag = true; 186 } 187 188 this.state.close(); //关闭连接 189 } 190 191 return flag; 192 } 193 194 195 }
com.daoimpl.WorkerService
1 /* 2 * Description:实现具体的业务逻辑 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-19 7 * 8 * */ 9 10 package com.daoimpl; 11 12 import java.util.List; 13 14 import com.dao.WorkerDao; 15 import com.db.DBConnection; 16 import com.vo.Worker; 17 18 public class WorkerService implements WorkerDao { 19 20 DBConnection dbc = null; //声明负责数据库连接关闭对象 21 WorkerDao workerDao = null; //声明业务逻辑操作接口对象 22 23 //定义构造方法,并实例化属性 24 public WorkerService() throws Exception { 25 26 this.dbc = new DBConnection(); //实例化数据库连接和关闭对象 27 this.workerDao = new WorkerDaoImpl(this.dbc.getConnection()); //实例化数据库操作对象 28 } 29 30 //添加职工信息 31 public boolean add(Worker worker) throws Exception { 32 33 boolean flag = false; 34 35 try { 36 if( this.workerDao.search(worker.getWorkerId()) == null ) { 37 38 flag = this.workerDao.add(worker); //调用WorkerDaoImpl类中add()方法 39 } 40 41 }catch(Exception e) { 42 43 throw e; 44 }finally { 45 46 this.dbc.close(); //关闭数据库连接 47 } 48 49 return flag; 50 } 51 52 //删除职工信息 53 public boolean remove(String workerId) throws Exception { 54 55 boolean flag = false; 56 57 try { 58 if( this.workerDao.search(workerId) != null ) { 59 60 flag = this.workerDao.remove(workerId); //调用WorkerDaoImpl类中的remove()方法 61 } 62 }catch(Exception e) { 63 64 throw e; 65 }finally { 66 67 this.dbc.close(); //关闭数据库连接 68 } 69 70 return flag; 71 } 72 73 //查询职工信息 74 public Worker search(String workerId) throws Exception { 75 76 Worker worker = null; 77 78 try { 79 80 worker = this.workerDao.search(workerId); //调用WorkerDaoImpl类中的search()方法 81 }catch(Exception e) { 82 83 throw e; 84 }finally { 85 86 this.dbc.close(); //关闭数据库连接 87 } 88 89 return worker; 90 } 91 92 //取得所有职工信息 93 94 public List<Worker> getWorkers() throws Exception { 95 96 List<Worker> list = null; 97 98 try { 99 100 list = this.workerDao.getWorkers(); //调用WorkerDaoImpl类中的getWorkers()方法 101 }catch(Exception e) { 102 103 throw e; 104 }finally { 105 106 this.dbc.close(); //关闭数据库连接 107 } 108 109 return list; 110 } 111 112 //更新员工信息 113 public boolean update(Worker worker) throws Exception { 114 115 boolean flag = false; 116 117 try { 118 if( this.workerDao.search(worker.getWorkerId()) != null ) { 119 120 flag = this.workerDao.update(worker); //调用WorkerDaoImpl类中的update()方法 121 } 122 }catch(Exception e) { 123 124 throw e; 125 }finally { 126 127 this.dbc.close(); //关闭数据库连接 128 } 129 130 return flag; 131 } 132 133 }
com.factory.Factory
1 /* 2 * Description:定义工厂类,获得WorkerService类的实例 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-19 7 * 8 * */ 9 10 package com.factory; 11 12 import com.dao.WorkerDao; 13 import com.daoimpl.WorkerService; 14 15 public class Factory { 16 17 //定义静态方法,返回实例化的WorkerService类对象 18 /* 19 * 通过工厂类的静态方法,获得业务处理类实例化对象,可以让我们不用在表现层再去new对象, 20 * 并且当我们的业务操作层需要换一种方法实现时,同时又想保留原来的业务层的实现,这样我们直接可以 21 * 修改静态方法中的代码,不用去表现层中再一个一个的修改new对象的类型 22 * 23 * */ 24 public static WorkerDao getWorkerServiceInstance() throws Exception { 25 26 return new WorkerService(); 27 } 28 29 }
main.TestDemo
1 /* 2 * Description:定义测试类,测试Dao模式下,对数据库中职工表的一系列操作 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-19 7 * 8 * */ 9 10 package main; 11 12 import java.util.Date; 13 import java.util.Iterator; 14 import java.util.List; 15 16 import com.factory.Factory; 17 import com.vo.Worker; 18 19 public class TestDemo { 20 21 public static void main(String args[]) { 22 23 List<Worker> list = null; 24 25 try { 26 27 //为了演示简单。所有的日期都直接new了 28 Factory.getWorkerServiceInstance().add(new Worker("0001","大象","male","办公室",new Date())); 29 Factory.getWorkerServiceInstance().add(new Worker("0002","狮子","male","办公室",new Date())); 30 Factory.getWorkerServiceInstance().add(new Worker("0003","老虎","male","办公室",new Date())); 31 Factory.getWorkerServiceInstance().add(new Worker("0004","蚂蚁","male","办公室",new Date())); 32 33 //演示插入相同id的职工信息,此处打印false,(插入失败) 34 System.out.println(Factory.getWorkerServiceInstance().add(new Worker("0004","蚂蚁","male","办公室",new Date()))); 35 //根据id找到对应员工,此处打印对应id的员工信息 36 System.out.println(Factory.getWorkerServiceInstance().search("0004") ); 37 //删除指定id编号的员工信息,此处打印true(删除成功) 38 System.out.println(Factory.getWorkerServiceInstance().remove("0004") ); 39 //删除指定不存在的id编号的员工信息,此处打印false(删除失败) 40 System.out.println( Factory.getWorkerServiceInstance().remove("0004") ); 41 //更新员工信息,将办公室改成厕所(恶搞。。。),此处打印true(更新成功) 42 System.out.println(Factory.getWorkerServiceInstance().update(new Worker("0003","老虎","male","办公室",new Date())) ); 43 //获取所有职工的信息 44 list = Factory.getWorkerServiceInstance().getWorkers(); 45 46 }catch(Exception e) { 47 48 e.printStackTrace(); 49 } 50 51 //迭代输出职工信息 52 Iterator<Worker> ite = list.iterator(); 53 while( ite.hasNext() ) { 54 55 System.out.println( ite.next() ); 56 } 57 58 59 } 60 61 }
初学小白,请多指教!