解决在IDEA编写Java代码时,向数据库中插入中文字符后显示?乱码问题

博主 3517 2020-07-02

在IDEA中编写代码时,向数据库(我使用的MySQL)中插入一条数据后,数据库中中文字符显示乱码问题,如图
image.png

解决的方法是:
一、确保IDEA中的文件都使用UTF-8编码方式
1)在IDEA中点击Perferences进入设置界面
image.png

2)找到Editor下的File Encodings
image.png

3)将GLobal Encoding和Project Encoding编码方式变为UTF-8
image.png

二、一般进行JDBC连接时,通过加载配置文件的方式进行驱动注册和数据库连接
在配置文件中(此处为properties文件)的url链接最后面加上
?useUnicode=true&characterEncoding=utf8
如图
image.png

三、确保数据库、表、字段等编码也是UTF-8方式

1)查看全局变量和会话变量的字符集
SHOW GLOBAL VARIables LIKE '%CHARACTER%';
SHOW SESSION VARIABLES LIKE '%CHARACTER%';
image.png

2)通过set命令将全局变量编码字符集全部转换为UTF-8,会话变量同理,将global改为session即可
SET @@global.character_set_client = 'utf8';
SET @@global.character_set_connection = 'utf8';
SET @@global.character_set_database = 'utf8';
SET @@global.character_set_results = 'utf8';
SET @@global.character_set_server = 'utf8';
SET @@global.character_set_system = 'utf8';

3)查看表和字段的字符集,确保也为UTF-8编码
查看表字符集
SHOW CREATE TABLE tableName;
image.png

修改表的字符集
方式一:
ALTER TABLE tableName CONVERT TO CHARACTER SET utf8 【COLLATE utf8_general_ci】;
方式二:
ALTER TABLE tableName DEFAULT CHARACTER SET utf8 【COLLATE utf8_general_ci】;

查看字段字符集
SHOW FULL COLUMNS FROM tableName;
image.png

修改字段字符集
方式一:
ALTER TABLE tableName CHANGE fieldName fieldNewName 字段类型 CHARACTER SET utf8 【COLLATE utf8_general_ci】;
方式二:
ALTER TABLE tableName MODIFY fieldName 字段类型 CHARACTER SET utf8 【COLLATE utf8_general_ci】;

这样再向表中插入数据,并查询数据就不会显示中文乱码了。

@Test
    public void test1() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;//?占位符
        try {
            //加载配置文件
            ClassLoader classLoader = PreparedStatementUpdateTest.class.getClassLoader();
            InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);

            String driver = properties.getProperty("driver");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");

            //注册驱动
            Class.forName(driver);
            //获取连接
            connection = DriverManager.getConnection(url, user, password);
            //System.out.println(connection);

            //编写sql语句
            String sql = "insert  into customers(name, email, birth) values (?, ?, ?)";

            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, "哪吒nazha");
            preparedStatement.setString(2, "nezha@gmail.com");

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date parse = simpleDateFormat.parse("2002-01-01");
            preparedStatement.setDate(3, new Date(parse.getTime()));

            //执行操作
            boolean execute = preparedStatement.execute();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            //资源关闭
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


        }

    }

查询结果:
image.png