2011-07-12 48 views

回答

59

MySQL的BLOB类具有以下功能:

blob.getBytes

这样使用它:

//(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(); 
+0

谢谢,实际上blob没有getbytes()方法,只有serialblob有getbytes()方法。如何做blob。 – androidGuy

+0

你使用'java.sql.Blob'吗? –

+5

不要忘了调用'blob.free()' –

39

最简单方式是这样的。

byte[] bytes = rs.getBytes("my_field"); 
+5

+1比我更整洁:) –

相关问题