Shanmugapriya
Shanmugapriya

Reputation: 57

BadSqlGrammarException:Caused by: java.sql.SQLException: Invalid column name

Hi all i am getting the above exception when running my code.

Here is my class.

    package com.bct.internal.form.model;

    public class Dept {
    public String deptno;
    public String deptname;
    public String location;

    public String getDeptno() {
        return deptno;
    }

    public void setDeptno(String deptno) {
        this.deptno = deptno;
    }

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Dept [DEPTNO=" + deptno + ", DNAME=" + deptname + ", LOC=" + location + ", ]";
    }

}

and my impl file is

@Override
    public Map<String, String> practicelist() {
        Map<String, String> map = new HashMap<String, String>();
        List<Dept> lang1 = namedParameterJdbcTemplate.query("select * from dept", new DeptMapper());
        for (int i = 0; i < lang1.size(); i++) {
            map.put(lang1.get(i).getDeptno(), lang1.get(i).getDeptname());
        }
        return map;
    }

    public static final class DeptMapper implements RowMapper<Dept> {

        @Override
        public Dept mapRow(ResultSet rs, int rowNum) throws SQLException {
            Map<String, String> map = new HashMap<String, String>();
            Dept dept = new Dept();
            dept.setDeptname(rs.getString("deptname"));
            dept.setDeptno(rs.getString("deptno"));
            dept.setLocation(rs.getString("location"));
            return dept;
        }

    }

when trying to execute the code i am getting an error like "ERROR GlobalExceptionHandler.defaultErrorHandler -1 - [URL] : http://localhost:8082/internalhost/userSearch/107 org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select * from dept]; nested exception is java.sql.SQLException: Invalid column name"

Here is my database table image

Upvotes: 2

Views: 10015

Answers (1)

Tamiliniyan Devarajan
Tamiliniyan Devarajan

Reputation: 86

Error says invalid column name. Can you validate your database table columns are exactly matching with RowMapper resultset getters.

Upvotes: 4

Related Questions