url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
表结构
@Test
public void PreparedConnectionTest() {
Connection con = null;
PreparedStatement ps= null;
try {
//读取配置文件
InputStream is =ClassLoader.getSystemClassLoader().getResourceAsStream("init.properties");
Properties properties=new Properties();
properties.load(is);
String user = properties.getProperty("user");
String url = properties.getProperty("url");
String password = properties.getProperty("password");
String classDriver = properties.getProperty("classDriver");
//连接
Class.forName(classDriver);
con = DriverManager.getConnection(url, user, password);
//插入数据
String sql="insert into customers(name,email,birth)values(?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1,"哪吒");
ps.setString(2,"123@qq.com");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=sdf.parse("1988-11-11");
ps.setDate(3,new java.sql.Date(date.getTime()));
System.out.println(con.getAutoCommit());
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
插入乱码
不知道那一步出错了,搞了一天了😭
哪两个字是啥.还有就是用utf-8mb4好点
你的数据库连接url发出来看下。