开发者社区> 问答> 正文

JDBC MySQL UTF-8字符串编写问题?mysql

连接到数据库:

public DBSource(ConnectionInfo ci) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { Class.forName("com.mysql.jdbc.Driver").newInstance(); String dbPath = String.format( "jdbc:mysql://%s:%d/%s?user=%s&password=%s&characterEncoding=utf-8&" + "useUnicode=true", ci.host, ci.port, ci.dbName, ci.user, ci.password); conn = java.sql.DriverManager.getConnection(dbPath); prepareTables(); } 表创建代码:

private void prepareTables() throws SQLException { java.sql.Statement stat = conn.createStatement();

String query = "set names utf8";
stat.execute(query);

query = "set character set utf8";
stat.execute(query);

query = "show variables like '%char%'";
stat.execute(query);
java.sql.ResultSet rs = stat.getResultSet();
while (rs.next())
{
    String k = rs.getString(1);
    String v = rs.getString(2);
    System.out.println(k + " - " + v);
}

query = "drop table if exists clt";
stat.execute(query);
query = "create table clt"
        + "("
        + "  id bigint not null"
        + ", text varchar(50) not null"
        + ") default character set utf8";
stat.execute(query);

} 行插入:

public void visit(Insert i) throws SQLException { String query = "insert into clt" + " (id, text) values (?, ?)"; java.sql.PreparedStatement stmt = conn.prepareStatement(query); if (i.rowData.id == 12656697) { String toOut = "<<< " + Long.toString(i.rowData.id) + " - " + i.rowData.text; System.out.println(toOut); } int it = 0; stmt.setLong(++it, i.rowData.id); stmt.setString(++it, i.rowData.text); stmt.execute(); stmt.close(); } 检查数据:

public void checkText() throws SQLException { java.sql.Statement stmt = conn.createStatement(); String query = "select id, text from clt where id = '12656697'"; stmt.execute(query); java.sql.ResultSet rs = stmt.getResultSet(); while (rs.next()) { String k = rs.getString(1); String v = rs.getString(2); String toOut = ">>> " + k + " - " + v; System.out.println(toOut); } } 输出:

character_set_client - utf8 character_set_connection - latin1 character_set_database - latin1 character_set_filesystem - binary character_set_results - utf8 character_set_server - utf8 character_set_system - utf8 character_sets_dir - /usr/share/mysql/charsets/

<<< 12656697 - Апарати

12656697 - ??????? 问题:在表中,文本字段中有“ ??????????? ”符号。

预期的字符串是:Апарати 结果:???????

我解决了一个问题,这是一种魔术 。但是如果smbd可以向我解释它,仍然会感激不尽。 所以。

我在Costis Aivalis建议的/etc/mysql/my.cnf行中添加 了相同的结果 我从代码中删除了以下行: query =“ set character set utf8”; stat.execute(查询); 这是工作!!!:)

展开
收起
保持可爱mmm 2020-05-17 22:36:00 34887 0
1 条回答
写回答
取消 提交回答
  • 确保正确定义了MySQL配置编码。使用以下命令检查设置和修改的正确性:

    show variables like 'character%'; 和 show variables like 'collation%';

    将这些行添加到my.cnf或my.ini中:

    对于MySQL 5.1.nn和更高版本5.5.29,您只需要这两行:

    [mysqld] character-set-server = utf8 character-set-filesystem = utf8 对于MySQL 5.0.nn和更早版本,请使用以下设置:

    [client] default-character-set=utf8

    [mysql] default-character-set=utf8

    [mysqld] default-character-set=utf8 character-set-server=utf8 使用MySQL-Workbench进行设置可能更方便。5+版本非常好。

    在此处输入图片说明

    在您的Java程序中按以下方式连接:

    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabase?useUnicode=tr来源:stack overflow

    2020-05-17 22:39:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像