我想创建返回一些值的SQL查询,其中一个值为 blob 。
当我在我的选择中放入blob列时,它返回BigDecimal:|
带参数的纯JDBC查询
答案 0 :(得分:2)
查看Materializing Blob Data上的javadocs部分。
您需要在blob列上调用getBlob
,然后读取二进制流。
Blob blob = rs.getBlob("BLOB_COLUMN_NAME");
InputStream blobStream = blob.getBinaryStream();
//e.g. save blob to a file
FileOutputStream out = new FileOutputStream("file.txt");
byte[] buffer = new byte[1024];
int n = 0;
while( (n = blobStream.read(buffer)) != -1 ) {
out.write(buffer, 0, n);
}
out.flush();
out.close();
blobStream.close();
答案 1 :(得分:1)
插入..
stmt = con.prepareStatement("INSERT INTO TABLE(fileName, "
+ "blobData) VALUES(?, ?)");
stmt.setString(1, "somefilename");
stmt.setObject(2, data);//data is byte[]
stmt.executeUpdate();
选择..
ResultSet rs;
stmt = con.prepareStatement("SELECT blobData "
+ "FROM BlobTest " + "WHERE fileName = ?");
stmt.setString(1, "somevalue");
rs = stmt.executeQuery();
if (!rs.next()) {
System.out.println("No such file stored.");
} else {
Blob b = rs.getBlob(1);
BufferedOutputStream os;
答案 2 :(得分:1)
尝试从ResultSet调用方法getBytes():
stmt = dbConn.prepareStatement("SELECT ...");
ResultSet rs = stmt.executeQuery();
byte[] blob = rs.getBytes(1);
这应该将blob对象作为字节数组返回。