PreparedStatement
具我所知,是欲编译的,所以已经很好的解决中文问题了,你看看是不是不是中文问题啊。

解决方案 »

  1.   

    我也认为是中文问题,把
    String newName = new String(name.getBytes("GBK"), "ISO-8859-1");
    换成
    String newName = new String("111111111111111");
    试试,要是程序编译通过的话,就是中文问题,
      

  2.   

    将U 
    nicode转换为ASCII形式,即一个汉字拆成两个char,用到两个类ByteToCharConve 
    rter和CharToByteConverter,在sun.io.*中,可以在JDK home\jre\lib\i18n.jar 
    中找到,没有文档,我也实在网上找到用法的. 
    在输入时,用ChineseStringToAscii转换为ASCII码字串,查询时用AsciiToChines 
    eString转换为Unicode字串.源代码如下: 
    public static String AsciiToChineseString(String s) { 
        char[] orig = s.toCharArray(); 
        byte[] dest = new byte[orig.length]; 
        for (int i=0;i<orig.length;i++) 
          dest[i] = (byte)(orig[i]&0xFF); 
        try { 
          ByteToCharConverter toChar = ByteToCharConverter.getConverter("g 
    b2312"); 
          return new String(toChar.convertAll(dest)); 
        } 
        catch (Exception e) { 
          System.out.println(e); 
          return s; 
        } 
      }   public static String ChineseStringToAscii(String s) { 
        try { 
          CharToByteConverter toByte = CharToByteConverter.getConverter("g 
    b2312"); 
          byte[] orig = toByte.convertAll(s.toCharArray()); 
          char[] dest = new char[orig.length]; 
          for (int i=0;i<orig.length;i++) 
            dest[i] = (char)(orig[i] & 0xFF); 
          return new String(dest); 
        } 
        catch (Exception e) { 
          System.out.println(e); 
          return s; 
        } 
      } 
    看看行不行
      

  3.   

    是中文问题。在读取和写入中文的时候,我使用
    new String(string.getBytes("GBK"), "ISO-8895-1"); //写入的时候转码  

    new String(string.getBytes("ISO-8859-1"), "GBK"); //读取的时候转码来处理。//-----------------------------------
    这是我在读取DB时的代码片断:
    pstmt = conn.prepareStatement("select xxx from ... ");
    rs = pstmt.excuteQuery();
    String xxx = new String(rs.get("xxx").getBytes("ISO-8859-1"), "GBK");运行和显示都正常。
    但是如果rs改成可更新的时候,则是乱码:
    pstmt = conn.prepareStatement("select xxx from ...", ResultSet.ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    rs = pstmt.excuteQuery();
    String xxx = new String(rs.get("xxx").getBytes("ISO-8859-1"), "GBK");运行正常,但是显示是乱码。//----------------------------------------------
    这是我一般用来写入DB时的代码片断:
    pstmt = conn.prepareStatement("update ... set xxx = ? where ...");
    pstmt.setString(1, new String(string.getBytes("GBK"), "ISO-8895-1"));
    pstmt.executeUpdate();运行正常。后来通过读取来测试,写入也正常。我如果想改成:
    pstmt = conn.prepareStatement("select xxx from ...", ResultSet.ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    if(rs.next()){
      rs.updateString("xxx", new String(string.getBytes("GBK"), "ISO-8895-1"));   //这个地方则会抛出Cannot map Unicode to Oracle Character的异常
      rs.updateRow();
    }我简直有点怀疑这是oracle jdbc的一个bug。
    感谢楼上三位,按照sunni说的转码成ascii之后以updateString的方式插入还是会抛异常。