linux下idea插入数据库中文乱码问题

首先介绍一下我的开发环境:

       mysql,idea,navicat 

首先排除其他比如说utf-8 之类的没设置。我这边测试了一下通过控制台插入式不乱码的。

解决方法:试试下面的方法。

    @Test
    public void test4(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/huangshun?characterEncoding=UTF-8", "root", "root");
                String sql ="insert into student (name,age,sex) value (?,?,?)";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,"啥打算的1133");
                preparedStatement.setInt(2,17);
                preparedStatement.setString(3,"女");
                int i = preparedStatement.executeUpdate();
                System.out.println("i====="+i);

            } catch (SQLException e) {
                e.printStackTrace();
            }
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

  

猜你喜欢

转载自blog.csdn.net/huangshun9541/article/details/87972016