0

In this Code whenever invoke storeEmployeeRecord() it reads jdbcTemplate and then it perform further steps to store the data entered by user in form but problem is with getEmployeeById() whenever i invoke this method it returns me null. I also found one issue with jdbcTemplate is that my first method is able to read it but getEmployeeById() method is not reading jdbcTemplate and diplaying it as null

    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;

    import org.naman.commandbean.Employee;
    import org.naman.commandbean.SearchEmployeeResult;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.PreparedStatementSetter;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.stereotype.Repository;

    @Repository
    public class EmployeeDAO {
        @Autowired
        private JdbcTemplate jdbcTemplate;

        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }


        public int storeEmployeeRecord(final Employee employee) {
            System.out.println("In storeEmployeeRecord jdbcTemplate= " + jdbcTemplate);
            final String query = "insert into EMP_TBL values(?,?,?)";

            return jdbcTemplate.update(query, new PreparedStatementSetter() {

                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setInt(1, employee.getEno());
                    ps.setString(2, employee.getName());
                    ps.setDouble(3, employee.getSalary());
                }// end setValues
            });// end update

        }// end storeEmployeeRecord


        public List<SearchEmployeeResult> getEmployeeById(final Integer eno) {
            final String QUERY_TO_RETRIEVEBY_ENO = "select * from EMP_TBL where eno=?";
            System.out.println("In DAO_________jdbcTemplate= " + jdbcTemplate);
            return jdbcTemplate.query(QUERY_TO_RETRIEVEBY_ENO, new PreparedStatementSetter() {

                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setInt(1, eno);
                    System.out.println("PreparedStatement_______value set");
                }// end PreparedStatementSetter
            }, new RowMapper<SearchEmployeeResult>() {

                public SearchEmployeeResult mapRow(ResultSet rs, int rowNum) throws SQLException {

                    SearchEmployeeResult employeeResult = new SearchEmployeeResult();
                    employeeResult.setEno(rs.getInt(1));
                    employeeResult.setName(rs.getString(2));
                    employeeResult.setSalary(rs.getDouble(3));

                    System.out.println("All the values are in SearchEmployeeResult.....returning values");
                    return employeeResult;
                }// end mapRow
            }); // end RowMapper
        }

    }// end class 

[Error Console Preview:][1]

This is Bean Configuration file

@Configuration
@PropertySource("classpath:db.properties")
public class DbConfiguration {

    @Autowired
    private Environment environment;
    @Autowired
    private DataSource dataSource;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getProperty("db.DriverClassName"));
        dataSource.setUrl(environment.getProperty("db.Url"));
        dataSource.setUsername(environment.getProperty("db.Username"));
        dataSource.setPassword((environment.getProperty("db.Password")));
        System.out.println(environment.getProperty("db.Password"));
        return dataSource;
    }// end dataSource

    @Bean
    public JdbcTemplate setJdbcTemplate() {
        return new JdbcTemplate(dataSource);
    }// end JdbcTemplate
}// end class

This is Service Class

import java.util.List;

import org.naman.commandbean.Employee;
import org.naman.commandbean.SearchEmployeeResult;
import org.naman.dao.EmployeeDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeService {

    @Autowired
    private EmployeeDAO employeeDAO;

    public int save(Employee employee) {
        return employeeDAO.storeEmployeeRecord(employee);
    }// end save

    public List<SearchEmployeeResult> getEmployeeById(Integer eno) {
        return new EmployeeDAO().getEmployeeById(eno);
    }// end getEmployeeById
}// end class

This is Controller

import java.util.List;

import javax.validation.Valid;

import org.naman.commandbean.Employee;
import org.naman.commandbean.SearchEmployee;
import org.naman.commandbean.SearchEmployeeResult;
import org.naman.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyController {
    @Autowired
    private EmployeeService employeeService;
    @RequestMapping(path = "/store", method = RequestMethod.GET)
    public String displayForm(Employee employee, Model model) {

        model.addAttribute("employee", employee);
        return "employeeDataCapture";
    }

    @RequestMapping(path = "/store", method = RequestMethod.POST)
    public String processData(@Valid Employee employee, BindingResult errors, Model model) {
        if (errors.hasErrors()) {
            return "employeeDataCapture";
        } // end if
        int saveEmloyeeDetails = employeeService.save(employee);
        model.addAttribute("employeeData", saveEmloyeeDetails);
        return "employeeDataSuccess";
    }    
    @RequestMapping(path = "/search", method = RequestMethod.GET)
    public String displayAllEmployeesFrom(SearchEmployee searchEmployee, Model model) {
        model.addAttribute("searchEmployee", searchEmployee);
        return "search";
    }// end displayAllEmployeesFrom

    @RequestMapping(path = "/search", method = RequestMethod.POST)
    private String employeeSearchResult(@Valid SearchEmployee searchEmployee, BindingResult errors, Model model) {
        if (errors.hasErrors()) {
            return "search";
        } // end if

        List<SearchEmployeeResult> result = employeeService.getEmployeeById(searchEmployee.getEno());
        model.addAttribute("searchResult", result);
        return "searchResult";
    }
}//end class
Naman
  • 53
  • 1
  • 12

1 Answers1

0

Possibly 3 points to fix:

  1. Remove your @Autowired private DataSource dataSource; in your DbConfiguration, you already have an @Bean method where you configure your DataSource

  2. Remove the setJdbcTemplate method inside your DAO. You alread @Autowire it in the fields.

  3. Add the @ComponentScan(basePackages = "org.naman") to your DbConfiguration. This scans for components (every class containing @Repository and @Service etc and injects the dependencies to them.

Actual fix:

Turn your method into this:
public List<SearchEmployeeResult> getEmployeeById(Integer eno) { return employeeDAO.getEmployeeById(eno); }
You never create your Beans yourself. That is what Spring is for. Your jdbcTemplate isn't set by Spring in getEmployeeById, so it is null.

Yoshua Nahar
  • 1,304
  • 11
  • 28