﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-无聊人士-随笔分类-Database</title><link>http://www.blogjava.net/mmwy/category/4891.html</link><description>wuyu</description><language>zh-cn</language><lastBuildDate>Sun, 08 Apr 2007 09:11:04 GMT</lastBuildDate><pubDate>Sun, 08 Apr 2007 09:11:04 GMT</pubDate><ttl>60</ttl><item><title>mysql 3.23.58 charset设置对blob字段操作的影响</title><link>http://www.blogjava.net/mmwy/archive/2006/10/12/74800.html</link><dc:creator>mmwy</dc:creator><author>mmwy</author><pubDate>Thu, 12 Oct 2006 07:54:00 GMT</pubDate><guid>http://www.blogjava.net/mmwy/archive/2006/10/12/74800.html</guid><wfw:comment>http://www.blogjava.net/mmwy/comments/74800.html</wfw:comment><comments>http://www.blogjava.net/mmwy/archive/2006/10/12/74800.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mmwy/comments/commentRss/74800.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mmwy/services/trackbacks/74800.html</trackback:ping><description><![CDATA[最近在“玩”hibernate Annotation，弄了个Attachment保存进数据库的测试，附件内容保存在content属性里面。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">  @Lob<br />  @Column(columnDefinition </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">LongBlob</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />  </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">[] getContent() {<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> content;<br />  }</span></div>一开始，配置mysql jdbc url如下<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">jdbc.url            </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">    jdbc:mysql://localhost/test\<br />                            ?useUnicode</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">true\<br />                            &amp;characterEncoding</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">gbk</span><span style="color: rgb(0, 0, 0);"></span></div>一测试，报错<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Caused by: java.sql.BatchUpdateException: Syntax error or access violation message from server: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">You have an error in your SQL syntax near ''D0CF11E0A1B11AE1000000000000000000000000000000003E000300FEFF0900060000000000000' at line 1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:</span><span style="color: rgb(0, 0, 0);">1540</span><span style="color: rgb(0, 0, 0);">)</span></div>同样的语句，直接在mysql命令行下运行就没问题，偏偏jdbc上就有这臭毛病。换jdbc driver版本，改@Lob为 @Type(type = "org.springframework.orm.hibernate3.support.BlobByteArrayType")，改hibernate配置（比如hibernate.jdbc.use_streams_for_binary true），甚至是直接用jdbc来insert，乱七八糟折腾半天，问题照旧。最后只好用上“歪”招，把byte[]配成String，在保存的时候把byte[]保存成hex String格式，取的时候再解码回来。<br /><br />歪招终归是歪招，这两天老为这东西心烦，晚上吃饭的时候无意中想起charset的问题，把代码捡回来再测试了一下，问题解决，哈哈！<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">jdbc.url            </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">    jdbc:mysql://localhost/mmwy_blog\<br />                            ?useUnicode</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">true\<br />                            &amp;characterEncoding</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">utf-</span><span style="color: rgb(0, 0, 0);">8</span></div>如果设成iso-8859-1、utf-8,保存一点问题都没有，换用gbk、gb2312、big5之类的字符集，问题就出来了。<br /><br /><img src ="http://www.blogjava.net/mmwy/aggbug/74800.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mmwy/" target="_blank">mmwy</a> 2006-10-12 15:54 <a href="http://www.blogjava.net/mmwy/archive/2006/10/12/74800.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>