BadSqlGrammarException:由以下引起:java.sql.SQLException:列名无效

时间:2017-07-28 11:45:12

标签: java spring-mvc

大家好,我在运行代码时遇到上述异常。

这是我的班级。

    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 + ", ]";
    }

}

我的impl文件是

@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;
        }

    }

尝试执行代码时出现错误,如“ERROR GlobalExceptionHandler.defaultErrorHandler -1 - [URL]:http://localhost:8082/internalhost/userSearch/107 org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback;错误的SQL语法[select * from dept];嵌套异常是java.sql.SQLException:无效的列名“

这是我的数据库表 image

1 个答案:

答案 0 :(得分:0)

错误表示列名无效。您能否验证数据库表列是否与RowMapper结果集getter完全匹配。