Note about how to operate Blob type in database using JDBC.
BLOB (Binary Large Object) is a data type in standard SQL used to store large amounts of data. It is basically a binary string of variable length, stored as a sequence of bytes or octets. BLOB data type is generally used to store large files such as images, media files such as video and audio clips in the database. We can write a BLOB value to the database as binary or character string depending on the file or requirement.
Type | Size(Byte) |
---|---|
TinyBlog | max: 255 |
Blog | max: 65K |
MediumBlob | max:16M |
LongBlob | max: 4G |
1 | public class blobTest { |
Assume the SQL statement looks like this
1 | select id, name, email, birth, photo from custormers where name = "xxx" |
the type of photo is blob. So there are two ways wo get the result:
by passing numbers
1 | int id = rs.getInt(1); |
by alias name(Recommended ways)
1 | int id = rs.getInt("id"); |
Get blob data
1 | Blob photo = rs.getBlog("photo"); |
And at end close the resource including InputStream
.
If there is an error when using MySQL: xxx too large
, but you the Type of Blob you use is big enough for the content, then you can add the config information in my.ini
file, add max_allowed_packet=16M
and restart MySQL.