2016-08-02 54 views
0

這是我當前的字符集。我試圖插入拉丁文字符,我不能得到它的工作。mysql jdbc驅動程序不識別latin1字符

SHOW VARIABLES LIKE 'char%'; 
+--------------------------+-----------------------------------+ 
| Variable_name   | Value        | 
+--------------------------+-----------------------------------+ 
| character_set_client  | latin1       | 
| character_set_connection | latin1       | 
| character_set_database | utf8        | 
| character_set_filesystem | binary       | 
| character_set_results | latin1       | 
| character_set_server  | latin1       | 
| character_set_system  | utf8        | 
| character_sets_dir  | /space/xyz/mysql/share/charsets/ | 
+--------------------------+-----------------------------------+ 

數據庫配置不是由我創建的,所以我不能更改字符集或整理。

show create table t1; 
+-------+-------------------------------------------------------------------------------------------------------------------------+ 
| Table | Create Table                           | 
+-------+-------------------------------------------------------------------------------------------------------------------------+ 
| t1 | CREATE TABLE `t1` (
    `col1` text CHARACTER SET latin1 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs | 
+-------+-------------------------------------------------------------------------------------------------------------------------+ 

以下是插入帶有unicode字符的名稱的簡單代碼。

private void insert() { 
     try { 
      loadJdbcProperties(); 
      String myquery = "INSERT INTO db.t1(col1) VALUES (?)"; 
      String url = "jdbc:mysql://localhost:3307/db?useUnicode=true&characterEncoding=Cp1252"; 
      Properties prop = new Properties(); 
      prop.put("user", "root"); 
      prop.put("password", "root"); 
      if (_jdbcProperties != null && !_jdbcProperties.isEmpty()) { 
       prop.putAll(_jdbcProperties); 
      } 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      Connection c = DriverManager.getConnection(url, prop); 

      PreparedStatement pstmt = null; 
      String sql = null; 

      sql = myquery; 
      pstmt = c.prepareStatement(sql); 
      pstmt.setString(1, "csvźà.py"); 
      pstmt.execute(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

這是檢索代碼。

private void showResults() { 
     try { 
      loadJdbcProperties(); 
      String myquery = "select col1 from db.t1"; 
      String url = "jdbc:mysql://localhost:3307/db?useUnicode=true&characterEncoding=Cp1252"; 

      Properties prop = new Properties(); 
      prop.put("user", "root"); 
      prop.put("password", "root"); 
      if (_jdbcProperties != null && !_jdbcProperties.isEmpty()) { 
       prop.putAll(_jdbcProperties); 
      } 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      Connection c = DriverManager.getConnection(url, prop); 

      PreparedStatement pstmt = null; 
      String sql = null; 
      ResultSet rs = null; 

      sql = myquery; 
      pstmt = c.prepareStatement(sql); 
      rs = pstmt.executeQuery(); 
      while (rs.next()) { 
       System.out.println(rs.getString("col1")); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

下面是結果:

csv?à.py 
+0

這是因爲根據MySQL文檔,latin1的Java風格字符編碼名稱是Cp1252。 https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html – user3566773

回答

0

ź的字符(Unicode代碼點U+017A)不包括在latin1字符集,因此MySQL不能將它存儲在一個latin1柱。 (根據記錄,這不是在Windows-1252字符集,無論是。)

如果你真的需要這個角色存儲在該列你要麼需要

  • 改變列的字符集utf8
  • 拿出你自己的方式來手動編碼和解碼字符串,因此它可以被存儲爲latin1的字符(例如,像csv\u017aà.py

和調整根據您的代碼LY。

+0

我明白這一點。但是,有趣的是,如果我使用MariaDB驅動程序而不是MySQL驅動程序來保持一切,它就可以工作。我可以存儲這個名稱並將其清理乾淨。的Class.forName( 「org.mariadb.jdbc.Driver」)的newInstance(); – user3566773

+1

「.java」源文件的字符編碼是什麼? –