1. BLOB type in MySQL
Mysql can store large file data, generally used blob objects. Such as pictures, videos and so on.
BLOB is a binary object that can accommodate variable data. Because it is a binary object, it has nothing to do with the encoding method. There are 4 types of blobs: Tinyblob, Blob, Mediumblob, and Longblob. They are just maximum lengths that can be accommodated.
The maximum length of the four field types is as follows:
Copy code code as follows:
Tinyblob -255 bytes
Blob -65535 bytes (64KB)
Mediumblob -16,777,215 bytes (16MB) (2^24-1)
Longblob -4G bytes (2^32 1)
2. Java Reading MySQL Pictures
Below is the Phototest table structure defined in the test database.
The code for saving and reading pictures is as follows:
Import java.io.*; Import Java.sql.*; Public Class LoadStoreBlob {Public Static void Main (String [] ARGS) {dbconnection db = new dbconnection (); /); / /Responsible for connecting mysql database link connection conne = null; PreparedStatement PS = NULL; ResultSet RS = NULL; InputStream in = NULL; Try {// Read a picture from the local hard disk to the database conne = db.getConn (); .png ")) ; ps = con.preparestatement ("Insert Into Test.phototest Values (?,?)"); (( );; db.closeconn (conne); // Read the picture from the database to save the local hard disk Con = db.getConn (); ps = con.preparestatement ("select * from test.phototoTotest Where ID = ? "); PS.Setint (1,2); rs = ps.executequry (); rs.next (); // Person the cursor to the first line in = rs.getbinaryStream (" Photo "); byte [] b = new byte [in.available ()]; // The byte array of the newly saved picture data in.read (b); outputStream out = new fileoutputStream ("222.jpg"); out.write (b); out.flush (); out.Close (); db.Closeconn (conne);} Catch (Exception E) {System.out.println ("Error ::"+E);}}}}
3. Choose the right field size
If the maximum length of the selected field type is smaller, the data that is not saved may be reported, and the Mysql data truncation may be reported. like:
Copy code code as follows:
com.mysql.jdbc.mysqldatruncation: Data Truncation: Data too long for column 'Photo' at row 1 1
MySQL Chinese Reference Manual Type:
http://dev.mysql.com/doc/refman/5.1/zh/column-html#blob