带有 SQL 服务器的 SpringBoot JdbcTemplate:不支持从 UNKNOWN 到 UNKNOWN 的转换

SpringBoot JdbcTemplate with SQL Server: The conversion from UNKNOWN to UNKNOWN is unsupported

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

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.

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