【问题标题】:BadSqlGrammarException in Spring jdbc templateSpring jdbc 模板中的 BadSqlGrammarException
【发布时间】:2019-01-25 21:10:14
【问题描述】:

我正在使用 Jdbctemplate 从 db 中检索单个记录。方法如下。

public Advocate queryPrimaryDetails(String regCaseType, String regNo, String regYear) throws SQLException {
        String sql = "select pet_adv,pet_adv_cd from civil_t where regcase_type=? and reg_no=? and reg_year=?";
        Advocate list = (Advocate) getJdbcTemplate().queryForObject(sql, new Object[]{Integer.valueOf(regCaseType),Integer.valueOf(regNo),Integer.valueOf(regYear)},new RowMapper<Object>() {
            public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {
                return resultSet.next() ? new Advocate(resultSet.getInt("pet_adv_cd"), resultSet.getString("pet_adv"))
                        : null;
            }
        });
        System.out.println(list);
        return list;
    }

在我的情况下,我的查询完全有可能没有得到点击,所以我的问题是如何绕过以下错误消息。

HTTP 状态 500 - 请求处理失败;嵌套异常是 org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;错误的 SQL 语法 [select pet_adv,pet_adv_cd from Civil_t where regcase_type=?];嵌套异常是 org.postgresql.util.PSQLException: ERROR: 关系“civil_t”不存在

【问题讨论】:

  • 尝试在表名select pet_adv,pet_adv_cd from "civil_t" ... 或数据库名select pet_adv,pet_adv_cd from database.civil_t ... 周围使用双引号
  • 当我尝试在查询生成器中执行它时,同一数据库中的所有其他表都与此查询一起成功执行。问题不在于查询。
  • 在表不成立前加前缀数据库名。

标签: java sql spring postgresql jdbc


【解决方案1】:

底层 postgresql JDBC 驱动程序引发异常,因为在您连接的数据库中找不到表 civil_t

【讨论】:

  • 只有在查询生成器中执行过的查询才有效。
  • 要么你没有连接到不存在的数据库,要么数据库中不存在表。
猜你喜欢
  • 2012-05-01
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
  • 2012-04-23
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
相关资源
最近更新 更多