When a database field is a BLOB type, we need to be aware of using the Setbinarystream (Int,inputstream,int) method in PreparedStatement
When inserting data into a BLOB field type, use Javaio's inputstream to read the file.
Instead, when you read the data from the Blob field, you also use the Javaio InputStream, and then write the file with Javaio OutputStream.
Problems in the same CLOB example
If you do not add a type conversion where the byte stream is set, the following is true:
Stat.setbinarystream (1, in, File.length ());
The following error will appear
Exception in thread "main" Java.lang.AbstractMethodError:com.mysql.jdbc.PreparedStatement.setBinaryStream (iljava/ Io/inputstream; J) V
At Test.jdbc.BlobRW.create (blobrw.java:38)
At Test.jdbc.BlobRW.main (blobrw.java:24)
Later looked at Java and MySQL JDBC driver Two aspects of the Code, the reason is clear, originally used in the jdk1.6 version, there is a long type of method.
The corresponding MySQL JDBC driver jar is not yet implemented.
After the type conversion, it can run normally. Run the code as follows:
public void Insertblob () throws Exception {String strimg= "e:/ibm/friend/src/abc.jpg"; String strtext= "E:/ibm/friend/src/abc.txt"; String strarea= "China China China"; conn = Dbutil.getconnection (); String sql = "INSERT into Userclob (id,name,photo,note) VALUES (?,?,?,?)"; this.pstm = conn.preparestatement (sql); This.pstm.setInt (1, 2); This.pstm.setString (2, "China 2"); File fileimg = new file (strimg); File Filetext=new file (strtext); this.pstm.setString (3, Strarea); This.pstm.setCharacterStream (4, New FileReader ( FILEIMG), (int) fileimg.length ()); This.pstm.executeUpdate (); This.pstm.close (); Conn.close ();}
Insert Com.mysql.jdbc.PreparedStatement.setBinaryStream on MySQL database longblob format data (iljava/io/inputstream; J) v Problem analysis