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回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星