Advertisement
Guest User

AnotherTry

a guest
Nov 8th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. **********************************EMPLOYEE CLASS*************************************
  2. public class Employee {
  3.  
  4. private int id;
  5. private String lastName;
  6. private String firstName;
  7. private int department;
  8.  
  9. public Employee() {
  10. super();
  11. }
  12.  
  13. public Employee(int id, String lastName, String firstName, int department) {
  14. super();
  15. this.id = id;
  16. this.lastName = lastName;
  17. this.firstName = firstName;
  18. this.department = department;
  19. }
  20.  
  21. public int getId() {
  22. return id;
  23. }
  24.  
  25. public void setId(int id) {
  26. this.id = id;
  27. }
  28.  
  29. public String getLastName() {
  30. return lastName;
  31. }
  32.  
  33. public void setLastName(String lastName) {
  34. this.lastName = lastName;
  35. }
  36.  
  37. public String getFirstName() {
  38. return firstName;
  39. }
  40.  
  41. public void setFirstName(String firstName) {
  42. this.firstName = firstName;
  43. }
  44.  
  45. public int getDepartment() {
  46. return department;
  47. }
  48.  
  49. public void setDepartment(int department) {
  50.  
  51. this.department = department;
  52. }
  53.  
  54. @Override
  55. public String toString() {
  56. return "Employee [id=" + id + ", lastName=" + lastName + ", firstName=" + firstName + ", department="
  57. + department + "]";
  58. }
  59. }
  60.  
  61. ***************************EMPLOYEE DAO*********************************************
  62. public class EmployeeDao {
  63.  
  64. // Logger
  65. private static final Logger logger = Logger.getLogger(EmployeeDao.class.getName());
  66. // Cache
  67. private static HashMap<Integer, Employee> cache = new HashMap<Integer, Employee>();
  68.  
  69. public List<Employee> getAllEmployees() throws SQLException {
  70.  
  71. List<Employee> list = new ArrayList<Employee>();
  72. PreparedStatement pstmt = JdbcUtils.getConnection()
  73. .prepareStatement("SELECT * FROM employees");
  74. try {
  75. ResultSet rs = pstmt.executeQuery();
  76. while(rs.next()) {
  77. Employee e = resultSetToEmployee (rs);
  78. list.add(e);
  79. }
  80. } catch (SQLException sqle) {
  81. logger.error("error executing: " + sqle);
  82. } finally {
  83. if(pstmt!= null)
  84. try {
  85. pstmt.close();
  86. } catch (SQLException e) {
  87. /*ignore it */
  88. }
  89. }
  90. return list;
  91. }
  92.  
  93. public boolean insert (Employee emp) throws SQLException {
  94.  
  95. if(emp == null) {
  96. return false;
  97. }
  98. PreparedStatement statement = JdbcUtils.getConnection()
  99. .prepareStatement("lNSERT INTO employees VALUES(?, ?, ?, ?)");
  100.  
  101. statement.setInt(1, emp.getId());
  102. statement.setString(2, emp.getLastName());
  103. statement.setString(3, emp.getFirstName());
  104. statement.setInt(4, emp.getDepartment());
  105.  
  106. int count = 0;
  107. try {
  108. count = statement.executeUpdate();
  109. } catch (SQLException sqle) {
  110. logger.error("error executing insert for employee: " + emp);
  111. } finally {
  112. statement.close();
  113. }
  114. return count > 0;
  115.  
  116. }
  117.  
  118. public boolean delete (int id) throws SQLException {
  119.  
  120. PreparedStatement statement = JdbcUtils.getConnection().
  121. prepareStatement("DELETE FROM employee WHERE id = ?");
  122. statement.setInt(1, id);
  123.  
  124. int count = 0;
  125. try {
  126. count = statement.executeUpdate();
  127. } catch (SQLException sqle) {
  128. logger.error("error executing delete for id: " + id);
  129. } finally {
  130. statement.close();
  131. }
  132.  
  133. return count > 0;
  134. }
  135.  
  136. private Employee resultSetToEmployee(ResultSet rs) throws SQLException {
  137. Employee employee = null;
  138.  
  139. int id = rs.getInt("employee_id");
  140.  
  141. if (cache.containsKey(id)) {
  142. employee = cache.get(id);
  143. } else {
  144. employee = new Employee();
  145. }
  146.  
  147. employee.setId(rs.getInt("employee_id"));
  148. employee.setLastName(rs.getString("lastName"));
  149. employee.setFirstName(rs.getString("firstName"));
  150. employee.setDepartment(rs.getInt("department_id"));
  151.  
  152. if(!cache.containsKey(id)) {
  153. cache.put(id, employee);
  154. }
  155.  
  156. logger.info("get name for employee " +
  157. employee.getFirstName() + " " + employee.getLastName());
  158.  
  159. return employee;
  160. }
  161.  
  162. public static void main( String[] args ) throws IOException {
  163.  
  164. // TESTING THE EMPLOYEEDAO
  165. logger.info("Testing EmployeeDao...");
  166. EmployeeDao employeeDao = new EmployeeDao();
  167.  
  168. /** GET ALL EMPLOYEES TEST **/
  169. try {
  170. EmployeeDao empDao = new EmployeeDao();
  171. empDao.getAllEmployees();
  172. } catch (Exception e) {
  173. logger.error(e.getMessage());
  174. }
  175.  
  176. /** INSERT EMPLOYEE TEST **/
  177. try {
  178. Employee employee = new Employee(123, "Smith", "John", 1);
  179. boolean success = employeeDao.insert(employee);
  180. if(success) {
  181. logger.info("SUCCESS: Employee inserted: " + employee.toString());
  182. }
  183. } catch (Exception e) {
  184. logger.error(e.getMessage());
  185. }
  186.  
  187. /** DELETE EMPLOYEE TEST **/
  188. }
  189. }
  190.  
  191. ********************************ERROR REPORTING STACKTRACE UNHELPFUL************************************
  192. 2019-11-08 19:56:12 INFO EmployeeDao:122 - Testing EmployeeDao...
  193. 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee Rob Smock
  194. 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee Bob Yellow
  195. 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee John Black
  196. 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee Thomas White
  197. 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee Derrick Brown
  198. 2019-11-08 19:56:15 ERROR EmployeeDao:64 - error executing insert for employee: Employee [id=123, lastName=Smith, firstName=John, department=1]
  199. [Ljava.lang.StackTraceElement;@53aad5d5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement