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个回答

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

你传的两个参数是什么

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐