简体   繁体   中英

Error retrieving data from MySQL database in JDBCTemplate

I am trying to get data from mysql database.

My method for EmployeeJDBCTemplate is

public List<Employee> getListEmployees() {
    String sql = "select * from testemp";
    List<Employee> listEmp = jdbcTemplaeObject.query(sql, new RowMapper<Employee>() {

        @Override
        public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
            Employee emp = new Employee();

            emp.setSn(rs.getInt("sn"));
            emp.setID(rs.getInt("ID"));
            emp.setName(rs.getString("name"));
            emp.setCheckin(rs.getString("checkin"));
            emp.setCheckout(rs.getString("checkout"));
            emp.setBreakstart(rs.getString("breakstart"));
            emp.setBreakend(rs.getString("breakend"));

            return emp;
        }

    });

    return listEmp;
}

And this is the error I got after running the program:

Caused by: java.lang.NullPointerException
    at att.user.dao.EmployeeJDBCTemplate.getListEmployees(EmployeeJDBCTemplate.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:183)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:173)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    ... 77 more

Can anybody help me? Thank you in advance.

Looks like employeeJDBCTemplate object is not initialize - is null. Have you initialize it.

Please replace the below tag

<p:dataTable var="emp" value="#{employeeJDBCTemplate.getListEmployees()}">

with

<p:dataTable var="emp" value="#{employeeJDBCTemplate.listEmployees()}">

Also please check if you initialize the employeeJDBCTemplate. template Initialization in Spring XML configuration or using annotations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM