有人可以帮我看看为什么我的代码输入中文会失败啊?
在网页上输入中文到数据库失败,显示都是???
下面是代码:servlet的:
public class InsertNewSubject extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312 ";
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding( "GB2312 ");
PrintWriter out = response.getWriter();
String stuNo1=request.getParameter( "stuNo ");
int stuNo=Integer.parseInt(stuNo1);
String stuName=request.getParameter( "stuName ");
String classes=request.getParameter( "classes ");
StuInfo student=new StuInfo();
student.setStuNo(stuNo);
student.setStuName(stuName);
student.setClasses(classes);
if(student.insertStu()){
System.out.println( "学生插入成功! ");
getServletContext().getRequestDispatcher( "/mlogin?password=admin&username=admin ").forward(request,response);
}
out.close();
}
javabean的:
public boolean insertStu() {
boolean bl = false;
status = "学生添加失败 ";
cb = new ConnectBean();
cb.openConnection();
/*if (!cb.openConnection()) {
status = "连接数据库失败!!! ";
}*/
try {
ps = cb.createPreparedStatement(
"insert into stumessage values(?,?,?,?,?,?,?) ");
ps.setInt(1, this.stuNo);
ps.setString(2, this.stuName);
ps.setString(3, "123456 ");
ps.setString(4, this.classes);
ps.setInt(5, 0);
ps.setInt(6, 0);
ps.setInt(7, 0);
ps.executeUpdate();
bl = true;
status = "用户添加成功 ";
} catch (Exception se) {
status = "写入数据发生错误!!! " + se;
} finally {
try {
cb.close();
} catch (Exception es) {
status = "后台发生错误 " + es;
}
}
return bl;
}
代码有问题么?
[解决办法]
你最好在中间多加些调试代码,看看取出的数据是不是中文。
你连接数据库的字符串是不是支持中文的?useUnicode=true
你的数据表是不是gb2312的?
[解决办法]
将字符集改gbk试试