java mysql utf8,Java + Mysql UTF8问题

as the title said, I have a problem between java and mysql

The mysql DB, tables, and columns are utf8_unicode_ci.

I have an application that took some input from an xml, then compose the query...

public String [] saveField(String xmltag, String lang){

NodeList nodo = this.doc.getElementsByTagName(xmltag);

String [] pos = new String[nodo.getLength()];

for (int i = 0 ; i < nodo.getLength() ; i++ ) {

Node child = nodo.item(i);

pos[i] = "INSERT INTO table (id, lang, value) VALUES (" +

child.getAttributes().getNamedItem("id").getNodeValue().toString() + " , " +

lang + " , " +

"'" + child.getFirstChild().getTextContent() + "'" +

");";

}

return pos;

}

this method return an array of String that contains one or more SQL insert Query...

then

Class.forName("com.mysql.jdbc.Driver").newInstance();

con = DriverManager.getConnection("jdbc:mysql:///dbname", "user", "pass");

.....

Statement s; s =

this.con.createStatement ();

s.execute(query);

both with s.execyte and s.executeUpdate the special characters are stored as ?

so special char are not stored correctly:

מסירות קצרות is stored as ?????????

Hi! is stored as Hi!

Any advice?

Thanks

解决方案

Solved,

I forgot to add the encoding when initializing Connection:

before was:

con = DriverManager.getConnection("jdbc:mysql:///dbname", "user", "pass");

now (working):

con = DriverManager.getConnection("jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8", "user", "pass");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值