在使用JDBC操作数据库时,常常调用PreparedStatement的对象使用setObject方法去遍历SQL语句传入的数据,
这时常常需要一个一个的去判断数据的类型,导致代码量成倍增加,而使用setObject可以自动识别SQL中的数据
类型,对大量数据量节约的时间是非常可观的。
public static int executeUpdate(String sql,Object...params) {
Connection conn = null;
PreparedStatement ps = null;
try {
//1.加载驱动
//2.连接数据库
conn = JDBCUtil.getConn();
//3.创建语句//st = conn.createStatement();
ps = conn.prepareStatement(sql);
//遍历参数
for(int i=0;i<params.length;i++) {
ps.setObject(i+1, params[i]);
}
//4.执行语句//st.executeLargeUpdate(sql);
return ps.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.close(conn, ps, null);
}
return 0;
}