Java - 返回Blob的SQLite Web服务 [英] Java - SQLite Web Service Returning Blob

查看:118
本文介绍了Java - 返回Blob的SQLite Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java Web服务,它正在查询包含blob数据的SQLite数据库。我正在返回sql语句,以便服务的使用者可以插入/更新/删除而不处理数据。我的问题是当我得到blob数据时,sqlite-jdbc驱动程序说没有实现getBlob函数。所以我的问题是可行的吗?是否有更好的驱动程序或方法来完成此任务?

I have a Java web service that is querying a SQLite database that contains blob data. I'm returning sql statements so that consumers of the service can just insert/update/delete with out processing the data. My problem is when I get to the blob data, the sqlite-jdbc driver says that the getBlob function is not implemented. So my question is this doable? Is there a better driver or way to accomplish this task?

谢谢!

推荐答案

SQLite支持BLOB数据类型。 Sqlitejdbc驱动程序不支持ResultSet#getBlob()方法,但支持BLOB。只需使用ResultSet#getBytes()方法。下面是示例代码(我没有在此代码中进行适当的异常处理以使其变得简单):

SQLite supports BLOB datatype. Sqlitejdbc driver does not support the ResultSet#getBlob() method but BLOBs are supported. Just use ResultSet#getBytes() method. Here is the sample code (I did not put proper exception handling in this code to make it simple):

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;

public class SQLiteTest {

public static void main(String[] args) throws SQLException,
        ClassNotFoundException {
    Class.forName("org.sqlite.JDBC");
    Connection connection = DriverManager
            .getConnection("jdbc:sqlite:test.db");
    Statement statement = connection.createStatement();
    createTable(statement);
    insertBlob(connection);
    byte[] bytes = query(statement);
    System.out.println(Arrays.toString(bytes));
            statement.close();
    connection.close();
}

private static void createTable(Statement statement) throws SQLException {
    statement.execute("CREATE TABLE test (data BLOB)");
}

private static void insertBlob(Connection connection) throws SQLException {
    PreparedStatement pStatement = connection
            .prepareStatement("INSERT INTO test VALUES (?)");
    pStatement.setBytes(1, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    pStatement.execute();
    pStatement.close();
}

private static byte[] query(Statement statement) throws SQLException {
    ResultSet rs = statement.executeQuery("SELECT data FROM test");
    byte[] bytes = rs.getBytes(1);
    return bytes;
}

}

这篇关于Java - 返回Blob的SQLite Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆