[求助]struts action添加数据入库mysql出现乱码_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2034 | 回复: 0   主题: [求助]struts action添加数据入库mysql出现乱码        下一篇 
dansmall
注册用户
等级:新兵
经验:52
发帖:71
精华:0
注册:2011-10-27
状态:离线
发送短消息息给dansmall 加好友    发送短消息息给dansmall 发消息
发表于: IP:您无权察看 2014-12-24 15:07:25 | [全部帖] [楼主帖] 楼主

首先是我的mysql编码已经修改:

mysql> show Variables like "%set%";
+--------------------------+-------------------------------------------------------------------+
| Variable_name            | Value                                                             |
+--------------------------+-------------------------------------------------------------------+
| auto_increment_offset    | 1                                                                 |
| character_set_client     | gb2312                                                            |
| character_set_connection | gb2312                                                            |
| character_set_database   | gb2312                                                            |
| character_set_filesystem | binary                                                            |
| character_set_results    | gb2312                                                            |
| character_set_server     | latin1                                                            |
| character_set_system     | utf8                                                              |
| character_sets_dir       | /usr/local/mysql-standard-5.0.27-linux-i686/share/mysql/charsets/ |


接着是我的action:
请大家帮我看看,好像是使用PreparedStatement生成insert语句时就产生了乱码:

public class registerAction extends Action {
      public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws Exception {
            ActionErrors errors = new ActionErrors();
            boolean useridvalid = true;
            useridvalid = useridcheck(request,(registerActionForm)form);
            //用户ID未被占用
            if(useridvalid){
                  boolean registered = false;
                  registered = doRegister(request,(registerActionForm)form);
                  if(registered){
                        form.reset(mapping, request);
                        return mapping.findForward("success");
                  }
                  else{
                        return mapping.findForward("error");
                  }
            }
            else{
                  errors.add("UserIDIsOccupied", new ActionMessage("UserIDIsOccupied"));
                  form.reset(mapping, request);
                  return mapping.findForward("error");
            }
      }
      private boolean doRegister(HttpServletRequest request, registerActionForm registerForm) {
      boolean b = false;
      try {
      Context ctx=new InitialContext();
      Connection conn=null;
      DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
      conn=ds.getConnection();
      String userid = registerForm.getUserid();
      String username = registerForm.getUsername();
      String password = registerForm.getPassword();
      String email = registerForm.getEmail();
      String telephone = registerForm.getTelephone();
      String userrole = registerForm.getUserrole();
      if("1".equals(userrole)){


userrole = "管理员";

}else if("2".equals(userrole)){


userrole = "专业用户";

}
String insSql = new String("insert into UserInfo(userid,username,password," +
"email,telephone,userrole) values(?,?,?,?,?,?)");
PreparedStatement preSQL = conn.prepareStatement(insSql);
userid = new String(userid.getBytes("GB2312"));
preSQL.setString(1,userid);
username = new String(username.getBytes("GB2312"));
System.out.println("#####"+username);
preSQL.setString(2,username);
password = new String(password.getBytes("GB2312"));
preSQL.setString(3,password);
email = new String(email.getBytes("GB2312"));
preSQL.setString(4,email);
telephone = new String(telephone.getBytes("GB2312"));
preSQL.setString(5,telephone);
userrole = new String(userrole.getBytes("GB2312"));
System.out.println("#####"+userrole);
preSQL.setString(6,userrole);
System.out.println("#####"+preSQL);
preSQL.executeUpdate();
preSQL.close();
conn.close();
b = true;
} catch (SQLException e) {
      e.printStackTrace();
} catch (UnsupportedEncodingException e) {
      e.printStackTrace();
} catch (NamingException e) {
      e.printStackTrace();
}
return b;
}
private boolean useridcheck(HttpServletRequest request, registerActionForm registerForm) {
      boolean checkpass = true;
      try {
            Context ctx=new InitialContext();
            Connection conn=null;
            DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
            conn=ds.getConnection();
            String userid = registerForm.getUserid();
            String sqlStr = new String("select * from UserInfo where userid=?");
            PreparedStatement preSQLSelect = conn.prepareStatement(sqlStr,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            userid = new String(userid.getBytes("GB2312"));
            preSQLSelect.setString(1,userid);
            ResultSet rs = preSQLSelect.executeQuery();
            if(rs.next()){
                  checkpass = false;
            }
            rs.close();
            preSQLSelect.close();
            conn.close();
      } catch (SQLException e) {
            e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
      } catch (NamingException e) {
            e.printStackTrace();
      }
      return checkpass;
}
}


经过调试,每执行到用黑体字标注的那一段时,打出来的信息都是乱码:

#####com.mysql.jdbc.PreparedStatement@f41393: insert into UserInfo(userid,username,password,email,telephone?userrole) values('bird','bird','123','bird@163.com','01082316463','????')
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?userrole) values('bird','bird','123','bird@163.com','01082316463','????')' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at action.registerAction.doRegister(registerAction.java:104)
at action.registerAction.execute(registerAction.java:40)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:39)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:834)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
at java.lang.Thread.run(Unknown Source)


求助!!!

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论