mysql取出中文乱码_mysql数据库取中文列名 乱码问题

在创建数据库的时候,我用的是中文的列名,其中插入数据的时候也有中文字符 要想在mysql下能够正常的显示中文,我们可以在建库的时候加入一条sql语句 mysql->set names gb2312 数据库是建立好了,但在用java连的时候又出现了问题,显示在jtable上的列名仍然是乱码,可是列的内容是正常的中文,为什么会这样呢? 查了很多资料,大部分都是说要用读字节的方法,即是 byte[]  bytes=new byte("iso-8859-1")                                                                                                   String s=new String(bytes,"big5"); 可是我这样做了仍然没有效果,所以我想问题并不是处在编码上面,因为编码都已经是gb2312.所以我用仔细的审了一遍程序,发现 前面取 数据库列名的时候用的是     getColumnName()    方法,api上getColumnName()  方法的意思是获取指定列的名称。 我觉得这个有点问题    于是把getColumnName()方法换成 getColumnLabel()方法,没有想到竟然在jtable上正常显示了中文列名,真的非常高兴, 以此志给那些仍然没解决问题的爷们一点 思路,下面是我附录的代码: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {         Vector columnHead=new Vector();         Vector rows=new Vector();                 String url = "jdbc:mysql://localhost:3306/perfect?useUnicode=true&character.Encoding=gb2312";         String username = "root";         String password = "root";         String drivername = "org.gjt.mm.mysql.Driver";         Connection conn;         Statement statement;         ResultSet resultset;         ResultSetMetaData rsmd;         try {             Class.forName(drivername).newInstance();             conn = DriverManager.getConnection(url, username, password);             statement=conn.createStatement();                                     //resultset=statement.executeQuery("set names gb2312");             resultset=statement.executeQuery("select * from baseinfo");             rsmd=resultset.getMetaData();                         for(int i=1;i<=rsmd.getColumnCount();++i){                 columnHead.addElement(rsmd.getColumnLabel(i));                 //byte[] bytes=rsmd.getColumnName(i).getBytes("iso-8859-1");                 //String s=new String(bytes,"big5");                 //columnHead.addElement(s);             }             while(resultset.next()){             Vector current=new Vector();             for(int i=1;i<=rsmd.getColumnCount();++i){                 current.addElement(resultset.getString(i));             }             rows.addElement(current);             }             JTable jtable=new JTable(rows,columnHead);             jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);             for(int i=0;i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值