Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package practice4;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Date;
- import practice3.Employee;
- public class EmployeeDAOJDBC implements EmployeeDAO {
- private String url = "jdbc:derby://localhost:1527/EmployeeDB";
- private String user = "test";
- private String pass = "tiger";
- private Connection con;
- // 建構子
- public EmployeeDAOJDBC() throws EmployeeDAOException {
- try {
- // 員工系統需要長時間執行,需要持續連線,所以目前沒處理 close()
- con = DriverManager.getConnection(url, user, pass);
- } catch (SQLException ex) {
- // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
- throw new EmployeeDAOException("EmployeeDAO 物件建構失敗", ex);
- }
- }
- @Override
- public void insert(Employee e) throws EmployeeDAOException {
- String query = "insert into employee values(?,?,?,?,?)"; // 使用問號代表參數,之後設定
- int count = 0; // 有幾筆資料完成新增
- try {
- PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
- // 設定各項 ? 參數值
- pstmt.setInt(1, e.getId()); // 設定第1個 ? 參數
- pstmt.setString(2, e.getFirstName()); // 設定第2個 ? 參數
- pstmt.setString(3, e.getLastName()); // 設定第3個 ? 參數
- // 資料庫所使用的日期是 java.sql.Date , 所以必須先將 java.util.Date 轉成 java.sql.Date
- Date date = e.getBirthDate();
- long time = date.getTime();
- java.sql.Date sqlDate = new java.sql.Date(time); // 產生 java.sql.Date 物件
- pstmt.setDate(4, sqlDate); // 設定第4個 ? 參數
- pstmt.setFloat(5, e.getSalary()); // 設定第5個 ? 參數
- // executeUpdate() // 回傳有幾筆資料完成更新
- count = pstmt.executeUpdate();
- } catch (SQLException ex) {
- // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
- throw new EmployeeDAOException("EmployeeDAO 物件建構失敗", ex);
- }
- if (count > 0) {
- //System.out.println(e.getFirstName() + " 新增 成功");
- } else {
- //System.out.println(e.getFirstName() + " 新增 失敗");
- throw new EmployeeDAOException("EmployeeDAO 新增失敗");
- }
- }
- @Override
- public Employee find(int id) throws EmployeeDAOException {
- String query = "select * from employee where id=" + id;
- return null;
- }
- @Override
- public void update(Employee e) throws EmployeeDAOException {
- String query = "update employee set firstname=? , salary=? where id=?"; // 使用問號代表參數,之後設定
- // 修改 firstname 與 salary
- e.setFirstName("大明");
- e.setSalary(100_000);
- int count = 0; // 有幾筆資料完成修改
- try {
- PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
- // 設定各項 ? 參數值
- pstmt.setString(1, e.getFirstName()); // 設定第1個 ? 參數
- pstmt.setFloat(2, e.getSalary()); // 設定第2個 ? 參數
- pstmt.setInt(3, e.getId()); // 設定第3個 ? 參數
- // executeUpdate() // 回傳有幾筆資料完成更新
- count = pstmt.executeUpdate();
- } catch (SQLException ex) {
- //System.err.println(ex);
- // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
- throw new EmployeeDAOException("EmployeeDAO 修改失敗", ex);
- }
- if (count > 0) {
- //System.out.println("修改 成功");
- //System.out.println(e);
- } else {
- //System.out.println("修改 失敗");
- throw new EmployeeDAOException("EmployeeDAO 修改失敗");
- }
- }
- @Override
- public void delete(int id) throws EmployeeDAOException {
- // 取得 Employee 物件
- Employee e = null;
- try {
- e = find(id);
- } catch (EmployeeDAOException ex) {
- // 將找不到員工 id 的問題包裹在新的 EmployeeDAOException 裡
- throw new EmployeeDAOException("EmployeeDAO 刪除失敗", ex);
- }
- String query = "delete from employee where id=?"; // 使用問號代表參數,之後設定
- int count = 0; // 有幾筆資料完成修改
- try {
- PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
- // 設定 ? 參數值
- pstmt.setInt(1, e.getId()); // 設定第1個 ? 參數
- // executeUpdate() // 回傳有幾筆資料完成更新
- count = pstmt.executeUpdate();
- } catch (SQLException ex) {
- //System.err.println(ex);
- // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
- throw new EmployeeDAOException("EmployeeDAO 刪除失敗", ex);
- }
- System.out.println(e);
- if (count > 0) {
- //System.out.println("刪除 成功");
- } else {
- //System.out.println("刪除 失敗");
- throw new EmployeeDAOException("EmployeeDAO 刪除失敗");
- }
- }
- @Override
- public Employee[] listAll() throws EmployeeDAOException {
- // 員工陣列
- ArrayList<Employee> employees = new ArrayList<>();
- try {
- Statement stmt = con.createStatement();
- String query = "select * from employee"; // 查詢所有員工
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()) { // 取得下一筆資料,若有獲取到資料回傳 true
- // 讀取目前這筆資料的各項欄位
- int id = rs.getInt("id");
- String firstName = rs.getString("firstname");
- String lastName = rs.getString("lastname");
- java.util.Date birthdate = rs.getDate("birthdate");
- float salary = rs.getFloat("salary");
- // 將員工物件加入陣列
- employees.add(new Employee(id, firstName, lastName, birthdate, salary));
- }
- } catch (SQLException ex) {
- //System.err.println(ex); // err 物件輸出的訊息是紅色的
- // 拋出例外,並將原本 SQLException 包裹在其中 (說明是 SQLException 導致的)
- throw new EmployeeDAOException("EmployeeDAO 查詢全部失敗", ex);
- }
- // 回傳員工陣列
- return employees.toArray(new Employee[0]); // ArrayList<Employee> 轉 Employee[]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement