Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. package practice4;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import practice3.Employee;
  12.  
  13. public class EmployeeDAOJDBC implements EmployeeDAO {
  14.  
  15. private String url = "jdbc:derby://localhost:1527/EmployeeDB";
  16. private String user = "test";
  17. private String pass = "tiger";
  18. private Connection con;
  19.  
  20. // 建構子
  21. public EmployeeDAOJDBC() throws EmployeeDAOException {
  22. try {
  23. // 員工系統需要長時間執行,需要持續連線,所以目前沒處理 close()
  24. con = DriverManager.getConnection(url, user, pass);
  25. } catch (SQLException ex) {
  26. // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
  27. throw new EmployeeDAOException("EmployeeDAO 物件建構失敗", ex);
  28. }
  29. }
  30.  
  31. @Override
  32. public void insert(Employee e) throws EmployeeDAOException {
  33. String query = "insert into employee values(?,?,?,?,?)"; // 使用問號代表參數,之後設定
  34. int count = 0; // 有幾筆資料完成新增
  35. try {
  36.  
  37. PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
  38. // 設定各項 ? 參數值
  39. pstmt.setInt(1, e.getId()); // 設定第1個 ? 參數
  40. pstmt.setString(2, e.getFirstName()); // 設定第2個 ? 參數
  41. pstmt.setString(3, e.getLastName()); // 設定第3個 ? 參數
  42.  
  43. // 資料庫所使用的日期是 java.sql.Date , 所以必須先將 java.util.Date 轉成 java.sql.Date
  44. Date date = e.getBirthDate();
  45. long time = date.getTime();
  46. java.sql.Date sqlDate = new java.sql.Date(time); // 產生 java.sql.Date 物件
  47.  
  48. pstmt.setDate(4, sqlDate); // 設定第4個 ? 參數
  49. pstmt.setFloat(5, e.getSalary()); // 設定第5個 ? 參數
  50.  
  51. // executeUpdate() // 回傳有幾筆資料完成更新
  52. count = pstmt.executeUpdate();
  53.  
  54. } catch (SQLException ex) {
  55. // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
  56. throw new EmployeeDAOException("EmployeeDAO 物件建構失敗", ex);
  57. }
  58. if (count > 0) {
  59. //System.out.println(e.getFirstName() + " 新增 成功");
  60. } else {
  61. //System.out.println(e.getFirstName() + " 新增 失敗");
  62. throw new EmployeeDAOException("EmployeeDAO 新增失敗");
  63. }
  64. }
  65.  
  66. @Override
  67. public Employee find(int id) throws EmployeeDAOException {
  68. String query = "select * from employee where id=" + id;
  69.  
  70. return null;
  71. }
  72.  
  73. @Override
  74. public void update(Employee e) throws EmployeeDAOException {
  75. String query = "update employee set firstname=? , salary=? where id=?"; // 使用問號代表參數,之後設定
  76. // 修改 firstname 與 salary
  77. e.setFirstName("大明");
  78. e.setSalary(100_000);
  79.  
  80. int count = 0; // 有幾筆資料完成修改
  81. try {
  82. PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
  83.  
  84. // 設定各項 ? 參數值
  85. pstmt.setString(1, e.getFirstName()); // 設定第1個 ? 參數
  86. pstmt.setFloat(2, e.getSalary()); // 設定第2個 ? 參數
  87. pstmt.setInt(3, e.getId()); // 設定第3個 ? 參數
  88.  
  89. // executeUpdate() // 回傳有幾筆資料完成更新
  90. count = pstmt.executeUpdate();
  91.  
  92. } catch (SQLException ex) {
  93. //System.err.println(ex);
  94. // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
  95. throw new EmployeeDAOException("EmployeeDAO 修改失敗", ex);
  96. }
  97. if (count > 0) {
  98. //System.out.println("修改 成功");
  99. //System.out.println(e);
  100. } else {
  101. //System.out.println("修改 失敗");
  102. throw new EmployeeDAOException("EmployeeDAO 修改失敗");
  103.  
  104. }
  105. }
  106.  
  107. @Override
  108. public void delete(int id) throws EmployeeDAOException {
  109. // 取得 Employee 物件
  110. Employee e = null;
  111. try {
  112. e = find(id);
  113. } catch (EmployeeDAOException ex) {
  114. // 將找不到員工 id 的問題包裹在新的 EmployeeDAOException 裡
  115. throw new EmployeeDAOException("EmployeeDAO 刪除失敗", ex);
  116. }
  117. String query = "delete from employee where id=?"; // 使用問號代表參數,之後設定
  118. int count = 0; // 有幾筆資料完成修改
  119. try {
  120. PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
  121. // 設定 ? 參數值
  122. pstmt.setInt(1, e.getId()); // 設定第1個 ? 參數
  123.  
  124. // executeUpdate() // 回傳有幾筆資料完成更新
  125. count = pstmt.executeUpdate();
  126.  
  127. } catch (SQLException ex) {
  128. //System.err.println(ex);
  129. // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
  130. throw new EmployeeDAOException("EmployeeDAO 刪除失敗", ex);
  131. }
  132. System.out.println(e);
  133. if (count > 0) {
  134. //System.out.println("刪除 成功");
  135. } else {
  136. //System.out.println("刪除 失敗");
  137. throw new EmployeeDAOException("EmployeeDAO 刪除失敗");
  138. }
  139. }
  140.  
  141. @Override
  142. public Employee[] listAll() throws EmployeeDAOException {
  143. // 員工陣列
  144. ArrayList<Employee> employees = new ArrayList<>();
  145. try {
  146. Statement stmt = con.createStatement();
  147. String query = "select * from employee"; // 查詢所有員工
  148. ResultSet rs = stmt.executeQuery(query);
  149. while (rs.next()) { // 取得下一筆資料,若有獲取到資料回傳 true
  150. // 讀取目前這筆資料的各項欄位
  151. int id = rs.getInt("id");
  152. String firstName = rs.getString("firstname");
  153. String lastName = rs.getString("lastname");
  154. java.util.Date birthdate = rs.getDate("birthdate");
  155. float salary = rs.getFloat("salary");
  156. // 將員工物件加入陣列
  157. employees.add(new Employee(id, firstName, lastName, birthdate, salary));
  158. }
  159. } catch (SQLException ex) {
  160. //System.err.println(ex); // err 物件輸出的訊息是紅色的
  161. // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
  162. throw new EmployeeDAOException("EmployeeDAO 查詢全部失敗", ex);
  163. }
  164. // 回傳員工陣列
  165. return employees.toArray(new Employee[0]); // ArrayList<Employee> 轉 Employee[]
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement