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 }