我已经看过这个话题:Unknown initial character set index '255' received from server但我没有 pom.xml,我使用 .jar 作为依赖项。我已经在我的项目中设置了最近和/或当前的 MySQL 连接器,但异常仍然存在。我正在使用 MySQL 8.0这在我将 MySQL 5 更新到 8 后开始发生这是我的连接类:package br.com.sat.util;import java.sql.Connection;import java.sql.*;public class ConnectionFactory { public static Connection getConnection() throws Exception { try { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection("jdbc:mysql://localhost:3306/test_wim", "root", "*******"); } catch (Exception e) { throw new Exception(e.getMessage()); } } public static void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs) throws Exception { close(conn, ps, rs); } public static void closeConnection(Connection conn, PreparedStatement ps) throws Exception { close(conn, ps, null); } private static void close(Connection conn, PreparedStatement ps, ResultSet rs) throws Exception { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (Exception e) { throw new Exception(e.getMessage()); } }}
3 回答
萧十郎
TA贡献1815条经验 获得超13个赞
我找到了解决该问题的另一种方法:
在 Connection 类中更改:
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test_wim",
"root", "*******");至:
return DriverManager.getConnection( "jdbc:mysql://localhost:3306/test_wim?characterEncoding=latin1&autoReconnect=true&useSSL=false&useTimezone=true&serverTimezone=UTC", "root", "*******");
这里重要的是:
characterEncoding=latin1
跃然一笑
TA贡献1826条经验 获得超6个赞
该错误与 MySQL 8.0 中默认字符编码的更改有关,因此使用不理解新编码的旧连接器将失败。如果您已更新到最新的 MySQL Connector/J(8.0 版),您还必须进行一些更改。其中之一是类名已更改为 com.mysql.cj.jdbc.Driver,否则您可能仍使用旧驱动程序。
另请参阅 Connector/J 的 MySQL 升级文档: https ://dev.mysql.com/doc/connector-j/8.0/en/connector-j-upgrading-to-8.0.html
哈士奇WWW
TA贡献1799条经验 获得超6个赞
要解决此问题,您可以在管理部分的选项文件中更改 mysql 工作台中的数据库设置。
对于 character-set-server,将值更改为 latin1,将 collation-server 更改为 latin1_swedish_ci。

添加回答
举报
0/150
提交
取消
