關於jdbc 增刪改通用的方法,只需要傳sql語句和查詢條件
//第二個引數為給佔位符賦值,可以理解為一個數組
//例如executeUpdate("select *from user where id=? and pwd =?","admin","123")
public int executeUpdate(String sql,Object...obj){
Connection conn = getConnection();PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
//給佔位符賦值
if(obj.length>0){
for (int i = 0; i < obj.length; i++) {
ps.setObject(i+1, obj[i]);
}
}
return ps.executeUpdate(); //執行SQL並返回影響行數
} catch (Exception e) {
// TODO: handle exception
}finally{
closeAll(conn, ps, null);
}
return 0;
}