2013-12-23 36 views
0

开始之前,对不起我的英文。MySQL数据库编码

我有一个MySQL的问题。我创建数据库,并将编码字符串更改为latin2。从Java查询:

`INSERT INTO infos VALUES (NULL, '100001486491198', 'ęłóżźćść[email protected]#', '2013-12-23 11:24:37', 0, 0, 0);` 

在数据库中看起来像“???????”。我在phpMyAdmin中将此字符串更改为targen,并且当我执行SELECT查询时,我得到相应的字符串作为响应。

而且我这个代码添加到数据库连接:

 Properties properties = new Properties(); 
     properties.put("user", DBUSER); 
     properties.put("password", DBPASS); 
     properties.put("characterEncoding", "ISO-8859-2"); 
     properties.put("useUnicode", "true"); 

    try { 
     Class.forName(DBDRIVER).newInstance(); 
     connection = DriverManager.getConnection(DBURL, properties); 
     statement = connection.createStatement(); 
    } 
    catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) { 
     e.printStackTrace(); 
    } 

但作为回应,我得到错误:

java.sql.SQLException: Incorrect string value: '\xB3\xF3\xBF\xBC\xE6\xBF...' for column 'content' at row 1 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2812) 
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811) 
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725) 
    at com.mpkinfo.server.database.Database.add(Database.java:48) 
    ... 

什么,我做错了什么?

+0

我想你应该选择UTF8斌,编码你所有的mysql表格。 –

回答

0

如果使用unicode字符,请将所有表和文件编码设置为utf-8。

+0

它有帮助,谢谢。 – kris14an

0

我会建议你使用UTF8,但是如果你坚持ISO-8859-2,你有你的DBURL改变模样:

jdbc:mysql://localhost:3306/yourDatabase?useUnicode=falsee&characterEncoding=ISO-8859-2 
+0

我改变之前,同样的回应。但是UTF-8有帮助。谢谢 – kris14an

+0

如果utf有帮助,请使用:jdbc:mysql:// localhost:3306/yourDatabase?useUnicode = true&characterEncoding = utf-8 – user987339

+0

在URL中设置属性和使用Properties类有什么不同? – kris14an