给你一个字符转换函数,可以在插入汉字的地方调用它,它插入到数据库之前调用
/**** 字符集的转换函数 ****/
// 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 "";
}
}
}
解决方案 »
- 搜索
- 时间每次加5分钟
- 关于AJAX和JSP的一些小问题~
- 菜鸟关于HIBERNATE N-1单向关联查询的问题
- hibernate中一对多中set集合赋值问题!
- 帮个忙,不知道怎样将此JSP代码转化为C标签代码!谢谢!:)在线等哦!!
- help me!!!有分的[100分]
- 刚学struts2--在form表单中有这样的写法:<input name="user.name" > 请问而这里的user.name是什么意思?这样写的好处!
- 页面上输入一个日期,怎么把他存入到MySQL 数据库中的datestamp类型字段中
- 谁能帮我解决一个小问题?
- 收集java程序员面试题(最好有答案)。
- apache+tomcat整和一问
java一般都是用的iso8859-1编码,而汉字用的编码方式是gb2312
所以要解决你的问题,就是要统一你存入数据库的字符的编码
如:
在存入前可以将String s转换为iso8859-1的编码方式:
new String(s.getBytes(),"iso8859-1");
然后在取出时改变到对应的编码方式,汉字就用gb2312:
new String(s.getBytes("iso8859-1"),"gb2312");
再显示应该就可以了
我插入两个数据得到的数据是 title=~{LeS}~} news_name=~{?r<\:=?U~}
我原来的数据已经有很多中文数据了,我希望插入后仍然是中文,SQL-SERVER取出中文没有什么限制,只是从文本框得到数据时才需要转换.
我的原程序为:主要完成向数据库中插入数据
<!--insert_news.jsp-->
<%@ page import="java.util.Date" %>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>insert news into DataBase</title>
</head>
<body>
<%
String remoteAddr=request.getRemoteAddr();
if(remoteAddr.equals("127.0.0.1"))
{
String news_name=request.getParameter("news_name");
String title=request.getParameter("title");
out.println(news_name);
out.println(title);
title= new String(title.getBytes(),"iso8859-1");
news_name= new String(news_name.getBytes(),"iso8859-1");
out.println("title="+title);
out.println("news_name="+news_name);
if((news_name!=null)&&(title!=null))
{
try {
Date date=new Date();
String datetime=""+(date.getYear()+1900);
datetime=datetime+"-"+(date.getMonth()+1);
datetime=datetime+"-"+date.getDate();
datetime=datetime+" "+date.getHours();
datetime=datetime+":"+date.getMinutes();
datetime=datetime+":"+date.getSeconds(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:wangzhan";
Connection con=DriverManager.getConnection(url,"jiangweixiang","291546");
PreparedStatement stm=con.prepareStatement("insert into news values(?,?,?)");
stm.setString(1,news_name);
stm.setString(2,datetime);
stm.setString(3,title);
try {
stm.executeQuery();
}catch(Exception e) {}
con.close();
}catch(Exception e) {}
%>
<!--jsp:forward page="insert_news.html" /-->
<%
}
else out.println("新闻名及新闻类型都不能为空!");
}
else out.println("很抱歉,你无权加进一条新新闻!");
%>
</body>
</html>
<%
request.setCharacterEncoding("GB2312");
%>
设置GB2312编码。