jsp mysql 中文乱码问题

whoisme123 2010-04-01 05:18:58
从JSP存储到MYSQL里,中文在数据库里显示正常
用编号从JSP页面查询,读取中文也正常显示,
但用中文输入JSP页面查询,就显示不出来了,请大家帮忙看看,
谢谢!!!


代码:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="cim.*" %>
<%@ include file="sessionadmin.jsp"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
<title>档案系统</title>
<SCRIPT language=javascript>
function Chk(theForm)
{

if (theForm.no.value == ""){
alert("请输入姓名!");
theForm.no.focus();
return (false);
}

}
</SCRIPT>

<link rel="stylesheet" href="images/css.css" type="text/css" media="screen">
</head>
<BODY text="#000000" topmargin=0>
<DIV id=overDiv style="Z-INDEX: 1; POSITION: absolute"></DIV>
<TABLE cellSpacing=1 cellPadding=0 width="98%" align=center border=0 class="tableBorder">
<TBODY>
<TR>
<FORM name="Form1" action="staff_search.jsp" method="post" onSubmit="return Chk(this);">
<th align=middle colSpan=6 height=22 bgcolor="#CCCCCC">
<input type="text" name="name" size="15" maxlength="15""/>  
<input type="SUBMIT" name="Submit" value="SSS" />
</th>
</FORM>
</TR>
</TABLE>
 
 
</FORM>

<table width="200%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000000">
<tr>
<td width="9%" height="20" class=forumrow>查询结果</td>
</tr>
</table>
<TABLE cellSpacing=1 cellPadding=0 width="200%" align=center border=0 class="tableBorder">
<TBODY>
<TR>
<th width="60" height="20">员工号</th>
<th width="70" height="20">姓名</th>
<th width="100" height="20">拼音</th>
<th width="30" height="20">性别</th>
</tr>

<%
request.setCharacterEncoding("GB2312");
String no=request.getParameter("name");
DBConnect dbc = new DBConnect();
if(no!=null){
String sql="select no,name,spell,sex from staff_base where name = '"+name+"'";
ResultSet rs = dbc.executeQuery(sql);
try{
while(rs.next()){
%>
<tr>
<td align="CENTER" bgcolor="#F3F3F3"><%=rs.getString("no") %></td>
<td align="CENTER" bgcolor="#F3F3F3"><%=rs.getString("name") %></td>
<td align="CENTER" bgcolor="#F3F3F3"><%=rs.getString("spell") %></td>
<td align="CENTER" bgcolor="#F3F3F3"><%=rs.getString("sex") %></td>
</tr>
<%
}
}catch(Exception e){
System.err.println(e);
dbc.close();
}finally{
}
}
%>



</table>
</BODY>
</HTML>
...全文
273 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
J381396205 2010-07-30
  • 打赏
  • 举报
回复
在传递中文参数时,对参数中的中文进行编码,
如 encodeURIComponent($("#sgqgk").text())
$("#sgqgk").text())是input text中获得的输入值
其他所有的编码部分都是用UTF-8, 应该就不会有问题了
lingfeng179 2010-07-30
  • 打赏
  • 举报
回复
jsp乱码问题后麻烦的 你要先查出是哪里开始出现乱码的 页面 还是 数据库
这样次啊能解决 如果在解决不了的话 你就把所有页面和数据库都默认utf-8
at1943 2010-07-30
  • 打赏
  • 举报
回复
解决mysql乱码:
1.mysql中的my.ini的以下两项设置一下默认编码
[client]
default-character-set=gbk
[mysqld]
default-character-set=gbk
没有my.ini的我在后面给一下整个文件
2.在页面中统一编码,或者使用filter来强制发送接收编码一致
spring中如何设置filter
web.xml中加入
Java code<filter>
<filter-name>SpringFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SpringFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

或者自己写个过滤器
package com.noday.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
public class Encoder extends HttpServlet implements Filter {
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
((HttpServletRequest) arg0).setCharacterEncoding("gbk");
arg1.setContentType("text/html;charset=gbk");
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
web.xml中加入
<filter>
<filter-name>Encoder</filter-name>
<filter-class>com.noday.filter.Encoder</filter-class>
</filter>

<filter-mapping>
<filter-name>Encoder</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



my.ini 完整内容
Java code# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is C:\mysql\data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
default-character-set=gbk
#password = your_password
port = 3306
socket = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
default-character-set=gbk
port = 3306
socket = /tmp/mysql.sock
skip-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking

# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin

# binary logging format - mixed recommended
binlog_format=mixed

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin

# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update = /path-to-dedicated-directory/hostname

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = C:\mysql\data/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = C:\mysql\data/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
leihuanhuan123 2010-04-12
  • 打赏
  • 举报
回复
String name = new String(request.getParameter("name").getBytes("iso8859-1"),"gb2312");
whoisme123 2010-04-12
  • 打赏
  • 举报
回复
请问这个web.xml里需要如何配置啊???
2010-04-02
  • 打赏
  • 举报
回复
乱码是什么形式的?

是不认识的生僻字 还是问号?

解决乱码 无非是把页面与数据库的编码保持一致即可。
zhouxuegangjava163 2010-04-02
  • 打赏
  • 举报
回复
你好,楼主!
MySql增删查改乱码的解决方法集合解决MySql数据乱码:
1 写过滤器设置编码格式(格式和JSP页面的编码一样),或则在请求里面写request.setCharacterEncoding("编码方式");
2 如果是查询出数据乱码,在链接的URL上加上编码格式(你这里加了,没问题);
3 修改my.ini文件里面的 default-character-set= 您要的编码格式 (一共有两处,你查找一下 改为一样的编码格式) ,如果是写入到数据库之后是乱码(前提是已经写了过滤器处理编码),修改my.ini文件里面查找sql-mode 设置 sql-mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"; 如果出现乱码情况 就试试吧!

祝你好运!
2010-04-02
  • 打赏
  • 举报
回复
给你个filter

package com.ccit.ejtg.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ChineseFilter implements Filter {

private String encode = "GBk"; // 设置你的编码

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding(encode);// 设置字符编码
chain.doFilter(request, response);
}

// 读取初始化参数
public void init(FilterConfig config) throws ServletException {

encode = config.getInitParameter("encode");
System.out.println("EncodeFilter...encode:" + encode);
}

}


在web.xml里做相应的配置就OK了
yueweiac110 2010-04-02
  • 打赏
  • 举报
回复
保证代码的一致性
要是gb2312
数据库和页面都是就可以了
yueweiac110 2010-04-02
  • 打赏
  • 举报
回复
首先你先看到写入数据库是否是乱码
如果是:是写入的问题
如果不是
那就是取的问题
whoisme123 2010-04-02
  • 打赏
  • 举报
回复
String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"GB2312");
out.println(name);

我用以上方法,看到页面上显示的是??
所以可以确定是JSP后台中文会乱码导致的.
请教一下,为什么传入MYSQL是正常的,直接查询就出现乱码呢?
真奇怪啊
whoisme123 2010-04-02
  • 打赏
  • 举报
回复
前面几位朋友,我已经说了,从JSP传数据到MYSQL是正常的,
只是用中文查询时,没有得到数据,可以确认是JSP后台中文乱码.

还有楼上的这句代用
String name = request.getParameter("name");

页面会出错啊???
UFOUFO777 2010-04-02
  • 打赏
  • 举报
回复
所有有关编码的地方全搞成一致的就行了
locationkey 2010-04-02
  • 打赏
  • 举报
回复
你到servlet中去做下转码就ok了,在页面输入数据到servlet中的时候遇到网络后就变成乱码了,在网络流中使用的都是iso-8859-1的编码在做传输,你转码在去数据库中查询就可以了。
whoisme123 2010-04-02
  • 打赏
  • 举报
回复
乱码全是????
楼上的这句还是不对的,根上一句的错误是一样的啊

改了my.ini还是一样的问题啊
dianwei888 2010-04-02
  • 打赏
  • 举报
回复
不好意思,我写错了,应该是
String name = new String(request.getParameter("name").getBytes("gb2312"),"iso8859-1");
dianwei888 2010-04-01
  • 打赏
  • 举报
回复
String name = new String(request.getParameter("name").getBytes("iso8859-1"),"gb2312");
SuperCustomer 2010-04-01
  • 打赏
  • 举报
回复
更改MySQL数据库编码:
运行MySQL Server Instance Confituration Wizard程序,根据向导一直点击“next”,当到配置数据库编码这一步时,注意,选择最后一个单选按钮(Manual Selected Default Character Set/Collation),在下拉框中选择utf-8,然后按向导完成配置即可。
sl514 2010-04-01
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*" pageEncoding="UTF-8"%>


在头部加上这句吧 吧你那句换掉
sl514 2010-04-01
  • 打赏
  • 举报
回复
保持你的 数据库 页面编码一致
加载更多回复(3)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧