本人使用JSP读取ORACLE数据库的时候,出现以下问题
当写为stu.setStuname(rs.getString("stuname"));的时候,页面从数据库中读取的汉字内容乱码,数据库的汉字是正常显示。
当修改为stu.setStuname(rs.getString(StringUtil.convertEncode(rs.getString("stuname"),StringUtil.ISO8859_1,StringUtil.GBK)));的时候,则提示java.sql.SQLException: 列名无效该段代码,在别的程序中执行没有提示该问题,但是只有这个程序有这个问题。不知道该如何修改
以下是有关汉字转换的代码
package com.jdbc;import java.io.UnsupportedEncodingException;
import java.util.UUID;public class StringUtil {
/**页记录数**/
private static int pageSize = 20;
/**SQL语 句显示控制位**/
private static boolean sqlShow = true;
/** GB2312字符集编码 */
public final static String GB2312 = "GB2312"; /** GBK字符集编码 */
public final static String GBK = "GBK"; /** ISO8859-1字符集编码 */
public final static String ISO8859_1 = "ISO8859-1"; /** UTF-8字符集编码 */
public final static String UTF8 = "UTF-8"; private final static int GB_SP_DIFF = 160;
/** 首字母区位码 */
private final static int[] InitialCode = { 1601, 1637, 1833, 2078, 2274,
2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,
4027, 4086, 4390, 4558, 4684, 4925, 5249, 5600 };
/** 字母表常量 */
private final static char[] Initial = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w',
'x', 'y', 'z' }; private StringUtil() {
} public static String getPK() {
return UUID.randomUUID().toString();
}

public static String getPK(String str){
str = str+":"+UUID.randomUUID().toString();
return str;
} /**
 * 转换字符串字符集方法
 * 
 * @param arg
 *            转换字符集字符串
 * @param oldEncode
 *            字符串原始字符集编码
 * @param newEncode
 *            字符串转码后字符集编码
 * @return String
 *         <li>转换字符集失败则返回null</li>
 */
public static String convertEncode(String arg, String oldEncode,
String newEncode) { try { byte[] byteTmp = arg.getBytes(oldEncode); return new String(byteTmp, newEncode); } catch (UnsupportedEncodingException e) {
return null;
}
} public static int getPageSize() {
return pageSize;
} public static void getSqlShow(String sql) {
if (sqlShow) {
System.out.println(sql);
}
} /**
 * 中文词组转拼音首字母串
 */
public static String getInitial(String arg) { arg = arg.toLowerCase(); char[] charTmp = arg.toCharArray(); byte[] uniCode = null; StringBuffer rtnInitial = new StringBuffer(); for (int i = 0; i < charTmp.length; i++) { uniCode = new String(String.valueOf(charTmp[i])).getBytes(); if (uniCode[0] < 128 && uniCode[0] > 0) {
// 非汉字
rtnInitial.append(charTmp[i]); } else { // 汉字
rtnInitial.append(StringUtil.getInitial(uniCode));
} } return rtnInitial.toString().toUpperCase();
} public static String getInitial(byte[] uniCode) { char rtnChar = '?'; for (int i = 0; i < uniCode.length; i++) {
uniCode[i] -= GB_SP_DIFF;
} int secPosValue = uniCode[0] * 100 + uniCode[1]; for (int i = 0; i < 23; i++) { if (secPosValue >= InitialCode[i]
&& secPosValue < InitialCode[i + 1]) { rtnChar = Initial[i]; break;
}
} return String.valueOf(rtnChar);
}
}