posts - 56, comments - 54, trackbacks - 0, articles - 4
             ::  ::  :: 聯(lián)系 :: 聚合  :: 管理

          在一般的日文字符表示正常的情況下正波浪線“~”任然經(jīng)常會(huì)出現(xiàn)亂碼的情況。

          數(shù)據(jù)庫(kù)參數(shù)NLS Database Parameters
          NLS_CHARACTERSET?????????????????? JA16SJIS
          NLS_NCHAR_CHARACTERSET?? AL16UTF16

          一:對(duì)于VARCHAR2字段
          現(xiàn)象:在頁(yè)面上輸入“~”存入DB后再取出到頁(yè)面時(shí)變成“?”(頁(yè)面的字符設(shè)置為charset=shift-jis)
          調(diào)查:
          1 頁(yè)面提交后在java中觀察編碼為\uff5e
          2 java中定義一個(gè)字符變量“~”觀察編碼為\uff5e
          3 編碼為\uff5e的字符存入數(shù)據(jù)庫(kù)再取出的編碼為\u301c
          4?用Object Browser觀察數(shù)據(jù)庫(kù)中字符都正常
          結(jié)論:存入數(shù)據(jù)庫(kù)時(shí)編碼發(fā)生了變化\uff5e ---> \u301c
          解決:取出數(shù)據(jù)時(shí)遍歷發(fā)現(xiàn)\u301c就轉(zhuǎn)換為\uff5e
          ????? public String getString(int columnIndex) throws SQLException {
          ???????? // TODO: ~を変更
          ???????? String value = rs.getString(columnIndex);
          ???????? if (value != null){
          ????????????? StringBuffer sbDest = new StringBuffer();
          ????????????? char ch;
          ????????????? for(int j= 0;j< value.length();j++){
          ????????????????? ch = value.charAt(j);
          ????????????????? if(ch == 0x301c){
          ????????????????????? sbDest.append("\uff5e"); // ~
          ????????????????? }else{
          ????????????????????? sbDest.append(ch);
          ????????????????? }
          ????????????? }
          ????????????? value = sbDest.toString();
          ???????? }

          ???????? return value;
          ???? }

          二:對(duì)于NVARCHAR2字段
          現(xiàn)象:在頁(yè)面上輸入正波浪線“~”存入DB后再取出到頁(yè)面時(shí)變成反波浪線“?”(頁(yè)面的字符設(shè)置為charset=UTF-8)
          用Object Browser工具察看表中的數(shù)據(jù) 發(fā)現(xiàn)是正波浪線“~” 編碼為\u301c。(實(shí)際上用Object Browser看\u301c? \uff5e都是正波浪線)
          如果用NVARCHAR2字段,那么正確的操作后可以存入\uff5e

          試驗(yàn)用表?Products ,表中的列定義如下所示
          id - VARCHAR2(10) — 產(chǎn)品 id
          lang_id — VARCHAR2(10) — 語(yǔ)言 id
          description — NVARCHAR2(2000) — Unicode 編碼的產(chǎn)品描述

          JDBC 允許 Java 程序訪問(wèn) Oracle9i 數(shù)據(jù)庫(kù)中的 NVARCHAR2 數(shù)據(jù)類型的列。Oracle JDBC 驅(qū)動(dòng)程序把 SQL NCHAR/NVARCHAR2 列中的數(shù)據(jù)從本地字符集編碼(UTF8 或 AL16UTF16)直接轉(zhuǎn)化為 UTF-16 編碼的 Java 字符串。
          為此,我們需要把 Java 字符串綁定到一個(gè) NVARCHAR2 列。下面的代碼段顯示了完成這項(xiàng)任務(wù)的代碼。

          // Get an Oracle preparedstatement
          OraclePreparedStatement orastmt =(OraclePreparedStatement)connection.prepareStatement(
          "INSERT INTO PRODUCTS VALUES(?,?,?)");
          // Bind the 3rd parameter to NVARCHAR2 form so that the data is stored as unicode
          orastmt.setFormOfUse(3,OraclePreparedStatement.FORM_NCHAR);
          orastmt.setString(1,product.getId());
          orastmt.setString(2,product.getLangId());
          orastmt.setString(3,product.getDescription());
          orastmt.executeUpdate();
          orastmt.close();

          使用 orastmt.setFormOfUse() 方法來(lái)指定列的類型是 NVARCHAR2。確保數(shù)據(jù)以Unicode 編碼存儲(chǔ)。

          關(guān)于NVARCHAR2字段的操作參考:
          http://www.oracle.com/technology/sample_code/tech/java/sqlj_jdbc/files/9i_jdbc/NCHARsupport4UnicodeSample/Readme.html

          注意這段話:
          The only difference in usage between the SQL CHAR and SQL NCHAR datatypes occur in a data bind situation.
          The JDBC program must call the setFormOfUse() method to specify if the data is bound for a SQL NCHAR
          datatype and it must be called before binding Java variables to SQL NCHAR datatypes.
          必須先setFormOfUse再綁定變量。否則存入數(shù)據(jù)庫(kù)中的仍是\u301c

          主站蜘蛛池模板: 邵东县| 绥棱县| 邵阳县| 舒城县| 长武县| 福鼎市| 乐山市| 宣汉县| 贡觉县| 屯昌县| 龙州县| 高唐县| 孟津县| 西乌珠穆沁旗| 东平县| 平乡县| 九寨沟县| 西华县| 泰州市| 云龙县| 万年县| 定西市| 茌平县| 乌审旗| 大冶市| 高台县| 丰镇市| 广昌县| 古丈县| 天气| 浙江省| 中方县| 辰溪县| 尼木县| 安康市| 岗巴县| 湛江市| 监利县| 丹阳市| 灯塔市| 木里|