【发布时间】:2012-03-20 10:49:57
【问题描述】:
我正在开发一个 Java 服务器应用程序,该应用程序使用 Spring 3 和 C3P0 访问 Microsoft SQL Server 2008 R2 数据库,并使用 Microsoft JDBC 4 驱动程序 3.0 版。
我有一个存储过程,它的输入定义如下:
@modifiedAfter datetime = NULL
我正在使用 Spring 来构造对这个存储过程的调用。
我正在构建一个 MapSqlParameterSource 来包含我的参数:
MapSqlParameterSource in = new MapSqlParameterSource()
in.addValue("modifiedAfter", "2011-01-01T00:00:00", Types.TIMESTAMP)
但是当我执行调用时:
this.sprocCall.execute(in);
我明白了:
com.microsoft.sqlserver.jdbc.SQLServerException: 从字符串转换日期和/或时间时转换失败
……我不知道为什么。
我尝试了一些添加参数的变体,例如将其作为 Date 传递,或将其指定为 VARCHAR,或不指定类型 - 它们都不起作用。
我开始怀疑这个问题可能与 Spring 有关。我写了一个小 Groovy 脚本来尝试隔离问题,这很好用:
dt = new DateTime("2012-02-01T00:00:00") // Joda DateTime
println sql.rows("exec spMySproc @modifiedAfter=${Sql.TIMESTAMP(dt.toString())}")
...但是当我尝试使用 MapSqlParameterSource 的等效方法时,我得到了上述错误。
在这一点上,我被难住了。
这是堆栈跟踪的顶部:
org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call spGetPoliciesCatalogPaged(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}]; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting date and/or time from character string.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:97)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:952)
at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:985)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:368)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:342)
at org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:164)
…后面是我的一些课程,然后是:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting date and/or time from character string.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet$FetchBuffer.nextRow(SQLServerResultSet.java:4762)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.fetchBufferNext(SQLServerResultSet.java:1682)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:955)
at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:2859)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:91)
at org.springframework.jdbc.core.JdbcTemplate.processResultSet(JdbcTemplate.java:1124)
at org.springframework.jdbc.core.JdbcTemplate.extractReturnedResults(JdbcTemplate.java:1023)
at org.springframework.jdbc.core.JdbcTemplate$5.doInCallableStatement(JdbcTemplate.java:995)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:936)
... 68 more
我尝试更新到最新版本的spring-jdbc,3.1.1,但没有帮助。
如果有任何帮助,我将不胜感激!
谢谢, 视频
【问题讨论】:
-
你试过用字符串代替 Types.TIMESTAMP 吗?我认为Types.TIMESTAMP格式不能在sql server中转换。
-
SQL Server 中的TIMESTAMP 与日期或时间完全无关。
-
@ThitLwinOo, @Aaron:我没有使用 SQL Server
timestamp格式。我使用的是 JDBC TIMESTAMP 格式。根据this page,JDBC 驱动程序将JDBC TIMESTAMP 格式映射到SQL Server 类型datetime和datetime2。此外,上面使用 JDBC TIMESTAMP 的 Groovy 脚本也可以正常工作。
标签: java sql-server spring stored-procedures jdbc