如何使用jdbc将插入查询中的二进制数据插入到Postgresql中

时间:2016-02-21 18:09:48

标签: java postgresql jdbc

我有如下表结构

CREATE  TABLE PUBLIC.STAFF(
    STAFF_ID INT NOT NULL,
    FIRST_NAME VARCHAR(45) NOT NULL,
    LAST_NAME VARCHAR(45) NOT NULL,
    ADDRESS_ID SMALLINT NOT NULL,
    PICTURE BYTEA,
    EMAIL VARCHAR(50),
    STORE_ID INT NOT NULL,
    ACTIVE BOOLEAN NOT NULL,
    USERNAME VARCHAR(16) NOT NULL,
    PASSWORD VARCHAR(40),
    LAST_UPDATE TIMESTAMP NOT NULL
); 

我在脚本文件中有几个插入查询或存储在arraylist中,如

INSERT INTO STAFF(STAFF_ID, FIRST_NAME, LAST_NAME, ADDRESS_ID, PICTURE, EMAIL, STORE_ID, ACTIVE, USERNAME, PASSWORD, LAST_UPDATE)
VALUES (
    1,
    'Mike',
    'Hillyer',
    3,
    X'89504e470d0a1a0a0000000d4948445200000079000000750802000000e55ad965000000097048597300000ec300000ec301c76fa8640000200049444154789c4cbb7794246779ffbbf78f7b7ebe466177677772ce3d9d667aa67ba62776ce39545557ce3974ee9eb049ab95563922104142580830d10203061bb049064cb031d916c160100284505aedee4cdd3f16b8b7ce73de53f5d61f75cee7bcf53ccff7fb561dbb7ce9ad972fbdf5aecb6fbd74e7a3b75f7ef4ce7bde72e9ae375fbaffcd676ebff7e29d658c864812c0e90acec0040d123aad8a284f950906205810672b140d900226b218c713028f0a5c8',
    'Mike.Hillyer@sakilastaff.com',
    1,
    TRUE,
    'Mike',
    '8cb2237d0679ca88db6464eac60da96345513964',
    TIMESTAMP '2006-02-15 04:57:16.0'
); 

当我尝试使用JDBC程序将数据插入postgres时,我收到以下错误:

ERROR:  column "picture" is of type bytea but expression is of type bit
LINE 2: (1, 'Mike', 'Hillyer', 3, X'89504e470d0a1a0a0000000d49484452..                                 
HINT:  You will need to rewrite or cast the expression.
********** Error **********

ERROR: column "picture" is of type bytea but expression is of type bit

如何使用Java解决此问题?

1 个答案:

答案 0 :(得分:0)

试试这个:

E'\\x89504e470d0a1a0a0000000d4948445200000079000000750802000000e55ad965000000097048597300000ec300000ec301c76fa8640000200049444154789c4cbb7794246779ffbbf78f7b7ebe466177677772ce3d9d667aa67ba62776ce39545557ce3974ee9eb049ab95563922104142580830d10203061bb049064cb031d916c160100284505aedee4cdd3f16b8b7ce73de53f5d61f75cee7bcf53ccff7fb561dbb7ce9ad972fbdf5aecb6fbd74e7a3b75f7ef4ce7bde72e9ae375fbaffcd676ebff7e29d658c864812c0e90acec0040d123aad8a284f950906205810672b140d900226b218c713028f0a5c8'