IDEA输出中文时乱码
我创建数据库时选了UTF-8 mb4
数据库连接语句也有UTF-8 为啥会乱码呢 下面是涉及到的所有代码

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

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);
}
}
}package com.mooc.sample;
import command.*;
public class Test {
public static void main(String[] args) {
command one=new truecommand();
one.execute();
}
}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();
}
}
}package command;
public interface command {
public void execute();
}40
收起
正在回答 回答被采纳积分+1
1回答
2023版Java工程师
- 参与学习 人
- 提交作业 8790 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程





恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星