问题描述
今天在学习JDBC时在向数据库表中插入数据时出现乱码问题,如图
代码如下
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, IOException, SQLException {
Properties pro = new Properties();
pro.load(new FileReader("src/mysql.properties"));
String url = pro.getProperty("url");
String user = pro.getProperty("user");
String password = pro.getProperty("password");
String driver = pro.getProperty("driver");
Class.forName(driver);
Connection con = DriverManager.getConnection(url,user,password);
System.out.println(con);
String sql = "insert into news values(1, '震惊', '某男子竟在大庭广众之下...')";
Statement statement = con.createStatement();
statement.executeUpdate(sql);
}
解决办法
1.将数据库和表的编码方式都修改为utf8,没有解决问题
2. 修改url
String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8";
结果:同样还是乱码。