在mysql数据库中传入中文字符,并实现正常显示

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
`answer` varchar(45) CHARACTER SET gbk DEFAULT NULL COMMENT '密码答案',
`email` varchar(45) CHARACTER SET gbk DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
out.print(username + "注册成功!");
out.print("</h1>");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
//发送数据库连接错误提示信息
response.sendError(500, "数据库连接错误!");
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegServlet extends HttpServlet {
在mysql中输入中文应做的正确操作
1.在安装mysql后要将C:\Program Files\MySQL\MySQL Server 5.5中的my.ini文件中的两个
default-character-set的值设为default-character-set=gbk
2.在创建mysql数据库时要进行如下创建
private static final long serialVersionUID = 1789481329876401944L;
//数据库连接Connection
private Connection conn;
//初始化方法
public void init() throws ServletException {
//传递给下一过滤器
chain.doFilter(request, response);
}
//销毁方法
public void destroy() {
//释放资源
this.encoding = null;
this.filterConfig = null;
}
}
3.在web.xm中调用
<!--声明字符编码过滤器-->
}
//过滤器处理方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//判断字符编码是否有效
<!--与所有请求关联-->
<url-pattern>/*</url-pattern>
<!--设置过滤器对应的请求方式-->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
4.在数据库连接中的代码
GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
UTF8: CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
2.创建字符编码servlet
CharacterEncodingFilter
package admin.jichupeizhi.zifu;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
//初始化方法
public void init(FilterConfig filterConfig) throws ServletException {
//对filterConfig赋值
this.filterConfig = filterConfig;
//对初始化参数赋值
this.encoding = filterConfig.getInitParameter("encoding");
super.init();
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//数据库连接url
String url = "jdbc:mysql://localhost:3306/zhuce?useUnicode=true&characterEncoding=GBK";
throws ServletException, IOException {
//设置request与response的编码
response.setContentType("text/html");
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
//获取数据库连接
conn = DriverManager.getConnection(url, "root", "111");
} catch (Exception e) {
e.printStackTrace();
}
}
// doPost()方法处理注册请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
String question = request.getParameter("question");
String answer = request.getParameter("answer");
String email = request.getParameter("email");
String dianhua = request.getParameter("dianhua");
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
*字符编码过滤器
ps.setString(3, sex);
ps.setString(4, question);
ps.setString(5, answer);
ps.setString(6, email);
ps.setString(7, dianhua);
ps.setString(8, shouji);
ps.setString(9, wangkadizhi);
数据库中的语名如下
CREATE DATABASE `zhuce` /*!40100 DEFAULT CHARACTER SET gbk */;
CREATE TABLE `tb_user` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(45) CHARACTER SET gbk DEFAULT NULL COMMENT '用户名',
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!--映射字符编码过滤器-->
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
String shouji = request.getParameter("shouji");
String wangkadizhi = request.getParameter("wangkadizhi");
String xueli = request.getParameter("xueli");
//判断数据库中否连接成功
`password` varchar(45) CHARACTER SET gbk DEFAULT NULL COMMENT '密码',
`sex` varchar(45) CHARACTER SET gbk DEFAULT NULL COMMENT '性别',
`question` varchar(45) CHARACTER SET gbk DEFAULT NULL COMMENT '密码问题',
//获取表单中属wk.baidu.com值
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
* @author Li Zhong Wei
*/
public class CharacterEncodingFilter implements Filter{
//字符编码(初始化参数)
protected String encoding = null;
// FilterConfig对象
protected FilterConfig filterConfig = null;
if (conn != null) {
try {
//插入注册信息的SQL语句(使用?占位符)
String sql = "insert into tb_user(username,password,sex,question,answer,email,dianhua,shouji,wangkadizhi,xueli) "
+ "values(?,?,?,?,?,?,?,?,?,?)";
//创建PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对SQL语句中的参数动态赋值
ps.setString(1, username);
ps.setString(2, password);
ps.setString(10, xueli);
//执行更新操作
ps.executeUpdate();
//获取PrintWriter对象
PrintWriter out = response.getWriter();
//输出注册结果信息
out.print("<h1 aling='center'>");
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>admin.jichupeizhi.zifu.CharacterEncodingFilter</filter-class>
<!--设置初始化参数-->
<init-param>
}
}
}
5.在mysql-front软件中
5中jsp文件中要写入
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
if (encoding != null) {
//设置request字符编码
request.setCharacterEncoding(encoding);
//设置response字符编码
response.setContentType("text/html; charset="+encoding);
}
相关文档
最新文档