1.url中用?useUnicode=true&characterEncoding=UTF-8
public static Connection getConnection() throws SQLException {
Connection con = null; //创建用于连接数据库的Connection对象
try {
Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动
con = (Connection) DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/company?useUnicode=true&characterEncoding=UTF-8", "root", "");// 创建数据连接
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
// con.close();
return con; //返回所建立的数据库连接
}
2.url中用?useUnicode=true&characterEncoding=UTF-8
jdbc:mysql://172.16.9.6:3306/school?useUnicode=true&characterEncoding=UTF-8 |
|