使用preparedstatement和sqllite在java中执行byte []到blob转换

时间:2015-05-15 08:30:54

标签: java sqlite blob

我正在尝试将byte []转换为blob。我的代码如下

        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)。我有点google并尝试下面的代码,但它在connection.createBlob()。

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

1 个答案:

答案 0 :(得分: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();