Insert Com.mysql.jdbc.PreparedStatement.setBinaryStream on MySQL database longblob format data (iljava/io/inputstream; J) v Problem analysis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.