在Struts中怎么把中文数据从JSP页面提交到MySql数据库中
Struts中的Action文件代码:
public class TiJiaoAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
ActionForward actionforward = new ActionForward();
UserInfoActionForm userInfoForm = (UserInfoActionForm)form;
request.setCharacterEncoding("gb2312");
String id = userInfoForm.getId();
String name = userInfoForm.getName();
Connection con = null;
Statement sta = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/china";
String user = "root";
String pwd = "111";
con = DriverManager.getConnection(url,user,pwd);
sta = con.createStatement();
String sql = "insert into user_info values('"+id+"','"+name+"')";
sta.executeUpdate(sql);
actionforward = mapping.findForward("success");
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return actionforward;
}
}
JSP页面中编码(<%@ page language="java" pageEncoding="gb2312"%>)也是GB2312。
但是在jsp页面提交时显示下面错误信息(在数据库平台可以直接向表中插入中文数据):
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'name' at row 1
跪求天下IT豪杰,代码尽量写的详细点,本人比较笨,思念答案早日到来...
public class TiJiaoAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
ActionForward actionforward = new ActionForward();
UserInfoActionForm userInfoForm = (UserInfoActionForm)form;
request.setCharacterEncoding("gb2312");
String id = userInfoForm.getId();
String name = userInfoForm.getName();
Connection con = null;
Statement sta = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/china";
String user = "root";
String pwd = "111";
con = DriverManager.getConnection(url,user,pwd);
sta = con.createStatement();
String sql = "insert into user_info values('"+id+"','"+name+"')";
sta.executeUpdate(sql);
actionforward = mapping.findForward("success");
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return actionforward;
}
}
JSP页面中编码(<%@ page language="java" pageEncoding="gb2312"%>)也是GB2312。
但是在jsp页面提交时显示下面错误信息(在数据库平台可以直接向表中插入中文数据):
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'name' at row 1
跪求天下IT豪杰,代码尽量写的详细点,本人比较笨,思念答案早日到来...
...全文
相关推荐
mysql+jsp+SSH网站开发中文乱码解决方案
mysql+jsp+SSH网站开发中文乱码解决方案
struts国际化注册登录验证
struts技术的国际化注册登录效果,带有mysql数据库
许愿墙程序 JAVA struts
马上要毕业了,写的小程序,程序功能基本实现,还有一些细节需要完善!比如乱码问题,目前不能正常显示中文,插入没有问题
struts+hibernate入门实例(eclipse版)
struts+hibernate入门实例(eclipse版) author:朱千平 phone:13522080786 qq:200896066
mysql+jsp+SSH网站开发中文乱码解决方案.doc
mysql+jsp+SSH网站开发中文乱码解决方案适合电子商务平台及其它数据库平台开发人员参考和学习.


Web 开发
