最近在“玩”hibernate Annotation,弄了個(gè)Attachment保存進(jìn)數(shù)據(jù)庫(kù)的測(cè)試,附件內(nèi)容保存在content屬性里面。
歪招終歸是歪招,這兩天老為這東西心煩,晚上吃飯的時(shí)候無(wú)意中想起charset的問(wèn)題,把代碼撿回來(lái)再測(cè)試了一下,問(wèn)題解決,哈哈!
??@Lob
??@Column(columnDefinition?=?"LongBlob")
??public?byte[]?getContent()?{
????return?content;
??}
一開(kāi)始,配置mysql jdbc url如下??@Column(columnDefinition?=?"LongBlob")
??public?byte[]?getContent()?{
????return?content;
??}
jdbc.url????????????=????jdbc:mysql://localhost/test\
?????????????????????????????useUnicode=true\
????????????????????????????&characterEncoding=gbk
一測(cè)試,報(bào)錯(cuò)?????????????????????????????useUnicode=true\
????????????????????????????&characterEncoding=gbk
Caused?by:?java.sql.BatchUpdateException:?Syntax?error?or?access?violation?message?from?server:?"You?have?an?error?in?your?SQL?syntax?near?''D0CF11E0A1B11AE1000000000000000000000000000000003E000300FEFF0900060000000000000'?at?line?1"
????at?com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1540)
同樣的語(yǔ)句,直接在mysql命令行下運(yùn)行就沒(méi)問(wèn)題,偏偏jdbc上就有這臭毛病。換jdbc driver版本,改@Lob為 @Type(type = "org.springframework.orm.hibernate3.support.BlobByteArrayType"),改hibernate配置(比如hibernate.jdbc.use_streams_for_binary true),甚至是直接用jdbc來(lái)insert,亂七八糟折騰半天,問(wèn)題照舊。最后只好用上“歪”招,把byte[]配成String,在保存的時(shí)候把byte[]保存成hex String格式,取的時(shí)候再解碼回來(lái)。????at?com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1540)
歪招終歸是歪招,這兩天老為這東西心煩,晚上吃飯的時(shí)候無(wú)意中想起charset的問(wèn)題,把代碼撿回來(lái)再測(cè)試了一下,問(wèn)題解決,哈哈!
jdbc.url????????????=????jdbc:mysql://localhost/mmwy_blog\
?????????????????????????????useUnicode=true\
????????????????????????????&characterEncoding=utf-8
如果設(shè)成iso-8859-1、utf-8,保存一點(diǎn)問(wèn)題都沒(méi)有,換用gbk、gb2312、big5之類(lèi)的字符集,問(wèn)題就出來(lái)了。?????????????????????????????useUnicode=true\
????????????????????????????&characterEncoding=utf-8