如何从 table in mysql 中正确获取包含意大利语、日语和俄语字符的数据

how to fetch correctly the data from the a table in mysql which contains Italian,Japanese and Russian characters

我有一个带有 UTF8 字符集的 table。这些列被视为 utf8_general_ci。我正在使用准备好的语句读取数据,但它们没有正确显示。 table 中的数据也不可读。我需要以人类可读的方式编写代码。我测试了很多方法都失败了。 对于连接 属性 我使用了 "?useUnicode=true&characterEncoding=UTF8";

String city=resultset.getString("city");
byte[] data = city.getBytes();
String valueCity = new String(data, "UTF-8"); // Or  String valueCity = new String(data, StandardCharsets.UTF_8);

我看到类似“名 ; & #21476 ;& #23627; & #24066;”的内容在我的 table 但我需要像名古屋市一样阅读或编写它们。 有什么建议可以解决这个让我脖子痛的问题吗? 提前一百万致谢

也许是 resultset.getString("city") 你的问题是什么。您已经收到字符串形式的数据。该字符串的字节表示可能不是 utf-8。结果集的类型是什么?

您确定您使用 characterEncoding=utf8 打开了数据库连接吗?您需要设置 connectionProperties="useUnicode=yes;characterEncoding=utf8;"

Whosebug

首先检查 resultset.getBytes(..) 而不是 getString 可能会有所帮助

终于找到代码了:

public static String unescapeXML( final String xml )
{
    Pattern xmlEntityRegex = Pattern.compile( "&(#?)([^;]+);" );
    // Matcher requires a StringBuffer instead of a StringBuilder
    StringBuffer unescapedOutput = new StringBuffer( xml.length() );

    Matcher m = xmlEntityRegex.matcher( xml );
    Map<String,String> builtinEntities = null;
    String entity;
    String hashmark;
    String ent;
    int code;
    while ( m.find() ) {
        ent = m.group(2);
        hashmark = m.group(1);
        if ( (hashmark != null) && (hashmark.length() > 0) ) {
            code = Integer.parseInt( ent );
            entity = Character.toString( (char) code );
        } else {
            //must be a non-numerical entity
            if ( builtinEntities == null ) {
                builtinEntities = buildBuiltinXMLEntityMap();
            }
            entity = builtinEntities.get( ent );
            if ( entity == null ) {
                //not a known entity - ignore it
                entity = "&" + ent + ';';
            }
        }
        m.appendReplacement( unescapedOutput, entity );
    }
    m.appendTail( unescapedOutput );

    return unescapedOutput.toString();
}

private static Map<String,String> buildBuiltinXMLEntityMap()
{
    Map<String,String> entities = new HashMap<String,String>(10);
    entities.put( "lt", "<" );
    entities.put( "gt", ">" );
    entities.put( "amp", "&" );
    entities.put( "apos", "'" );
    entities.put( "quot", "\"" );
    return entities;
}

某些东西,不是MySQL,正在生成“html 个实体”,例如&#21517;。找到那些来自哪里并撤消它。

由于这些实体可能已经存储在 table 中,因此也需要撤消。

html 实体应在任何浏览器中正确呈现。您是否尝试在其他情况下使用它们?