使用 preparedstatement 和 sqlite 在 java 中将 byte [] 转换为 blob

byte [] to blob conversion in java using preparedstatement and sqllite

我正在尝试将 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();

但是它在 preStatement 处中断了。 setBlob(4,斑点)。我做了位 google 并尝试了下面的代码,但它在 connection.createBlob().

处给出了 null
       Blob blob = connection.createBlob();
       blob.setBytes(1, bytes);

尝试使用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();