【问题标题】:how to write a BLOB into a database when PreparedStatement.setBlob() not implemented by JDBC driver当 JDBC 驱动程序未实现 PreparedStatement.setBlob() 时如何将 BLOB 写入数据库
【发布时间】:2013-09-28 19:52:37
【问题描述】:

在我使用以下代码将 BLOB 值写入数据库之后:

    // bb is of type ByteBuffer
    Blob blob = new SerialBlob(bb.array());
    sqlStatement = "UPDATE Words SET Examples=? WHERE Word=\"" + word + "\"";
    // c is of type Connection
    PreparedStatement pstmt = c.prepareStatement(sqlStatement);
    pstmt.setBlob(1, blob);
    pstmt.executeUpdate();
    c.commit();

我收到以下错误:

java.sql.SQLException: SQLite JDBC 驱动没有实现

无论我从哪里下载此驱动程序,我的问题是您是否知道将 BLOB 值写入 SQLite 数据库的任何 SAFE 方法。

【问题讨论】:

  • Example 的值在哪里设置?

标签: java sqlite jdbc


【解决方案1】:

我认为您忘记为 Example 列设置值

st.setAsciiStream(1, fin, (int) file.length());//if you are storing file in database

【讨论】:

  • 最好使用 setAsciiStream() 或 setBinaryStream() 代替 setBlob()
【解决方案2】:

我想我找到了一个有用的方法。最后我使用了下面的代码,它成功了!

pstmt.setBytes(1, blob.getBytes(1, bb.capacity()));

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 2016-01-01
    • 2019-09-17
    • 1970-01-01
    • 2015-07-23
    • 2017-11-13
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多