首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

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

2012-05-21 
关于jsp 通过JDBC连接mysql乱码问题我用的所有编码方式都是UTF-8我的连接数据库的字符串为jdbc:mysql://lo

关于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")%> 


<%} %>
>
<!-- 注册失败返回数据 -->
</td>
<td width = "45%" align = "left" valign = "bottom">*
<input name= "checkUser" type = "button" value="查看是否可用" onclick = "checkId(form);" 
style = "background-color:rgb(132,57,57)">
</td>
</tr>
<tr bgcolor="#efefef">
<td colspan="3">(用户名由a~z的英文字母(区分大小写)、0~9的数字或下划线组成,长度为10~20位)
</tr>
<!-- 注册错误时输出的信息 开始 -->
<tr bgcolor="#efefef"> 
<td colspan = "2" align = "center">
<font size = "4" color = "#ff0000">
<%if(request.getAttribute("errorUserMsg") != null){
out.print(request.getAttribute("errorUserMsg"));
}
%>
</font>
</td>
<td></td>
</tr>
<!-- 注册错误时输出的信息 结束 --> 
<tr>
<td width = "20%" align = "right">密码:</td>
<td width = "35%" align = "left" valign = "bottom">
<input type = "password" name = "password" <%if(request.getAttribute("password")!=null) {%>
value = <%=request.getAttribute("password")%>
<%} %>
>
<!-- 注册失败返回数据 -->
</td>
<td width = "45%" align = "left" valign = "bottom">*
(请输入长度为10~20位的密码)
</td>
</tr>
<!-- 注册错误时输出的信息 开始 -->
<tr>
<td colspan = "2" align = "center">
<font size = "4" color = "#ff0000">
<%if(request.getAttribute("errorPasswordMsg") != null){
out.print(request.getAttribute("errorPasswordMsg"));
}
%>
</font>
</td>
</tr>
<!-- 注册错误时输出的信息 结束 -->
<tr bgcolor = "#efefef">
<td width = "20%" align = "right">确认密码:</td>
<td width = "35%" align = "left" valign = "bottom">
<input type = "password" name = "confirmpassword" <%if(request.getAttribute("confirmpassword")!=null) {%>
value = <%=request.getAttribute("confirmpassword") %>
<%} %>
>
</td>
<td>*
(请再次输入密码)
</td>
</tr>
<!-- 注册错误时输出的信息 -->
<tr>
<td width = "20%" align = "right">代表队名称:</td>
<td width = "35%" align = "left" valign = "bottom">
<input type = "text" name = "groupname" <%if(request.getAttribute("groupname")!=null){ %>
value = <%=request.getAttribute("groupname") %>
<%} %>
>
<!-- 注册失败返回数据 -->
</td>
<td>*
(代表队名称绑定用户名!限为1~15个字)
</td>
</tr>
<!-- 注册错误时输出的信息 开始 -->
<tr>
<td colspan = "2" align = "center">
<font size = "4" color="#ff0000">
<% if(request.getAttribute("errorGroupnameMsg") != null)
out.print(request.getAttribute("errorGroupnameMsg"));
%>
</font>
</td>
</tr>
<!-- 注册错误时输出的信息 结束 -->
<tr bgcolor = "#efefef">
<td width = "20%" align = "right" valign = "bottom">代表队备注</td>


<td width = "35%" align = "left" valign = "bottom">
<input type = "text" name = "groupremark" <%if(request.getAttribute("groupremark")!=null) {%>
value = <%=request.getAttribute("groupremark") %>
<%} %>
>
<!-- 注册失败返回数据 -->
</td>
<td width = "45%">*
(代表队备注长度限为1~15个字)
</td>
</tr>
<!-- 注册错误时输出的信息 开始 -->
<tr bgcolor="#efefef">
<td colspan = "2" align = "center">
<font size = "4" color = "#ff0000">
<% if(request.getAttribute("errorGroupremarkMsg") != null)
out.print(request.getAttribute("errorGroupremarkMsg"));
%>
</font>
</td>
<td></td>
</tr>
<!-- 注册错误时输出的信息 结束 -->
<tr>
<td width = "20%" align = "right" valign = "bottom">领队1名</td>
<td width = "35%" align = "left" valign = "bottom">
<input type = "text" name = "captain1">
</td>
<td width = "45%">*
(领队名字限为0~10个字)
</td>
</tr>
<!-- 注册错误时输出的信息 开始 -->
<tr>
<td colspan = "2" align = "center">
<font size = "4" color = "#ff0000">
<% if(request.getAttribute("errorPeopleMsg") != null)
out.print(request.getAttribute("errorPeopleMsg"));
%>
</font>
</td>
</tr>
<!-- 注册错误时输出的信息 结束 -->
<tr bgcolor = "#efefef">
<td width = "20%" align = "right" valign = "bottom">领队2名</td>
<td width = "35%" align = "left" valign = "bottom">
<input type = "text" name = "captain2">
</td>
<td width = "45%">*
(领队名字限为0~10个字)
</td>
</tr>
<tr>
<td width = "20%" align = "right" valign = "bottom">教练1名</td>
<td width = "35%" align = "left" valign = "bottom">
<input type = "text" name = "coach1">
</td>
<td width = "45%">*
(教练名字限为0~10个字)
</td>
</tr>
<tr bgcolor = "#efefef">
<td width = "20%" align = "right" valign = "bottom">教练2名</td>
<td width = "35%" align = "left" valign = "bottom">
<input type = "text" name = "coach2">
</td>
<td width = "45%">* 
(教练名字限为0~10个字) 
</td>
</tr>
<tr>
<td width = "20%" align = "right" valign = "bottom">级别</td>
<td width = "35%" align = "left" valign = "bottom">
<select name = "range" type = "select">
<option value = "1">1</option>
<option value = "2">2</option>
</select>
</td>
<td width = "45%">* 
(用户级别:1为管理员;2为普通用户) 
</td>
</tr>
<tr>
<td colspan = "3" align = "center">
<br>
<input type = "submit" name = "Submit" value = "注&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;册" onMouseOut = this.style.color="blue">


</td>
</tr>
  </table>
  </form>
  <!-- 获得注册错误的代码 -->
  <!--javascript-->
  <script type="text/javascript">
  import com.model.*;
function checkId(form){
if(form.username.value == ""){
alert("用户名不能为空!");
}
else if(form.username.value.length<10 || form.username.value.length>20){
alert("用户名长度不符合要求!");
}
else if(new com.model.userBeanController().checkUser(username){
;
}
 }// end function
</script>
  <!--javascript-->
  </body>
</html>








希望得到一个全部的编码方案

[解决办法]
你使用的是MYSQL数据库。数据库的编码你看了吗?把MYSQL的字符编码设置为GB2312或者UTF-8。试一试吧
[解决办法]
MYSQL的编码集没有设好吧!
[解决办法]
请求上的时候没设置字符集吧?
[解决办法]
呵呵,明白就好。恭喜恭喜。

热点排行