JDBC Spring中的Bad Sql Grammar异常
Posted
技术标签:
【中文标题】JDBC Spring中的Bad Sql Grammar异常【英文标题】:Bad Sql Grammar exception in JDBC spring 【发布时间】:2012-08-29 13:27:14 【问题描述】:我得到了
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;糟糕的 SQL 语法 [选择 cid, 临床医生的代码、密码、名字、姓氏 临床医生代码=?];嵌套异常是 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知 “字段列表”中的“临床医生”列
以下代码出错,您也可以在屏幕截图中看到表格,除了 cid 所有其他属性都是 VARCHAR(45)
行映射器类
public class CClinicianRowMapper implements RowMapper
@Override
public Object mapRow(ResultSet rs, int line) throws SQLException
CClinicianResultSetExtractor extractor = new CClinicianResultSetExtractor();
return extractor.extractData(rs);
结果提取器类 公共类 CClinicianResultSetExtractor 实现 ResultSetExtractor
@Override
public Object extractData(ResultSet rs) throws SQLException
CClinician clinician = new CClinician();
clinician.setCid(rs.getLong("cid"));
clinician.setClinicianCode(rs.getString("clinician-code"));
clinician.setPassword(rs.getString("password"));
clinician.setFirstName(rs.getString("first-name"));
return clinician;
从表中选择数据的类
public List<CClinician> findClinician(CClinician _clinician)
// TODO Auto-generated method stub
JdbcTemplate select = new JdbcTemplate(dataSource);
try
return select.query("select cid, clinician-code, password, first-name, last-name from Clinician where clinician-code= ?",
new Object[] _clinician.getClinicianCode(), new CClinicianRowMapper());
catch (Exception e)
e.printStackTrace();
return null;
【问题讨论】:
【参考方案1】:为了在列名中使用破折号,您需要用反引号将它们转义。
"SELECT cid, `clinician-code`, password, `first-name`, `last-name`
FROM Clinician
WHERE `clinician-code` = ?"
【讨论】:
非常感谢,异常消失了,但尽管数据在表中,但它仍然没有返回任何查询【参考方案2】:我知道这是一个旧线程,但希望任何偶然发现这个问题的人都会发现这个答案很有用。
我在我的 spring 应用程序中遇到了同样的异常。根据输出,我认为我的查询存在语法问题。事实证明,我的 mapper 方法实际上存在语法错误。我遇到了这个问题,因为我最初创建的 Product 类的字段名称与列名称不同。
public class ProductMapper implements RowMapper<Product>
public Product mapRow(ResultSet rs, int rowNum) throws SQLException
Product product = new Product();
product.setProduct_id(rs.getInt("product_id")); //was id. was causing BadSqlGrammarException
product.setProduct_name(rs.getString("product_name")); //was name. was causing BadSqlGrammarException
product.setPrice(rs.getDouble("price"));
product.setQuantity(rs.getInt("quantity"));
return product;
完成上述更改后(我的字段名为 product_id 和 product_name),我的应用程序就可以正常工作了。请记住,您对列名或 java 字段所做的任何更改不仅应针对您的查询语句,还应针对您的映射器方法。我看到 OP 正确地做到了这一点,但我认为值得重申。我希望有人觉得这很有帮助。
【讨论】:
是的,你是对的,目前我面临着同样的问题,在阅读这篇文章之前,我猜这是因为映射器的缘故。 这正是我遇到的问题。非常感谢! 您为我省去了很多麻烦,谢谢。当您专注于请求时,很难想到映射器。【参考方案3】:正确检查您的查询语法是否缺少 ;或额外的逗号 (,) 。 “Bad SQL Grammar Exception”仅与 Sql 查询中的语法错误有关。 在使用 Spring JDBC 时,我在以下代码中遇到了同样的错误:
String query= "update student set name=?, city=?, where id=?"; // due to extra comma after city=?,
将代码更改为
String query= "update student set name=? city=?, where id=?";
错误已解决。
【讨论】:
以上是关于JDBC Spring中的Bad Sql Grammar异常的主要内容,如果未能解决你的问题,请参考以下文章
SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.Nes
PreparedStatementCallback; JDBC Spring 中的错误 SQL 语法
从 Camel 和 Spring Boot 到 MS SQL Server 的 JDBC