ResultSet.updateRow()为操作'< =>'

时间:2016-03-01 17:57:26

标签: java mysql resultset collation

我的下表中name为LATIN1,其余为UTF8。

CREATE TABLE `test_names` (
  `name` varchar(500) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  `other_stuff_1` int DEFAULT NULL,
  `other_stuff_2` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

我在Java中遇到以下问题:

SELECT ... FOR UPDATE。然后我在其ResultSet上调用updateInt(2, 1)updateRow()并获取Illegal mix of collations (latin1_bin,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '<=>'

如何在不更改表格/连接字符集的情况下完成此工作?

非常感谢。

---更新---

我使用SELECT name, other_stuff_1 FROM test_names LIMIT 1 FOR UPDATE;,连接字符串为DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db + "?allowMultiQueries=true", user, password);

确切的堆栈跟踪是:

java.sql.SQLException: Illegal mix of collations (latin1_bin,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '<=>'
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2834)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2441)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2366)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2350)
    at com.mysql.jdbc.UpdatableResultSet.updateRow(UpdatableResultSet.java:2405)

3 个答案:

答案 0 :(得分:1)

从我这边,我可以给你一些建议

  1. 首先更新您的Connector / J最新版本。
  2. 运行此查询
  3. SET NAMES='utf8'
    
    1. &characterEncoding=UTF-8添加到JDBC连接字符串中。希望你已经完成了。

    2. 使用convert() for insert or updatecast() for select查询。有关详细信息,请http://dev.mysql.com/doc/refman/5.7/en/charset-convert.html

    3. 对于&#34; 非法混合排序&#34; 相关问题,您可以跟进Troubleshooting "Illegal mix of collations" error in mysql

答案 1 :(得分:0)

问题是您尝试从不同列上具有不同字符集的表中进行选择。

您必须使用CONVERT()将查询中具有不同字符集的列转换为正确的字符集。

http://dev.mysql.com/doc/refman/5.7/en/charset-convert.html

在您的情况下,您应该将查询修改为:

SELECT CONVERT(name USING latin1), other_stuff_1 FROM test_names LIMIT 1 FOR UPDATE;

答案 2 :(得分:0)

恕我直言,当表格中的编码不统一时,你不能使用可更新的结果集(例如主键在latin1中,但其他列在utf8中)。

正如Andras所指出的,您可以在sql端转换编码,但之后您将无法更新结果集。

为什么不用executeUpdate(...)简单地更新表?您可以通过简单的选择缩小目标行的范围,然后迭代生成的主键列表并调用executeUpdate。

对我来说,它适用于混合编码列。举个例子:

    conn.setAutoCommit(false);
    ResultSet rs = st.executeQuery("SELECT name other_stuff_1 FROM test_names");
    List<String> keys = new ArrayList<String>();
    while(rs.next()) {
        keys.add(rs.getString(1));
    }
    rs.close();
    for(String key : keys) {
        st.executeUpdate("update test_names set other_stuff_1='"+key.length()+"', other_stuff_2='" + key.toUpperCase() + "' where name='" + key + "'");
    }
    conn.commit();