给你一个字符转换函数!在插入中文的地方调用它!
/**** 字符集的转换函数 ****/
// added by baiqing 2003.02.18
public static String charConvert(String strSource){
if (strSource == null){
return "";
}
else{
try{
//return new String(strSource.getBytes("GB2312"), "ISO-8859-1");
String str = new String(strSource.getBytes("ISO-8859-1"), "GB2312");
System.out.println(str);
return str;
}
catch (Exception ex){
System.out.println(ex.toString());
return "";
}
}
}
/**** 字符集的转换函数 ****/
// added by baiqing 2003.02.18
public static String charConvert(String strSource){
if (strSource == null){
return "";
}
else{
try{
//return new String(strSource.getBytes("GB2312"), "ISO-8859-1");
String str = new String(strSource.getBytes("ISO-8859-1"), "GB2312");
System.out.println(str);
return str;
}
catch (Exception ex){
System.out.println(ex.toString());
return "";
}
}
}
解决方案 »
- Connection aborted by peer 异常,求高手
- 关于汉字编码问题,在线等!分不够继续加。
- 根据ip地址 获取周边信息 有什么技术能实现 最好是基于Java或Flex的开源
- 请问高手,怎么在新建的工程里面运用数据连接池啊,要怎么搞
- 请问如何输出html标签
- JAVA
- 江湖救急!全分奉送!谁有structs 实现的网上商城的代码?
- 怎么回事,我发的所有帖子都不见了?
- 如何在jsp和servlet中传递bean?????
- RedHat 7.2 上有没有比较好的JSP,Java IDE???(netbeans太慢)????
- 怎么用jsp读取.txt文件,并把数据存进数据库
- 重金求JSP语法手册~
<%request.setCharacterEncoding("GBK")%><%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.sql.*" %>
<%//@ page import="javax.servlet.*" %>
<%//@ page import="javax.servlet.http.*" %>
1:在jsp页面加入:
<%@ page contentType="text/html; charset=gb2312" %>
2:在servlet里面:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");//这是重要的
3:上面的如果在不行就用如下的方法在数据入库前进行调用:
public static String UnicodeToChinese(String s){
try{
if(s==null ¦ ¦s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
public static String ChineseToUnicode(String s){
try{
if(s==null ¦ ¦s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
<%@ page language="java" contentType="text/html; charset=GB2312">(注意中间的空格)……
将下面这个改一下就行了。
String url = "jdbc:mysql://localhost:3306/info?useUnicode=true&characterEncoding=GB2312";
String url = "jdbc:mysql://localhost:3306/info?useUnicode=true&characterEncoding=GBK";注意不是:String url = "jdbc:mysql://localhost:3306/info?useUnicode=true&characterEncoding=8859_1";