在Java中将数据类型nvarchar转换为datetime2时出错

时间:2017-11-06 05:58:35

标签: java sql-server

我从spring storedprocedure.execute方法调用一个MSSQL过程。我尝试将日期参数传递为java.sql.Date,java.sql.Timestamp和纯字符串,但是所有选项都给出了相同的错误“将数据类型nvarchar转换为datetime2时出错” 这是我的proc的语法

Map<String, Object> results = super.execute(errMsg,convertJavaDateToSql   (startDate),convertJavaDateToSql(endDate),null,null,null);

调用它的Java代码:

 Timestamp convertJavaDateToSql(String date){
            Date javaDate = null;
            try {
                javaDate =  dateFormat.parse(date);
                logger.info("Parsed Date is "+javaDate );
            } catch (ParseException e) {

                logger.error("Error Parsing startDate and endDate" );
            }

            if(javaDate!=null) {
                Timestamp sqlDateTime = new Timestamp(javaDate.getTime());
                logger.info("sql Date  "+sqlDateTime);
                return sqlDateTime;
            }else
                return null;
        }

和convertJavaDateToSql方法是

declareParameter(new SqlReturnResultSet("RESULT_LIST",
                    myDataExtractor));
            declareParameter(new SqlOutParameter("errorMsg",Types.VARCHAR));
    declareParameter(new SqlParameter("startDate",Types.TIMESTAMP));
    declareParameter(new SqlParameter("endDate", Types.TIMESTAMP));
    declareParameter(new SqlParameter("par1",Types.VARCHAR));
    declareParameter(new SqlParameter("par2", Types.VARCHAR));
    declareParameter(new SqlParameter("par3",Types.VARCHAR));

这就是我声明参数

的方式
   org.springframework.dao.DataIntegrityViolationException: CallableStatementCallback; SQL [{call sampleProc(?, ?, ?, ?, ?, ?)}Error converting data type nvarchar to datetime2.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type nvarchar to datetime2.
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:247)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1400)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1063)
    at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1102)
    at org.springframework.jdbc.object.StoredProcedure.execute(StoredProcedure.java:122)

这是错误日志

{{1}}

我在这个网站上发现了许多相同标题的问题但没有帮助java代码。有人可以帮我解决这个问题。 感谢

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。问题是storedProcedure.execute不维护参数顺序,因此它在另一个nvarchar字段中设置日期值。 我现在在执行方法中传递一个带有字段名称和值的地图,它可以工作。

  Map<String,Object> inParameters = new HashMap<>();
                //Its a mandatory field , so passing it as null.
                inParameters.put(DAOConstants.ERROR_MSG, null);
                inParameters.put(DAOConstants.START_DATE, converJavaToSqlDate(startDate));
                inParameters.put(DAOConstants.END_DATE, converJavaToSqlDate(endDate));
                inParameters.put(DAOConstants.PARAM1, par1);
                inParameters.put(DAOConstants.PARAM2, par2);
                inParameters.put(DAOConstants.PARAM3, par3);

                Map<String, Object> results = super.execute(inParameters);