QQ171941
TopCoderMyDream
采纳率0%
2017-01-19 04:24

jdbc查询mysql单列发生中文乱码

环境:
jdk:1.7
mysql:5.0.96
我使用jdbc方式连接数据库,where过滤查询一条数据,查出来后发现有一列是乱码的,这一列刚好是中文,然后我惊奇的发现后面两列的中文竟然不是乱码,试了好多办法,都无果,希望各路大神能够指教一二;

我mysql的默认编码已经改为utf-8了,.java文档编码也改为utf-8了

 StudentScore sc2 = new StudentScore();
        StudentScore scResult = new StudentScore();
        sc2.setName("高三");
        sc2.setGrade("高二");
        String selSql = "select * from studentscore where grade=? and name=?";
        java.sql.Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        java.sql.PreparedStatement statm = connection.prepareStatement(selSql);
        statm.setString(1, sc2.getGrade());
        statm.setString(2, sc2.getName());
        ResultSet rs = statm.executeQuery();
        while(rs.next()){
            scResult.setId(rs.getInt("id"));
            String name = rs.getString("name");
//          String name2 = new String(name.getBytes(),"utf-8");
            scResult.setName(rs.getString("name"));
            scResult.setYuwen(rs.getInt("yuwen"));
            scResult.setShuxue(rs.getInt("shuxue"));
            scResult.setYingyu(rs.getInt("yingyu"));
            scResult.setZy1(rs.getInt("zy1"));
            scResult.setZy2(rs.getInt("zy2"));
            scResult.setTotal(rs.getInt("total"));
            scResult.setBanRanking(rs.getInt("banranking"));
            scResult.setJiRanking(rs.getInt("jiranking"));
            scResult.setGrade(rs.getString("grade"));
            scResult.setSchool(rs.getString("school"));
        }
        statm.close();
        rs.close();
        connection.close();
        System.out.println(scResult);

图片说明
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • HGoooonG HGoooonG 4年前

    你传的两个参数是什么

    点赞 评论 复制链接分享
  • JE_GE JE_GE 4年前

    确认mysql安装目录my.ini里面2个编码是否为utf8
    现在是奇数就乱码

    点赞 评论 复制链接分享

相关推荐