1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| package com.powernode.jdbc.dao;
import com.powernode.jdbc.beans.Employee; import com.powernode.jdbc.utils.DbUtils;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
public class EmployeeDao {
public int insert(Employee employee) { Connection conn = null; PreparedStatement ps = null; int count = 0; try { conn = DbUtils.getConnection(); String sql = "insert into t_employee(name,job,salary,hiredate,address) values(?,?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, employee.getName()); ps.setString(2, employee.getJob()); ps.setDouble(3, employee.getSalary()); ps.setString(4, employee.getHiredate()); ps.setString(5, employee.getAddress()); count = ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { DbUtils.close(conn, ps, null); } return count; }
public int update(Employee employee){ Connection conn = null; PreparedStatement ps = null; int count = 0; try { conn = DbUtils.getConnection(); String sql = "update t_employee set name=?, job=?, salary=?, hiredate=?, address=? where id=?"; ps = conn.prepareStatement(sql); ps.setString(1, employee.getName()); ps.setString(2, employee.getJob()); ps.setDouble(3, employee.getSalary()); ps.setString(4, employee.getHiredate()); ps.setString(5, employee.getAddress()); ps.setLong(6, employee.getId()); count = ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { DbUtils.close(conn, ps, null); } return count; }
public int deleteById(Long id){ Connection conn = null; PreparedStatement ps = null; int count = 0; try { conn = DbUtils.getConnection(); String sql = "delete from t_employee where id = ?"; ps = conn.prepareStatement(sql); ps.setLong(1, id); count = ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { DbUtils.close(conn, ps, null); } return count; }
public Employee selectById(Long id){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; Employee employee = null; try { conn = DbUtils.getConnection(); String sql = "select * from t_employee where id = ?"; ps = conn.prepareStatement(sql); ps.setLong(1, id); rs = ps.executeQuery(); if(rs.next()){ employee = new Employee(); employee.setId(id); employee.setName(rs.getString("name")); employee.setJob(rs.getString("job")); employee.setSalary(rs.getDouble("salary")); employee.setHiredate(rs.getString("hiredate")); employee.setAddress(rs.getString("address")); } } catch (SQLException e) { throw new RuntimeException(e); } finally { DbUtils.close(conn, ps, rs); } return employee; }
public List<Employee> selectAll(){ List<Employee> employees = new ArrayList<>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DbUtils.getConnection(); String sql = "select * from t_employee"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ Employee employee = new Employee(); employee.setId(rs.getLong("id")); employee.setName(rs.getString("name")); employee.setJob(rs.getString("job")); employee.setSalary(rs.getDouble("salary")); employee.setHiredate(rs.getString("hiredate")); employee.setAddress(rs.getString("address")); employees.add(employee); } } catch (SQLException e) { throw new RuntimeException(e); } finally { DbUtils.close(conn, ps, rs); } return employees; } }
|