【问题标题】:byte [] to blob conversion in java using preparedstatement and sqllitejava中使用preparedstatement和sqlite将byte []转换为blob
【发布时间】:2015-05-15 08:30:53
【问题描述】:

我正在尝试将字节 [] 转换为 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,斑点)。我做了一点谷歌并尝试了下面的代码,但它在 connection.createBlob() 处给出了 null。

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

【问题讨论】:

  • 你有没有试过只打电话给preStatement.setBytes(1, bytes)stackoverflow.com/questions/6090170/… 为什么要担心 256 字节的 BLOB?
  • 添加堆栈跟踪和查询
  • connection.createBlob() 处为空。表示未创建连接。添加堆栈跟踪或完整代码
  • connection.createBlob() 给我 null 并创建连接对象。请找到堆栈跟踪。目标 VM 中发生异常:SQLite JDBC 驱动程序未实现 java.sql.SQLException:SQLite JDBC 驱动程序未在 preStatement 上实现。 setBlob(1, blob)

标签: java sqlite blob


【解决方案1】:

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

【讨论】:

  • 声明前。 setBytes(1, hashValue) 给我在目标 VM 中发生异常:查询不返回结果 java.sql.SQLException:查询不返回结果在
  • 您正在执行的查询是什么?如果是insert/update/delete query则执行preStatement.execute();方法,如果是select query则使用rs = preStatement.executeQuery();方法。
猜你喜欢
  • 2021-05-25
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多