关闭

在Java中解决MySQL5.0中文字符集问题全程详细记录

发表于:2007-8-17 15:59

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:zieckey    来源:zieckey的博客

2 rows in set (0.00 sec)
mysql>
  但是如果重启数据库客户端,那么还是不能正确显示中文,
  最彻底的方法就是更改MySQL的配置文件。
  在MySQL5.0的安装目录找到my.ini文件,将其中以下两行:
default-character-set=utf8
default-character-set=utf8
改为:
default-character-set=gbk
default-character-set=gbk
  重启MySQL服务器。
重新连接:
mysql> select * from a;
+----------------------+
| a                    |
+----------------------+
| 默认数据库的字符集和 |
| 当然是联系 java      |
+----------------------+
2 rows in set (0.02 sec)
  现在好了,可以直接读取中文字符了。
  看看插入中文字符情况:
mysql> insert b values('看看插入中文字符情况:');
mysql> insert b values('看看插入中文字符情况:');
Query OK, 1 row affected (0.03 sec)
mysql> select * from b;
+------------------------+
| a                      |
+------------------------+
| 默认数据库的字符集和   |
| 看看插入中文字符情况: |
+------------------------+
2 rows in set (0.00 sec)
mysql>
  下面再看看创建表的情况:
mysql> create table c(a text);
Query OK, 0 rows affected (0.06 sec)
mysql> insert into c values();
Query OK, 1 row affected (0.01 sec)
mysql> insert into c values('下面再看看创建表的情况:');
Query OK, 1 row affected (0.02 sec)
mysql> select * from c;
+--------------------------+
| a                        |
+--------------------------+
| NULL                     |
| 下面再看看创建表的情况: |
+--------------------------+
2 rows in set (0.00 sec)
mysql>
一切正常了。
下面看看程序中的情况:
 
package test;
/**
 * 该程序用来测试JDBC和MySQL连接,然后插入中文字符到数据库中。
 * 其中用到的表为:
 * CREATE TABLE MessageBoard (
 * ID int(10) unsigned NOT NULL default '0',
 * title varchar(255) NOT NULL default '',
 * content text NOT NULL,
 * author varchar(20) NOT NULL default '',
 * time datetime NOT NULL default '0000-00-00 00:00:00',
 * modifyTime datetime NOT NULL default '0000-00-00 00:00:00',
 * PRIMARY KEY (ID)
 * ) ;
 *
 */
import java.sql.*;
public class InsertChineseCharacterJDBC
{
 static
 {
  try
  {
   //驱动和odbc不同

   Class.forName ( "org.gjt.mm.mysql.Driver" );
   System.out.println ( "success loading mysql Driver...." );
  } catch ( Exception e )
  {
   System.out.println ( "Error loading mysql Driver....." );
   e.printStackTrace ( );
  }
 }
 /**
  * @param args
  */
 public static void main ( String agrs[] )
 {
  try
  {
   //连接参数与Access不同

   String url = "jdbc:mysql://localhost/BohaoDB";
   //建立连接

   Connection con = DriverManager.getConnection ( url, "root", "011124" );
   //建立发送SQL命令的Statement对象

   Statement stmt = con.createStatement ( );
   //返回查询结果

   int flag = stmt.executeUpdate ( "insert into MessageBoard(ID,title,content,author,time,modifyTime) values(1,'大家好!','我是新来,请多多关照!','zieckey','2007-01-28 20:03:50','2007-01-27 20:03:50');" );
   if ( 1==flag )
   {
    System.out.println ("插入中文字符到数据库成功!");
   }else {
    System.out.println ("插入中文字符到数据库失败!");
   }
   ResultSet rs = stmt.executeQuery ( "select * from MessageBoard" );

   /**
    * 取出列属性!
    */
   ResultSetMetaData md = rs.getMetaData ( );
   int col = md.getColumnCount ( );
   for ( int i = 1; i <= col; i++ )
   {
    System.out.println ( md.getColumnName ( i ) + "\t" );
   }
   /**
    * 取出列值!
    */
   while ( rs.next ( ) )
   {
    String strData = "";
    for ( int i = 1; i <= col; i++ )
    {
     strData = strData + rs.getString ( i ) + "\t";
    }
    System.out.println ( strData );
   }
   //断开Connection连接

   rs.close();
   stmt.close();
   con.close ( );
  } catch ( SQLException e )
  {
   e.printStackTrace ( );
  }
 }
}

 


运行输出:
success loading mysql Driver....
插入中文字符到数据库成功!
ID
title
content
author
time
modifyTime

22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号