MGD*_*MGD 4 java mysql spring jdbc
我到了
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);
}
Run Code Online (Sandbox Code Playgroud)
}
结果Extractor类公共类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;
}
Run Code Online (Sandbox Code Playgroud)
}
从表中选择数据的类
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;
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个老线程,但希望任何绊倒这个问题的人都会发现这个答案很有用.
我在春季应用程序中遇到了同样的异常.基于输出,我认为我的查询存在语法问题.事实证明,我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
完成上述更改后(我的字段名为product_id和product_name),我的应用程序正常运行.请记住,您对列名称或java字段所做的任何更改不仅应该用于查询语句,还应该用于mapper方法.我看到OP正确地做到了这一点,但我认为值得重申.我希望有人觉得这很有帮助.
为了在列名中使用破折号,您需要使用反向标记来转义它们.
"SELECT cid, `clinician-code`, password, `first-name`, `last-name`
FROM Clinician
WHERE `clinician-code` = ?"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65825 次 |
| 最近记录: |