PreparedStatement
具我所知,是欲编译的,所以已经很好的解决中文问题了,你看看是不是不是中文问题啊。
具我所知,是欲编译的,所以已经很好的解决中文问题了,你看看是不是不是中文问题啊。
解决方案 »
- socket 编程有点含糊
- 双向回环链表的实现.
- flex得到安全策略文件后不发送socket请求,要发送两次
- 关于org.omg.cwm.objectmodel.core.*API的下载地址
- 大家帮帮我看看,我错在什么地方了?
- 谁能帮帮我,谢谢,分少了可以再给!
- new一个类为什么要加上this
- 我想下一个weblogic7,可在bea的网站上看到了那么多版本,不知道该下哪一个,帮忙~~~
- 【白送分】
- 不允许使用自关闭元素
- 用JAVAMAIL如何设置content-type等参数??急
- 请问我接受到的这种编码是什么,如果转化成汉字 (%C4%FA%B5%C4%CA%D6%BB%FA%BA%C5%C2%EB%D4%CB)
String newName = new String(name.getBytes("GBK"), "ISO-8859-1");
换成
String newName = new String("111111111111111");
试试,要是程序编译通过的话,就是中文问题,
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;
}
}
看看行不行
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的方式插入还是会抛异常。