在 java 中使用预制的 stmt 设置十六进制值
Set Hex Value using prepeared stmt in java
我在下面声明:
insert into t1 (col1) values (x'4D7953514C');
我想使用准备好的语句将此值插入 DB2 数据库table:
insert into t1 (col1) values (?);
如果我使用字符串和数据类型 NCHAR
我得到一个异常:
com.ibm.db2.jcc.b.SqlException: Unrecognized jdbc type -15
我怎样才能做到这一点?
更新:
String SQL = "insert into t1 (col1) values (?);"
String id = "x'"+id+"'"; // here id = 20151120120811356186000000
Object args[] = { id};
int types[] = { Types.CHAR};
db2Template.update(SQL, args, types);
x'4D7953514C'
不是 "hex string";它是一个二进制字符串,对应DB2数据类型VARCHAR(whatever_length) FOR BIT DATA
(也可能是CHAR(whatever_length) FOR BIT DATA
)。
相应的 Java 数据类型是 byte[]
,因此您需要执行类似
的操作
byte[] x = new byte[] { 0x20, 0x15, ..., 0x00};
...
int types[] = {Types.WHATEVER_CORRESPONDS_TO_THE_ACTUAL_DB2_TYPE};
...
我在下面声明:
insert into t1 (col1) values (x'4D7953514C');
我想使用准备好的语句将此值插入 DB2 数据库table:
insert into t1 (col1) values (?);
如果我使用字符串和数据类型 NCHAR
我得到一个异常:
com.ibm.db2.jcc.b.SqlException: Unrecognized jdbc type -15
我怎样才能做到这一点?
更新:
String SQL = "insert into t1 (col1) values (?);"
String id = "x'"+id+"'"; // here id = 20151120120811356186000000
Object args[] = { id};
int types[] = { Types.CHAR};
db2Template.update(SQL, args, types);
x'4D7953514C'
不是 "hex string";它是一个二进制字符串,对应DB2数据类型VARCHAR(whatever_length) FOR BIT DATA
(也可能是CHAR(whatever_length) FOR BIT DATA
)。
相应的 Java 数据类型是 byte[]
,因此您需要执行类似
byte[] x = new byte[] { 0x20, 0x15, ..., 0x00};
...
int types[] = {Types.WHATEVER_CORRESPONDS_TO_THE_ACTUAL_DB2_TYPE};
...