Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **********************************EMPLOYEE CLASS*************************************
- public class Employee {
- private int id;
- private String lastName;
- private String firstName;
- private int department;
- public Employee() {
- super();
- }
- public Employee(int id, String lastName, String firstName, int department) {
- super();
- this.id = id;
- this.lastName = lastName;
- this.firstName = firstName;
- this.department = department;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public int getDepartment() {
- return department;
- }
- public void setDepartment(int department) {
- this.department = department;
- }
- @Override
- public String toString() {
- return "Employee [id=" + id + ", lastName=" + lastName + ", firstName=" + firstName + ", department="
- + department + "]";
- }
- }
- ***************************EMPLOYEE DAO*********************************************
- public class EmployeeDao {
- // Logger
- private static final Logger logger = Logger.getLogger(EmployeeDao.class.getName());
- // Cache
- private static HashMap<Integer, Employee> cache = new HashMap<Integer, Employee>();
- public List<Employee> getAllEmployees() throws SQLException {
- List<Employee> list = new ArrayList<Employee>();
- PreparedStatement pstmt = JdbcUtils.getConnection()
- .prepareStatement("SELECT * FROM employees");
- try {
- ResultSet rs = pstmt.executeQuery();
- while(rs.next()) {
- Employee e = resultSetToEmployee (rs);
- list.add(e);
- }
- } catch (SQLException sqle) {
- logger.error("error executing: " + sqle);
- } finally {
- if(pstmt!= null)
- try {
- pstmt.close();
- } catch (SQLException e) {
- /*ignore it */
- }
- }
- return list;
- }
- public boolean insert (Employee emp) throws SQLException {
- if(emp == null) {
- return false;
- }
- PreparedStatement statement = JdbcUtils.getConnection()
- .prepareStatement("lNSERT INTO employees VALUES(?, ?, ?, ?)");
- statement.setInt(1, emp.getId());
- statement.setString(2, emp.getLastName());
- statement.setString(3, emp.getFirstName());
- statement.setInt(4, emp.getDepartment());
- int count = 0;
- try {
- count = statement.executeUpdate();
- } catch (SQLException sqle) {
- logger.error("error executing insert for employee: " + emp);
- } finally {
- statement.close();
- }
- return count > 0;
- }
- public boolean delete (int id) throws SQLException {
- PreparedStatement statement = JdbcUtils.getConnection().
- prepareStatement("DELETE FROM employee WHERE id = ?");
- statement.setInt(1, id);
- int count = 0;
- try {
- count = statement.executeUpdate();
- } catch (SQLException sqle) {
- logger.error("error executing delete for id: " + id);
- } finally {
- statement.close();
- }
- return count > 0;
- }
- private Employee resultSetToEmployee(ResultSet rs) throws SQLException {
- Employee employee = null;
- int id = rs.getInt("employee_id");
- if (cache.containsKey(id)) {
- employee = cache.get(id);
- } else {
- employee = new Employee();
- }
- employee.setId(rs.getInt("employee_id"));
- employee.setLastName(rs.getString("lastName"));
- employee.setFirstName(rs.getString("firstName"));
- employee.setDepartment(rs.getInt("department_id"));
- if(!cache.containsKey(id)) {
- cache.put(id, employee);
- }
- logger.info("get name for employee " +
- employee.getFirstName() + " " + employee.getLastName());
- return employee;
- }
- public static void main( String[] args ) throws IOException {
- // TESTING THE EMPLOYEEDAO
- logger.info("Testing EmployeeDao...");
- EmployeeDao employeeDao = new EmployeeDao();
- /** GET ALL EMPLOYEES TEST **/
- try {
- EmployeeDao empDao = new EmployeeDao();
- empDao.getAllEmployees();
- } catch (Exception e) {
- logger.error(e.getMessage());
- }
- /** INSERT EMPLOYEE TEST **/
- try {
- Employee employee = new Employee(123, "Smith", "John", 1);
- boolean success = employeeDao.insert(employee);
- if(success) {
- logger.info("SUCCESS: Employee inserted: " + employee.toString());
- }
- } catch (Exception e) {
- logger.error(e.getMessage());
- }
- /** DELETE EMPLOYEE TEST **/
- }
- }
- ********************************ERROR REPORTING STACKTRACE UNHELPFUL************************************
- 2019-11-08 19:56:12 INFO EmployeeDao:122 - Testing EmployeeDao...
- 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee Rob Smock
- 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee Bob Yellow
- 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee John Black
- 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee Thomas White
- 2019-11-08 19:56:15 INFO EmployeeDao:113 - get name for employee Derrick Brown
- 2019-11-08 19:56:15 ERROR EmployeeDao:64 - error executing insert for employee: Employee [id=123, lastName=Smith, firstName=John, department=1]
- [Ljava.lang.StackTraceElement;@53aad5d5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement