private void update(Employee employee) { String query = "update Employee set name=?, role=? where id=?"; log.info("Executing query :: " + query); Object[] args = new Object[] {employee.getName(), employee.getRole(), employee.getId()}; int out = jdbcTemplate.update(query, args); if (out != 0) { log.info("Employee updated with id=" + employee.getId()); } else log.info("No Employee found with id=" + employee.getId()); }
private void save(Employee employee) { String query = "insert into Employee (name, role) values (?,?)"; log.info("Executing query :: " + query); Object[] args = new Object[] {employee.getName(), employee.getRole()}; int out = jdbcTemplate.update(query, args); if (out != 0) { log.info("Employee saved with id=" + employee.getId()); } else log.info("Employee save failed with id=" + employee.getId()); }
public List<Employee> getAll() { String query = "select id, name, role from Employee"; log.info("Executing query :: " + query); List<Employee> empList = new ArrayList<Employee>(); List<Map<String, Object>> empRows = jdbcTemplate.queryForList(query); for (Map<String, Object> empRow : empRows) { Employee emp = new Employee(); emp.setId(Integer.parseInt(String.valueOf(empRow.get("id")))); emp.setName(String.valueOf(empRow.get("name"))); emp.setRole(String.valueOf(empRow.get("role"))); empList.add(emp); } if (empList.isEmpty()) { log.info("No record to fetch"); } return empList; }
// For add and update person both @RequestMapping(value = "/employee/add", method = RequestMethod.POST) public String addPerson(@ModelAttribute("employee") Employee p) { if (p.getId() == 0) { // new person, add it save(p); } else { // existing person, call update update(p); } return "redirect:/home"; }