IDEA输出中文时乱码

IDEA输出中文时乱码

我创建数据库时选了UTF-8 mb4

数据库连接语句也有UTF-8  为啥会乱码呢   下面是涉及到的所有代码

https://img1.sycdn.imooc.com//climg/625fb615094a559208480745.jpg

Stringurl="jdbc:mysql://localhost:3306/asduseSSL=false&useUnicode=true&characterEncoding=UTF-8&" +"serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";

https://img1.sycdn.imooc.com//climg/625fb49a09ab643708460398.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package command;
import com.mooc.h.DbUtils;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class truecommand implements command{
    @Override
    public void execute() {
        Connection conn=null;
        PreparedStatement pstmt=null;
        try {
            conn=DbUtils.getConnection();
            String sql="select * from goods";
            pstmt=conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
                Integer eno=rs.getInt(1);
                String ename=rs.getString(2);
                Float salary=rs.getFloat(3);
                String desp=rs.getString(4);
                System.out.println(eno+"-"+ename+"-"+salary+"-"+desp);
            }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DbUtils.closeConnection(null,pstmt,conn);
        }
    }
}
1
2
3
4
5
6
7
8
package com.mooc.sample;
import command.*;
public class Test {
    public static void main(String[] args) {
        command one=new truecommand();
        one.execute();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.mooc.h;
 
import java.sql.*;
 
public class DbUtils {
    /**
     * 创建新的数据库连接
     * @return 新的Connection对象
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/asd?useSSL=false&useUnicode=true&characterEncoding=UTF-8&" +
                "serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        Connection conn= DriverManager.getConnection(url,"root""www1473040752");
        return conn;
    }
 
    /**
     * 关闭连接,释放资源
     * @param rs 结果集对象
     * @param stmt Statement对象
     * @param conn Connection对象
     */
    public static void closeConnection(ResultSet rs, Statement stmt, Connection conn){
        try {
            if(rs!=null){
                rs.close();
            }
        catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if(stmt!=null){
                stmt.close();
            }
        catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if((conn != null && conn.isClosed()) == false){
                conn.close();}
        catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}
1
2
3
4
5
package command;
 
public interface command {
 public void execute();
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小小 2022-04-20 16:08:54

同学你好,先确认下存储的数据库中数据是否可以正常显示,是否存在乱码。

再将IDEA如下位置均设置为UTF-8后重新测试运行。

https://img1.sycdn.imooc.com//climg/625fbf8509f58c7f10130738.jpg

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码