本人使用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);
}
}
当写为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);
}
}
解决方案 »
- java问题:这个正则表达式^[^$%\\\\]+$是什么意思?
- Spring配置,无效属性
- corba问题
- struts hibernate spring 整合问题
- 邮件附件之问
- 同时装上JBulider和eclipse可以嘛?
- 要用java操纵WORD实现痕迹保留,公文模板,数字签章
- 好久没来,最近总有个想法左右我对Java的想法,Java开发难道只是不断学用别人的技术,而自己一点技术思想都没有吗?
- NetBeans 运行项目 错误:程序包javax.persistence不存在。我已添加相应的jar还会错
- SpringMVC+mybatis+mysql 执行sql语句太慢的问题
- 想做单点登录的功能,但是cookie取不到
- java打印只显示最后1个 g.drawString()
不知道为什么不能进行修改,正确的部分应该是这个,但是在别的程序中均能正常运行,就是这次不可以