我有一个SpringBoot应用程序,我使用jdbcTemplate将行插入mssql
int numOfRowsAffected = remoteJdbcTemplate.update("insert into dbo.[ELCOR 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 (?,CONVERT(varbinary,?),?,?,?,?,?,?,?,?,?,?);",
ELCORResourceTimeRegistr.getEntryNo(),
ELCORResourceTimeRegistr.getEntryNo()),
ELCORResourceTimeRegistr.getPostingDate(),
ELCORResourceTimeRegistr.getResourceNo(),
jobNo,
ELCORResourceTimeRegistr.getWorkType(),
ELCORResourceTimeRegistr.getQuantity(),
ELCORResourceTimeRegistr.getUnitOfMeasure(),
ELCORResourceTimeRegistr.getDescription(),
ELCORResourceTimeRegistr.getCompanyName(),
ELCORResourceTimeRegistr.getCreatedDate(),
0);
ELCORResourceTimeRegistr.getEntryNo()
的值是一个值为0x00173672
但是在数据库中插入的内容是<30007800 30003000 31003700 33003600 37003200>
ELCORResourceTimeRegistr.getEntryNo().getClass().getCanonicalName() => java.lang.String
答案 0 :(得分:1)
documentation for the CONVERT function表示二进制类型的默认“样式”为0
:
将ASCII字符转换为二进制字节,或将二进制字节转换为ASCII字符。每个字符或字节都以1:1转换。
所以,
SELECT CONVERT(VARBINARY, '0x00173672') AS foo;
返回
foo
--------------------------------------------------------------
0x30783030313733363732
是十六进制文字的ASCII字节值,而不是十六进制字节本身。为了使CONVERT 解释十六进制文字,您需要使用样式1
,即
SELECT CONVERT(VARBINARY, '0x00173672', 1) AS foo;
返回
foo
--------------------------------------------------------------
0x00173672