2015-05-15 45 views
0

我想要的byte []轉換成二進制大對象的Java BLOB轉換。我的代碼如下的byte [],使用PreparedStatement和sqllite

 byte [] hashValue = HashClass.hash256Bytes(messageValue); 

     //query = "insert into message values (?)"; 
     connection = ConnectionFactory.getConnection(); 
     //Blob blob = new javax.sql.rowset.serial.SerialBlob(hashValue); 
     Blob blob = new SerialBlob(hashValue); 
     //blob.setBytes(1, hashValue); 

     preStatement = (PreparedStatement)  
     connection.prepareStatement(query); 
     preStatement. setBlob(1, blob); 
     rs = preStatement.executeQuery(); 

但它在預先聲明中斷。 setBlob(4,blob)。我已經做了谷歌並嘗試下面的代碼,但它給null null connection.createBlob()。

 Blob blob = connection.createBlob(); 
     blob.setBytes(1, bytes); 
+0

你試過只是調用'preStatement.setBytes(1字節)'? http://stackoverflow.com/questions/6090170/cannot-save-image-as-blob-to-sqlite爲什麼要擔心256字節的BLOB? – Thilo

+0

在connection.createBlob()處添加堆棧跟蹤和查詢 – Jens

+0

null。意味着連接不被創建。添加堆棧跟蹤或完整代碼 – dogant

回答

0

嘗試setBytes();方法

byte [] hashValue = HashClass.hash256Bytes(messageValue); 

    //query = "insert into message values (?)"; 
    connection = ConnectionFactory.getConnection(); 
    //Blob blob = new javax.sql.rowset.serial.SerialBlob(hashValue); 
    //Blob blob = new SerialBlob(hashValue); 

    preStatement = (PreparedStatement) connection.prepareStatement(query); 
    preStatement. setBytes(1, hashValue); 
    rs = preStatement.executeQuery(); 
+0

preStatement。 setBytes(1,hashValue)給我異常發生在目標虛擬機:查詢不返回結果 java.sql.SQLException:查詢不返回結果 \t在 – sangita

+1

你正在執行的查詢是什麼?如果是「插入/更新/刪除查詢」,則執行'preStatement.execute();'方法,如果選擇查詢,則使用rs = preStatement.executeQuery()方法。 – ELITE