简单记录下,爬虫项目中需要用到的,细节代码就不贴出来了。
/**
* 增加、删除、改
* @param sql
* @param params
* @return
* @throws SQLException
*/
public static Integer updateByPreparedStatement(String sql, List<Object>params)throws SQLException{
int result = -1;
pstmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
int index = 1;
if(params != null && !params.isEmpty()){
for(int i=0; i<params.size(); i++){
pstmt.setObject(index++, params.get(i));
}
}
result = pstmt.executeUpdate();
ResultSet rs = pstmt.getGeneratedKeys();
Integer key = null;
if(rs.next()) {
key = rs.getInt(1);
}
return key;
}