//工具类和属性池和 Statment文章的一致
// 测试类
1 public class TestInsert { 2 public static void main(String[] args) { 3 Connection conn = null; 4 PreparedStatement st = null; 5 ResultSet rs = null; 6 try{ 7 conn = JdbcUtils.getConnection(); 8 //区别 9 //使用?占位符代替参数 10 String sql ="insert into users(id,name,password,email,birthday) values(?,?,?,?,?);"; 11 12 st = conn.prepareStatement(sql);//预编译 和createStstment的不同 13 14 //手动给参数赋值 15 st.setInt(1,4); 16 st.setString(2,"liujl"); 17 st.setString(3,"123456"); 18 st.setString(4,"[email protected]"); 19 //注意点 sql.Date 数据库 java.sql.Date() 20 // util.Date java new Date.getTime() 获得时间戳 21 st.setDate(5,new java.sql.Date(new Date().getTime())); 22 23 //执行 24 int i = st.executeUpdate(); 25 if(i>0){ 26 System.out.println("插入成功"); 27 } 28 } catch (SQLException e) { 29 e.printStackTrace(); 30 }finally { 31 JdbcUtils.release(conn,st,null); 32 } 33 } 34 }
标签:JDBC,加密,PerpareStatment,st,sql,Date,new,null,conn From: https://www.cnblogs.com/kidzxy/p/16792321.html