t1441332354 2018-05-28 01:27 采纳率: 71.8%
浏览 2429
已结题

关于从jsp页面提交数据到数据库出现乱码的问题

这是jsp页面

 String ShipName = new String(request.getParameter("ShipName").getBytes("ISO8859_1"),"UTF-8");
        String project = new String(request.getParameter("project").getBytes("ISO8859_1"),"UTF-8");
        int number = Integer.parseInt(request.getParameter("test"));
        String name = (String)session.getAttribute("username");
        try{

            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://192.168.3.228:3305/YuDetect";
            String username = "bigdaddy";
            String password = "senya@2018";
            Connection conn = DriverManager.getConnection(url,username,password);
            String isExit = "select * from TestResult where ShipName = '"+ShipName+"' and DetectItem = '"+project+"'";
            PreparedStatement pstmt = conn.prepareStatement(isExit);
            ResultSet rs = pstmt.executeQuery();
            if(rs.next()){
                out.print("该数据已存在,请不要重复添加!");
            }else{              
                String sql = "insert into TestResult(ShipName,DetectItem,Val_detect,Detector) values(?,?,?,?)";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1,ShipName);
                ps.setString(2,project);
                ps.setInt(3,number);
                ps.setString(4,name);
                int row = ps.executeUpdate();
                if(row > 0){
                    out.print("成功添加数据!");
                }
                ps.close();
                conn.close();
                }
        }catch(Exception e){
            out.print("数据添加失败!");
            e.printStackTrace();
        }

这是mysql数据库的字符集
图片说明
现在可以确定的是,传到jsp页面的数据里没有乱码,“ShipName”可以正常显示中文,我觉得关键的问题在于往数据库里添加的时候出了编码问题,但是我数据库的编码格式也是utf-8(UTF-8和UTF8mb4都试过)结果都是
图片说明
想问问还有没有什么解决方法

  • 写回答

16条回答 默认 最新

  • Mr.ADiao 2018-05-28 02:37
    关注

    造成中文乱码的原因有:
    1.如果你用的是SSM,那么你看web.xml里边是否配置了中文乱码处理


    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter

    encoding
    UTF-8



    CharacterEncodingFilter
    /*

    2.还有可能表单提交时 ,以post方式提交 tomat也需要配置中文乱码
    connectionTimeout="20000"
    redirectPort="8443" URIEncoding="UTF-8" />
    3. 还有就是 数据库的问题 在安装mysql数据库时可以选择字符集 为UTF-8 如果没有选择 也可能出现中文乱码问题

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型