日期:2014-05-20  浏览次数:20799 次

求助:JAVA查询MYSQL语句有中文返回为空
MYSQL配置默认字符集是utf8,MyEclipse6.0设置默认字符集是utf8,数据库表中含中文。
Java code

public class TestPolm{
   public static void main(String args[]){
       try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Load driver failed??");
        }
       Connection con = null;
        PreparedStatement ps=null;
        ResultSet rs=null;
       
       
    try {
        
        
        con =  (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "1111");
        String sql = null;
        try {
            sql = new String("select * from ic_content_0002 where Title like '%窗口%'".getBytes(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();
        System.out.println(rs.getFetchSize());
        while(rs.next()){
            StringBuffer sb = new StringBuffer();
            sb.append(rs.getString("Id"));
        }
    
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   }
}



System.out.println(rs.getFetchSize());一直输出是0,字符集做过很多更改尝试都没用。数据库要求用UTF-8。网上N多都是查询结果是乱码,我这是根本查补出来.....

------解决方案--------------------
dddddddddddddddddddddddddddddd
------解决方案--------------------
mysql数据库里面的字符集应该用“gbk”,可以先在mysql数据库里面用测试下语句
------解决方案--------------------
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
------解决方案--------------------
("select * from ic_content_0002 where Title like '%窗口%'");
应该后面不用改字符集吧!....
------解决方案--------------------
语句单独在mysql中使用是否成功呢?
------解决方案--------------------
探讨
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8

------解决方案--------------------
探讨
引用:
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8

这串的问题吧,晕啊,被无视了