我也在网上找过相关问题,在eclipse中也改了三个地方的字符编码,以及数据库管理软件中的编码。但是提交到数据库后 ,还是有乱码,全部都是问号,图如下:
以及代码:
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<%
String name = (String) request.getParameter("username");
String password = (String) request.getParameter("password");
String repassword = (String) request.getParameter("repassword");
if(!password.equals(repassword)){
response.sendRedirect("error.jsp");
}
String sex = (String) request.getParameter("sex");
String work = (String) request.getParameter("work");
String[] love = (String[]) request.getParameterValues("love");
String show = (String) request.getParameter("show");
String strLove = "";
for (int i = 0; i < love.length; i++) {
if(strLove.trim().equals("")){
strLove = strLove + "'" + love[i].toString() + "'";
}else{
strLove=strLove+","+"'"+love[i].toString()+"'";
}
}
if(strLove.trim().substring(0,strLove.trim().length()).equals(",")){
strLove=strLove.trim().substring(0,strLove.trim().length()-1);
}
Connection conn=null;
PreparedStatement stmt=null;
String mname=new String(name.getBytes("ISO-8859-1"), "UTF-8");
String mpassword=new String(password.getBytes("ISO-8859-1"), "UTF-8");
String msex=new String(sex.getBytes("ISO-8859-1"), "UTF-8");
String mwork=new String(work.getBytes("ISO-8859-1"), "UTF-8");
String mlove=new String(strLove.getBytes("ISO-8859-1"), "UTF-8");
String mshow=new String(show.getBytes("ISO-8859-1"), "UTF-8");
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","123456");
stmt=conn.prepareStatement("insert into student(username,password,sex,work,love,shows) values(?,?,?,?,?,?)");
stmt.setString(1, mname);
stmt.setString(2, mpassword);
stmt.setString(3, msex);
stmt.setString(4, mwork);
stmt.setString(5, mlove);
stmt.setString(6, mshow);
stmt.execute();
}catch(Exception e){
e.printStackTrace();
}
//response.sendRedirect("success.jsp");
%>
</body>
</html>
请大神帮我解决下这个问题吧,头都要炸了。。。。。
本人在学习初期,懂得不多,不喜勿喷。。。