IDEA输出中文时乱码
我创建数据库时选了UTF-8 mb4
数据库连接语句也有UTF-8 为啥会乱码呢 下面是涉及到的所有代码
Stringurl="jdbc:mysql://localhost:3306/asduseSSL=false&useUnicode=true&characterEncoding=UTF-8&" +"serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
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(); } |
40
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧