开发者

Insert Chinese Character String into MySQL LongBlob column as UTF-8

By doing this:

File file1 = new File("/usr/local/test/multipartMimeMsg.txt");
            Fi开发者_运维问答leOutputStream fos1 = new FileOutputStream(file1);
            String s = byteArrayToHexString(multipartMimeMsg.getBytes());
            fos1.write(s.getBytes(), 0, s.getBytes().length);

I verified that my multipartMimeMsg string can see the hexa representation of the chinese characters. I then proceed to do this:

prepstmt.setObject(2, multipartMimeMsg );

But it turns up as ??? or 3F3F3F in hexa representation. It should show E4BDA0E5A5BDE59097 Am I missing something? I have set my Table Options to Charset utf8, Collation to utf8_general_ci.

Connection string is jdbc:mysql://localhost:3306/test


I'm not sure if setObject() can handle LOBs correctly - have you tried this with setBytes() or setBinaryStream()?

Also, any reason why you're storing text as a BLOB instead of TEXT?


String s = byteArrayToHexString(multipartMimeMsg.getBytes());

I assume multipartMimeMsg is a string. Look carefully about the getBytes() method in JavaDoc, it will use system default character set(depending on your OS) to encode the string into bytes, which can vary on different systems.

The more accurate way is set the exact character set you want, like aString.getBytes("UTF-8").

And for the column definition problem, I recommend using "TEXT" type, as suggested by @Dmitri.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜