package com.kuang.lesson02;
import com.kuang.lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestInsert2 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
try {
conn = JdbcUtils.getConnection();
//区别 使用问号占位符
String sql = "INSERT INTO users (`id`,`NAME`,`PASSWORD`,`email`,`birthday`) VALUES (?,?,?,?,?)";
st = conn.prepareStatement(sql); //预编译,先写sql,但不执行
//3.手工给参数赋值
st.setInt(1,4);
st.setString(2,"lirui");
st.setString(3,"1333333");
st.setString(4,"24765@qq.com");
//注意点 sql.date 数据库
// util.Date Java 用new Date().getTime() 获得时间戳
st.setDate(5,new java.sql.Date(new Date().getTime()));
//4、执行
int i = st.executeUpdate();
if (i>0){
System.out.println("再次插入成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,null);
}
}
}