Easiest way to convert a Blob into a byte array

2020-01-28 03:14发布

what is the easiest way to convert a Blob into a byte array?I am using MYSQL and i want to convert a Blob datatype into a byte array.

Iam using java programming language:)

2条回答
萌系小妹纸
2楼-- · 2020-01-28 03:50

the mySql blob class has the following function :

blob.getBytes

use it like this:

//(assuming you have a ResultSet named RS)
Blob blob = rs.getBlob("SomeDatabaseField");

int blobLength = (int) blob.length();  
byte[] blobAsBytes = blob.getBytes(1, blobLength);

//release the blob and free up memory. (since JDBC 4.0)
blob.free();
查看更多
贪生不怕死
3楼-- · 2020-01-28 03:58

The easiest way is this.

byte[] bytes = rs.getBytes("my_field");
查看更多
登录 后发表回答