微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用PreparedStatementSetter 设置参数跟 表字段类型不匹配出现的错误

错误提示
[ INFO 2008-04-08 14:57:42 org.springframework.jdbc.support.sqlErrorCodesFactory - 120 - <init> ] : sqlErrorCodes loaded: [DB2,Hsql,MS-sql,MysqL,Oracle,Informix,Postgresql,Sybase]
[ ERROR 2008-04-08 14:57:42 yourpackagePaht.dao.impl.jdbc.postgresql.OcapDaoImpl - 247 - insertOcap ] : org.springframework.jdbc.BadsqlGrammarException: PreparedStatementCallback; bad sql grammar [INSERT INTO tableName (last_group_date) VALUES (?)]; nested exception is org.postgresql.util.PsqlException: ERROR: column "last_group_date" is of type timestamp with time zone but expression is of type character varying

源码
new PreparedStatementSetter(){
public void setValues(PreparedStatement ps) throws sqlException {
int i=1;
ps.setString(i++,"2008-04-10" );
}

原因

数据库表的类型跟PreparedStatementSetter回调方法内的设置参数不一致。如果用sql 直接调试

INSERT INTO tableName (last_group_date ) VALUES ("2008-04-10") 在Database直接运行是没有问题的,
但在PreparedStatementSetter 用setString(i,"2008-04-10")是有问题的

正确代码

new PreparedStatementSetter(){ public void setValues(PreparedStatement ps) throws sqlException { int i=1; ps.setDate(i++,ConvertFormula.changeDateStrToTimestamp("2008-04-10") ); }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐