简体   繁体   中英

How to insert data in to mysql database table which contain mediumtext datatype using java

I want to insert image into table which contain a field with datatype MEDIUMTEXT. Please can any one tell how to insert mediumtext data

psmt=con.prepareStatement("INSERT INTO ofvcard (username,vcard) " + "VALUES (?,?)");
psmt.setString(1,"admin");
fis=new FileInputStream(image);
psmt.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
int s = psmt.executeUpdate();

As documented under The BLOB and TEXT Types :

BLOB values are treated as binary strings (byte strings). They have no character set, and sorting and comparison are based on the numeric values of the bytes in column values. TEXT values are treated as nonbinary strings (character strings). They have a character set, and values are sorted and compared based on the collation of the character set.

Therefore your image file (which is binary , not character , data) may be hitting some encoding issues when byte sequences which are not valid in the column's character set are encountered.

You could instead use the MEDIUMBLOB type, but really one should store files in a filesystem (which is a database designed specifically for that purpose) and simply store the respective filesystem paths in MySQL.

psmt=con.prepareStatement("INSERT INTO ofvcard (username,vcard) " + "VALUES (?,?)"); psmt.setString(1,"admin"); pstmt.setString(2,"testing") int s = psmt.executeUpdate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM