楼主看一下以下的代码吧,我前几天也是遇到了这样的问题,现在已经解决了,其中数据库的编码是UTF-8,或许有更好的方法吧,我的问题就是这样解决的,希望能给你一点启示,呵呵!Connection con;
PreparedStatement stmt;
try
{//载入数据库驱动
Class.forName(constants.DRIVERSTRING);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
try
{
con=DriverManager.getConnection(constants.DATABASES,constants.USER,constants.PASSWORD);
stmt=con.prepareStatement
(constants.SQLSTRING);
stmt.setInt(1,ID);
stmt.setInt(2,temper[0]);
stmt.setInt(3,temper[1]);
stmt.setBytes(4,s[3].getBytes("UTF-8"));//编码转化->前提是中文能写进内存
stmt.setBytes(5,s[5].getBytes("UTF-8"));
stmt.setBytes(6,s[1].getBytes("UTF-8"));
stmt.setBytes(7,s[4].getBytes("UTF-8"));
System.out.println(stmt);
stmt.executeUpdate();
System.out.println("数据插入成功!");
con.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
PreparedStatement stmt;
try
{//载入数据库驱动
Class.forName(constants.DRIVERSTRING);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
try
{
con=DriverManager.getConnection(constants.DATABASES,constants.USER,constants.PASSWORD);
stmt=con.prepareStatement
(constants.SQLSTRING);
stmt.setInt(1,ID);
stmt.setInt(2,temper[0]);
stmt.setInt(3,temper[1]);
stmt.setBytes(4,s[3].getBytes("UTF-8"));//编码转化->前提是中文能写进内存
stmt.setBytes(5,s[5].getBytes("UTF-8"));
stmt.setBytes(6,s[1].getBytes("UTF-8"));
stmt.setBytes(7,s[4].getBytes("UTF-8"));
System.out.println(stmt);
stmt.executeUpdate();
System.out.println("数据插入成功!");
con.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
解决方案 »
- ssh2整合问题
- tomcat 上部署WAR包 程序自动生成日志文件功能创建不了文件
- 浏览器提示addEvent is not defined
- Spring2.5 事务配置
- 请教一个jsp里弹出js对话框问题
- 求救 无效的描述符索引
- JavaMail做好了,也发送成功了。但右下角出现个东西,那是什么??
- 大家有好的JSP书籍推荐一下?
- 50分求问几个JAVA与SQL的问题???(要是不够的话可以加分)
- 急!急!急! 如果要存很长一个字符串到SQL SERVER2000里面怎么办
- 老外书中的JSP例子为什么不支持中文
- 求助:jsp总是出java.lang.NumberFormatException: null错
<%@ page contentType="text/html;charset=GB2312" %>
<%@ include file="pass.jsp" %>
<%@ include file="conn.jsp" %>
<%!
Connection con=null;
%>
<%
Statement sql=null;
ResultSet rs=null;
%>
<%
String inputdate=request.getParameter("input_date");
String errorsource=request.getParameter("error_source");
String error_id=request.getParameter("error_id");
String start_time=request.getParameter("error_start_time");
String end_time=request.getParameter("error_end_time");
String person=request.getParameter("do_person");
String module=request.getParameter("error_module");
String level=request.getParameter("error_level");
String region=request.getParameter("influence_region");
String phenomena=request.getParameter("error_phenomena");
String process=request.getParameter("do_process");
String result=request.getParameter("do_result");
String analyse=request.getParameter("error_analyse");
String idea=request.getParameter("manager_idea");
String will=request.getParameter("will_do");
%>
<%
sql=con.createStatement();
sql.executeUpdate("insert into error_do (input_date,error_source,error_id,error_start_time,error_end_time,do_person,error_module,error_level,influence_region,error_phenomena,do_process,do_result,error_analyse,manager_idea,will_do) values ('"+inputdate+"','"+errorsource+"','"+error_id+"','"+start_time+"','"+end_time+"','"+person+"','"+module+"','"+level+"','"+region+"','"+phenomena+"','"+process+"','"+result+"','"+analyse+"','"+idea+"','"+will+"')");
response.sendRedirect("./6-1.jsp");
%>
我不知道如何处理。请具体告之。
PreparedStatement stmt;
try
{//载入数据库驱动
Class.forName(constants.DRIVERSTRING);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
try
{
con=DriverManager.getConnection(constants.DATABASES,constants.USER,constants.PASSWORD);
stmt=con.prepareStatement
(constants.SQLSTRING);
stmt.setInt(1,ID);
stmt.setInt(2,temper[0]);
stmt.setInt(3,temper[1]);
stmt.setBytes(4,s[3].getBytes("UTF-8"));//编码转化->前提是中文能写进内存
stmt.setBytes(5,s[5].getBytes("UTF-8"));
stmt.setBytes(6,s[1].getBytes("UTF-8"));
stmt.setBytes(7,s[4].getBytes("UTF-8"));
System.out.println(stmt);
stmt.executeUpdate();
System.out.println("数据插入成功!");
con.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
在JSP中加入以下的一句编译指令,定义JSP程序使用字符集为GBK
<%@ page contentType="text/html;charset=GBK" %>
把所有的变量都转换成“ISO-8859-1”编码的。
情况1:
String str=new String(dbstr.getBytes("GBK"),"ISO-8859-1");
情况2:
String test1=new String(dbstr.getBytes("ISO-8859-1"),"GBK");
JSP中所有的中文乱码问题由此都可解决的
这是因为在使用JDBC时,有些Driver会将从数据库中读出的中文自动地转换成Unicode,而有些不会,如果Driver做过转换而系统又再做一次,就会出现问题。
你的问题应该属于情况1,如果不行的话就换成情况2,一定可以的。
request.setCharsetEncoding("GBK");
不就成了
rs = pStmt.executeQuery();
while(rs.next()){
String field1 = rs.getString("FIELD1");
String field1_ch = new String(field1.getBytes("ISO - 88591 - 1"),"GB2312");
}
把数据库字符编码为“ISO-8859-1”转换为GB2312就可以直接读取数据库中的中文数据了
1、jsp中表单传递时,解决办法,在涉及到的开始加入?request.setCharacterEncoding("GBK"); ?
2、在把数据存到数据库时,解决办法,应该只要设置正确的数据库连接字符串就可以了