myeclispe中向mysql中插入中文数据出现??问题解决办法
或许很多人会出现??这种令人头痛的mysql的中文乱码问题:解决如下:
1.先对于新建的数据库要设置默认的字符集为UTF-8
create database mydb default character set utf8 collate utf8_general_ci;
2.然后对于想对应的表进行设置默认字符集
CREATE TABLE IF NOT EXISTS `mydb` (
`username` varchar(64) NOT NULL,
`userid` int(11) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.最重要的一步在jdbc中的uri的地址中添加如下语句?characterEncoding=UTF-8设置字符集为UTF-8
jdbc:mysql://localhost:3306/db_name?characterEncoding=UTF-8
如下是一个例子:
regServlet.java
package cn.lonecloud.demo; import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class RegServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection conn=null;
PreparedStatement prst=null;
ResultSet rs=null;
String db_driver="com.mysql.jdbc.Driver";
String db_url="jdbc:mysql://localhost:3306/db_friend?characterEncoding=UTF-8";
String db_user="root";
String db_password="";
String sql1="select * from userinfo where username=?";
String sql2="insert into userinfo values(?,?)";
try {
String username=request.getParameter("userName");
String userpass=request.getParameter("password");
Class.forName(db_driver);
conn=DriverManager.getConnection(db_url,db_user,db_password);
prst=conn.prepareStatement(sql1);
prst.setString(1, username);
rs=prst.executeQuery();
if (!rs.next()) {
prst=conn.prepareStatement(sql2);
prst.setString(1, username);
prst.setString(2, userpass);
int count=prst.executeUpdate();
if (count!=0) {
HttpSession session=request.getSession();
session.setAttribute("username", username);
request.setAttribute("msg", "注册成功!");
request.getRequestDispatcher("index.jsp").forward(request, response); }else{
request.setAttribute("msg", "登录失败,密码错误!");
request.getRequestDispatcher("error.jsp").forward(request, response); }
}else {
request.setAttribute("msg", "用户不存在!");
request.getRequestDispatcher("error.jsp").forward(request, response); }
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if (rs!=null) {
try {
rs.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
if (prst!=null) {
try {
prst.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
if (conn!=null) {
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
out.close();
}
} }
reg.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>新用户注册</h1>
<form action="RegServlet" method="post">
<input type="text" name="userName"></br>
<input type="password" name="password"></br>
<input type="submit" id="btn_load">
</form>
</body>
</html>
LoginFilter.java
package cn.lonecloud.demo; 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class LoginFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub } @Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//在过滤器中必须加入下面这两句话,不然会不能这样调用下面的方法
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse resp=(HttpServletResponse)response;
request.setCharacterEncoding("UTF-8");
String path=req.getServletPath();
HttpSession session=req.getSession();
String username=(String)session.getAttribute("username");
System.out.println(path);
boolean b1=path.endsWith("login.jsp");
boolean b2=path.endsWith("error.jsp");
boolean b3=path.endsWith("LoginServlet");
boolean b4=path.endsWith("RegServlet");
boolean b5=path.endsWith("reg.jsp");
boolean b6=(username!=null);
if (b1||b2||b3||b4||b5||b6) {
chain.doFilter(request, response);
}else {
resp.sendRedirect("error.jsp");
} } @Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub } }
myeclispe中向mysql中插入中文数据出现??问题解决办法的更多相关文章
- 解决Python向MySQL数据库插入中文数据时出现乱码
解决Python向MySQL数据库插入中文数据时出现乱码 先在MySQL命令行中输入如下语句查看结果: 只要character_set_client character_set_database ch ...
- 解决mysql无法插入中文数据及插入后显示乱码的问题
(1)废话不多说就是使用mysql数据库的时候无法输入中文,可以输入中文后显示的又是乱码!! (2开始解决问题: 第一步:找到安装mysql的目录找到 my.ini 文件: 第二步:使用记事本打开my ...
- python 向MySQL里插入中文数据
用python向MySQL中插入中文数据出错,原代码片段: 1 db = MySQLdb.connect("localhost","root","12 ...
- mysql不能插入中文数据
上次遇到的是向mysql插入中文数据,中文数据乱码了.这次直接就不能插入中文数据了!!!! 参考博文:http://blog.csdn.net/generalyy0/article/details/7 ...
- pycharm连接mysql数据库插入中文数据时出现1366编码错误
创建数据库的时候应该这样创建: create database xxxxxxx DEFAULT CHARSET utf8 COLLATE utf8_general_ci:
- mysql使其支持插入中文数据的办法
1.找到安装的文件夹C:\Program Files (x86)\MySQL\MySQL Server 5.0 2.修改文件夹下的my.ini文件: 找到画黄线的这一字段 将后面改为utf8 这个过程 ...
- mysql插入中文数据变成问号怎么处理
插入中文数据变成问号,一般都是因为字符集没有设置成utf8的原因 1.修改字符集: ALTER TABLE 表名 MODIFY 列名 类型(50) CHARACTER SET "utf8&q ...
- MySQL插入中文数据出现?号
原文转载自:https://blog.csdn.net/LynneZoe/article/details/79174119 运行环境:win10 mysql版本:Mysql5.6 做一个项目的时候,向 ...
- EF 连接MySQL 数据库 保存中文数据后乱码问题
EF 连接MySQL 数据库 保存中文数据后乱码问题 采用Code First 生成的数据库,MySQL数据库中,生成的表的编码格式为***** 发现这个问题后,全部手动改成UTF8(图是另一个表的 ...
随机推荐
- spring使用c3p0报错
java.sql.SQLException: Connections could not be acquired from the underlying database! at com.mchang ...
- js_10_dom表单
事件的优先级? 先执行事件,后执行标签内置事件,如果事件返回false不执行后面的事件或标签内置事件 如何通过js提交表单? 任意标签定义onclick事件 函数中写入:document.getEle ...
- chroot: failed to run command `/bin/bash': No such file or directory
1 使用chroot命令时报错如下: testupgrade:/ # chroot /sb chroot: cannot change root directory to /sb: No such f ...
- android 中的ExpandableListView取消一级图标
mainlistview = (ExpandableListView) view.findViewById(R.id.listview_myteacher); mainlistview.setGrou ...
- C# 关键字--virtual(转)
C# 关键字--virtual 一. virtual 关键字用于修饰方法.属性.索引器或事件声明,并使它们可以在派生类中被重写.虚拟成员的实现可由派生类中的重写成员更改,而非虚拟成员是无法由派生类 ...
- Failed to get D-Bus connection: Operation not permitted解决
docker中安装centos无法使用systemctl命令管理进程,报以下错误: Failed to get D-Bus connection: Operation not permitted 原因 ...
- [ASP.NET][Session] 使用 SQLServer 会话管理解决 Session 丢失问题
使用 SQLServer 会话管理解决 Session 丢失问题 步骤 1.通过命令行执行 aspnet_regsql.exe 程序(不要双击安装),先在 CMD 中输入命令 cd C:\Window ...
- Hexo中添加本地图片
First 1 把主页配置文件_config.yml 里的post_asset_folder:这个选项设置为true 2 在你的hexo目录下执行这样一句话npm install hexo-asset ...
- grep工具及正则表达式
正则表达式和通配符 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法,那么该工具就可以处理正则表达式的字符串.vim.grep.awk.sed都支持正 ...
- Nginx负载均衡搭建(Window与Linux)
windows上搭建nginx负载均衡 1.准备几台http服务器软件,这里选用一台apache一台tomcat apache(windows)下载链接:https://www.apachehaus. ...