MySQL - 问号

时间:2013-07-31 11:04:58

标签: java mysql utf-8 character-encoding collation

从我的应用程序插入一些中文字符时,它们会被写入“???”。不用说,它在内置的命令行mysql客户端中运行良好。

连接字符串:

  

- user-db-uri = jdbc:mysql:// localhost / tigasedb?user = tigase_user& password = tigase_passwd& useUnicode = true& characterEncoding = utf8& noAccessToProcedureBodies = true

代码:

try {
            conn_valid_st.setQueryTimeout(query_timeout);
            st = conn.prepareStatement("SET character_set_client = utf8");
            st.execute();
            st.close();
            st = conn.prepareStatement("SET character_set_connection = utf8");
            st.execute();
            st.close();
            st = conn.prepareStatement("SET character_set_results = utf8");
            st.execute();
            st.close();
            st = conn.prepareStatement("SET collation_connection = utf8_general_ci");
            st.execute();
            st.close();
            st = conn.prepareStatement("SET NAMES utf8");
            st.execute();
            st.close();
            st = conn.prepareStatement("SET CHARACTER SET utf8");
            st.execute();
            st.close();
        } catch (SQLException ex) {
            // Ignore for now, it seems that PostgreSQL does not support this method
            // call yet
            if (null != st)
                st.close();

            log.log(Level.WARNING, "DB server said: {0}", ex.toString());
        }

是什么让我失望?

编辑:

  

创建表... ENGINE = InnoDB默认字符集utf8整理   utf8_unicode_ci ROW_FORMAT = DYNAMIC;

插入字符:在健身房

验证由内置命令行mysql客户端完成。

编辑: http://docs.oracle.com/cd/E17952_01/refman-5.0-en/connector-j-reference-charsets.html没有帮助

  

检查系统属性“file.encoding”的值。如果不是这样的话   “UTF-8”,那么你需要明确指定“UTF-8”作为字符   每当您将字节解码为字符时编码。例如,何时   您使用byte []调用String构造函数,或使用   InputStreamReader中。

Problems reading/writing UTF-8 data in MySQL from Java using JDBC connector 5.1 - 没有帮助

将存储过程中的字符串变量声明为_loc VARCHAR(128) CHARSET utf8 - 没有帮助

2 个答案:

答案 0 :(得分:0)

应使用正确的字符集和排序规则创建数据库表列。您可以在创建或更改表时定义该项:

col_name {CHAR | VARCHAR | TEXT} (col_length)
    [CHARACTER SET charset_name]
    [COLLATE collation_name]

这是帮助您了解更多信息的mysql链接:

http://dev.mysql.com/doc/refman/5.0/en/charset-column.html

答案 1 :(得分:0)

它适用于当前的5.1.26驱动程序版本:

createTable("testBugF1", "(`str` varchar(10) ) default character set utf8 collate utf8_unicode_ci ");
String str = "在健身房";

Properties props = new Properties();
props.put("characterEncoding", "utf8");
Connection c = getConnectionWithProps(props);

this.stmt = c.createStatement();
this.stmt.executeUpdate("insert into testBugF1 values (\""+ str +"\")");
this.rs = this.stmt.executeQuery("select * from testBugF1");
assertTrue(this.rs.next());
String res = this.rs.getString(1);
System.out.println(res);
assertEquals(str, res);

你使用旧司机吗?你如何插入字符?