带有SQL Server的SpringBoot JdbcTemplate:不支持从UNKNOWN到UNKNOWN的转换

时间:2018-02-15 10:53:11

标签: sql-server jdbc spring-data spring-jdbc jdbctemplate

我有一个SpringBoot应用程序。使用JdbcTemplate连接到SQL Server数据库,使用以下语法:

int numOfRowsAffected = remoteJdbcTemplate.update(
        "insert into dbo.[ATRESMEDIA Resource Time Registr_]  " +
        "( [Entry No_], [Record ID], [Posting Date], [Resource No_], [Job No_], [Work Type], [Quantity], [Unit of Measure], [Description], [Company Name], [Created Date-Time], [Status] ) " +
                " VALUES (?,?,?,?,?,?,?,?,?,?,?,?);",

                atresMediaTimeRegistr.getEntryNo(),
                "Record ID",
                atresMediaTimeRegistr.getPostingDate(),
                atresMediaTimeRegistr.getResourceNo(),
                atresMediaTimeRegistr.getJobNo(),
                atresMediaTimeRegistr.getWorkType(),
                atresMediaTimeRegistr.getQuantity(),
                atresMediaTimeRegistr.getUnitOfMeasure(),
                atresMediaTimeRegistr.getDescription(),
                atresMediaTimeRegistr.getCompanyName(),
                atresMediaTimeRegistr.getCreatedDate(),
                1);

其中[Record ID]varbinary类型,但我收到此错误

Caused by: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [insert into dbo.[ATRESMEDIA Resource Time Registr_]  ( [Entry No_], [Record ID], [Posting Date], [Resource No_], [Job No_], [Work Type], [Quantity], [Unit of Measure], [Description], [Company Name], [Created Date-Time], [Status] )  VALUES (?,?,?,?,?,?,?,?,?,?,?,?);]; SQL state [S0003]; error code [257]; Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query.

1 个答案:

答案 0 :(得分:1)

您的问题有一个答案:...Implicit conversion from data type nvarchar to varbinary is not allowed...您正在尝试将字符串作为varbinary传递。首先,你必须做这样的事情 byte[] varBinary = "record_ID".getBytes(StandardCharsets.UTF_8); 然后将其作为Record_ID参数放入jdbcTemplate中。 祝你好运,希望它会有所帮助!