3

当我使用 sql insert 将数据(包括法语字符)插入数据库时​​,它工作正常:

   CREATE TABLE Categories(code_interne_c varchar(100) primary key,
                           description    varchar(100));

   INSERT INTO Categories  VALUES ('01','Matières premières');
   INSERT INTO Categories  VALUES ('02','Articles de conditionnement');
   INSERT INTO Categories  VALUES ('03','Consommables production');
   INSERT INTO Categories  VALUES ('04','Produits de nettoyage');
   INSERT INTO Categories  VALUES ('05','Réactifs');

当我使用 mysql 命令行时,它也可以正常工作(如您所见,它是正常的和预期的输出):

   mysql> select * from categories;
   +----------------+-------------------------------------------+
   | code_interne_c | description                               |
   +----------------+-------------------------------------------+
   | 01             | Matières premières                        |
   | 02             | Articles de conditionnement               |
   | 03             | Consommables production                   |
   | 04             | Produits de nettoyage                     |
   | 05             | Réactifs                                  |
   +----------------+-------------------------------------------+
   14 rows in set (0.00 sec)

但是当我使用jdbc检索数据时 :

public static String testreqchar(){
    try {
        Statement st = cx.createStatement();
        ResultSet rs = st.executeQuery("select * from categories");
        rs.next();
        return rs.getString(2)+"|| and  test French accent characters : é ô ê à è û ";
    } catch (SQLException ex) {
        Logger.getLogger(Methodes.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}
public static void main(String a[]) {

           System.out.println(testreqchar());
}

这是我在 IDE 和接口中得到的输出:

    MatiŠres premiŠres|| and  test French accent characters : é ô ê à è û

我不知道是什么问题。

   Mysql 5.5.27 set on UTF-8 as charset on Win server 2008 R2 French
   mysql-connector-java-5.1.21
   JDK 1.7 Running on win 7 pro French.
4

1 回答 1

2

放在useUnicode=true&characterEncoding=UTF8连接字符串的末尾。见样品,

      Connection conn = DriverManager.getConnection  
        ( "jdbc:mysql://localhost/bug_utf8?useUnicode=true&characterEncoding=UTF8"  
于 2012-09-07T11:32:35.617 回答