在使用JDBC操作数据库 在向数据库中插入图片时,程序老是抛异常
原来的MYSQL字符集是GBK 但是这种字符集虽然支持中文 但是当插入图片等二进制数据时 会出现错误
解决方法:
将数据库字符集更改为gb2312或utf-8
1.在MYSQL DOS界面中输入 show variables like "%char%";
- +--------------------------+---------------+
- | Variable_name | Value |
- +--------------------------+---------------+
- | character_set_client | latin1 |
- | character_set_connection | latin1 |
- | character_set_database | latin1 |
- | character_set_filesystem | binary |
- | character_set_results | latin1 |
- | character_set_server | latin1 |
- | character_set_system | utf8 |
- +--------------------------+---------------+
1 、修改数据库默认字符集:
打开C:/Program Files/MySQL/MySQL Server 5.0 里面的my.ini 文件找到这段文字:
引用
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=latin1
把default-character-set=latin1 改为default-character-set=gb2312
2 、如果你是导入的数据库,需要对my.ini 做如下修改找到
引用
[client]
port=3306
[mysql]
default-character-set=latin1
把 default-character-set=latin1 改为default-character-set=gb2312
引用
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
找到
增加default-character-set=gbk
然后在设置一下你导入的数据库字符集即可:
命令:alter database 你的数据库名 charset = gb2312;
3. 重启MYSQL show variables like "%char%"
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | gb2312 |
| character_set_connection | gb2312 |
| character_set_database | gb2312 |
| character_set_filesystem | binary |
| character_set_results | gb2312 |
| character_set_server | gb2312 |
| character_set_system | utf8 |
| character_sets_dir | D:/Program Files/MySQL/MySQL Server 5.0/share/charsets/ |
+--------------------------+---------------------------------------------------------+
4.重启MYSQL 就可以解决问题了