当前位置: 代码迷 >> Java Web开发 >> 关于jsp 通过JDBC连接mysql乱码有关问题
  详细解决方案

关于jsp 通过JDBC连接mysql乱码有关问题

热度:4946   发布时间:2013-02-25 21:20:11.0
关于jsp 通过JDBC连接mysql乱码问题
我用的所有编码方式都是UTF-8
 我的连接数据库的字符串为jdbc:mysql://localhost/game?useUnicode=true&characterEncoding=UTF-8
mysql 数据库的编码方式为 character:UTF-8 collation:utf8_unicode_ci
浏览器设置的编码方式也是UTF-8
所有的jsp、servlet、和java文件的编码方式都是UTF-8
jsp 里的编码方式也是UTF-8
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

servlet中添加用户的函数为
public void addUser(userBean user){
PreparedStatement statement = null;
Connection cnn = null;
try{
cnn = new ConnectionMysql().getConnection();
statement = cnn.prepareStatement("insert user values(?,?,?,?,?,?,?,?,?)");//插入用户名称
statement.setString(1, user.getUserName());
statement.setString(2, user.getPassword());
statement.setString(3, user.getGroupName());
statement.setString(4, user.getGroupRemark());
statement.setString(5, user.getCaptain1());
statement.setString(6, user.getCaptain2());
statement.setString(7, user.getCoach1());
statement.setString(8, user.getCoach2());
statement.setInt(9, user.getRange());
statement.executeUpdate();
System.out.println(statement);
}// end try
catch(Exception e){
e.printStackTrace();
}// end catch
finally{
close(null,statement,cnn);
}
}

System.out.println(statement)语句
只要文本框里面带了中文的全出现乱码
insert user values('aiaiia59272168421ff','aiaiia59272168421','?¨??°???°','è??vcxzxcv',' ??????','','','',1)
插入mysql 数据库时发现插进去的都是乱码

像用户注册模块注册失败时,本来应该在像“代表队”等文本框中返回注册前输入的值,可是也出现乱码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>用户登录</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<metax http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
  <div align="center"><br> 
  </div>
  <div align = "center" >
<marquee width="90%" direction="left" style="background-color: rgb(0, 255, 250);"><em> 
<font size="10" face="Vrinda" color="#ffffff">武 术 邀 请 赛 注 册 用 户</font></em>
</marquee>
  </div>
  <form name="userRegedit" method="post" action = "regeditSevlet">
  <table width = "%70" border="0" align = "center" cellpadding = "2" cellspacing = "0">
<tr bgcolor="#efefef">
<td width = "20%" align = "right">用户名:</td>
<td width = "35%" align = "left" valign = "bottom">
<input name = "username" type = "text" <%if(request.getAttribute("username")!=null){%>
value = <%= request.getAttribute("username")%> 
  相关解决方案