public static boolean updateData(Long id, String tableName, Map<String, Object> fields) throws BusinessException, SQLException { StringBuilder fieldNames = new StringBuilder(""); Iterator<Entry<String, Object>> iter = fields.entrySet().iterator(); if (fields.isEmpty()) { log.error("更新字段为空"); return true; } else { int count = 0; List<Object> fieldValues = new ArrayList<>(); while (iter.hasNext()) { ++count; Entry<String, Object> entry = iter.next(); String fieldName = (String) entry.getKey(); Object fieldValue = entry.getValue(); String splitStr = count == fields.size() ? "" : ","; /* 这段代码是一个Java中的三目运算符,它的作用是根据一个条件来决定一个字符串变量的值。 具体来说,这行代码的意思是:如果 count 等于 fields.size(),那么 splitStr 的值为一个空字符串 "",否则 splitStr 的值为逗号 ","。 通常情况下,这行代码用于在生成一个字符串时,根据某些条件来决定是否需要添加分隔符。如果这些条件不满足,就不需要添加分隔符,否则需要添加一个逗号分隔符。*/ fieldNames.append(fieldName + "=?" + splitStr); fieldValues.add(fieldValue); } JDBCAgent jdbc = null; String sql = "update " + tableName + " set " + fieldNames.toString() + " where id =?"; fieldValues.add(id); boolean isUpdate; try { jdbc = new JDBCAgent(); isUpdate = jdbc.execute(sql, fieldValues) > 0; } catch (Exception var15) { log.error(sql); log.error(fieldValues); log.error(var15.getMessage(), var15); throw var15; } finally { if (null != jdbc) { jdbc.close(); } } return isUpdate; } }参考这个把 String sql = "update smsserver_out set status = 'Z' where user_id in (?) and status in ('U','Q')";这个往里面套 防止sql注入
时间: 2024-01-28 15:03:31 浏览: 25
可以使用预编译的 SQL 语句,例如:
```
String sql = "update smsserver_out set status = 'Z' where user_id in (?) and status in ('U','Q')";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setLong(1, userId);
statement.executeUpdate();
```
其中,`?` 是一个占位符,需要在执行语句前使用 `setXXX` 方法为其设置具体的值,这样可以避免 SQL 注入攻击。
相关问题
java实现将list中对象的省市区统计成 List<Map<String,List<Map<String,List<Map<String,Object>>>>>>结构
您好!要实现将一个List中的对象的省市区统计成一个嵌套的List<Map<String, List<Map<String, List<Map<String, Object>>>>>>结构,您可以按照以下步骤进行操作:
1. 创建一个空的结果列表:
```java
List<Map<String, List<Map<String, List<Map<String, Object>>>>>> result = new ArrayList<>();
```
2. 遍历原始的List对象,对每个对象进行省市区的统计:
```java
for (YourObject obj : yourList) {
String province = obj.getProvince();
String city = obj.getCity();
String district = obj.getDistrict();
// 检查省份是否已存在于结果列表中
boolean provinceExists = false;
for (Map<String, List<Map<String, List<Map<String, Object>>>>> provinceMap : result) {
if (provinceMap.containsKey(province)) {
provinceExists = true;
// 检查城市是否已存在于省份中
boolean cityExists = false;
List<Map<String, List<Map<String, Object>>>> cityList = provinceMap.get(province);
for (Map<String, List<Map<String, Object>>> cityMap : cityList) {
if (cityMap.containsKey(city)) {
cityExists = true;
// 检查区县是否已存在于城市中
boolean districtExists = false;
List<Map<String, Object>> districtList = cityMap.get(city);
for (Map<String, Object> districtMap : districtList) {
if (districtMap.containsKey(district)) {
districtExists = true;
// 在区县中添加其他统计数据
// districtMap.put("其他键", 其他值);
break;
}
}
// 若区县不存在于城市中,则添加新的区县
if (!districtExists) {
Map<String, Object> newDistrictMap = new HashMap<>();
// 在新的区县中添加其他统计数据
// newDistrictMap.put("其他键", 其他值);
districtList.add(newDistrictMap);
}
break;
}
}
// 若城市不存在于省份中,则添加新的城市和区县
if (!cityExists) {
Map<String, List<Map<String, Object>>> newCityMap = new HashMap<>();
List<Map<String, Object>> newDistrictList = new ArrayList<>();
Map<String, Object> newDistrictMap = new HashMap<>();
// 在新的区县中添加其他统计数据
// newDistrictMap.put("其他键", 其他值);
newDistrictList.add(newDistrictMap);
newCityMap.put(city, newDistrictList);
cityList.add(newCityMap);
}
break;
}
}
// 若省份不存在于结果列表中,则添加新的省份、城市和区县
if (!provinceExists) {
Map<String, Object> newDistrictMap = new HashMap<>();
// 在新的区县中添加其他统计数据
// newDistrictMap.put("其他键", 其他值);
List<Map<String, Object>> newDistrictList = new ArrayList<>();
newDistrictList.add(newDistrictMap);
Map<String, List<Map<String, Object>>> newCityMap = new HashMap<>();
newCityMap.put(city, newDistrictList);
List<Map<String, List<Map<String, Object>>>> newCityList = new ArrayList<>();
newCityList.add(newCityMap);
Map<String, List<Map<String, List<Map<String, Object>>>>> newProvinceMap = new HashMap<>();
newProvinceMap.put(province, newCityList);
result.add(newProvinceMap);
}
}
```
3. 最终的结果会保存在result列表中,其中每个元素都是一个省份的Map对象,包含省份名称作为键,相应的城市列表作为值。每个城市列表中的元素是一个城市的Map对象,包含城市名称作为键,相应的区县列表作为值。每个区县列表中的元素是一个区县的Map对象,您可以在其中添加其他统计数据。
请注意,上述代码仅提供了一种基本实现思路,具体根据您的数据结构和需求进行相应的调整和优化。希望对您有所帮助!如有更多问题,请随时提问。
/** * 根据formBean修改指定参数 / public static boolean updateData(Long id, String tableName, Map<String, Object> fields) throws BusinessException, SQLException { StringBuilder fieldNames = new StringBuilder(""); Iterator<Entry<String, Object>> iter = fields.entrySet().iterator(); if (fields.isEmpty()) { log.error("更新字段为空"); return true; } else { int count = 0; List<Object> fieldValues = new ArrayList<>(); while (iter.hasNext()) { ++count; Entry<String, Object> entry = iter.next(); String fieldName = (String) entry.getKey(); Object fieldValue = entry.getValue(); String splitStr = count == fields.size() ? "" : ","; / 这段代码是一个Java中的三目运算符,它的作用是根据一个条件来决定一个字符串变量的值。 具体来说,这行代码的意思是:如果 count 等于 fields.size(),那么 splitStr 的值为一个空字符串 "",否则 splitStr 的值为逗号 ","。 通常情况下,这行代码用于在生成一个字符串时,根据某些条件来决定是否需要添加分隔符。如果这些条件不满足,就不需要添加分隔符,否则需要添加一个逗号分隔符。*/ fieldNames.append(fieldName + "=?" + splitStr); fieldValues.add(fieldValue); } JDBCAgent jdbc = null; String sql = "update " + tableName + " set " + fieldNames.toString() + " where id =?"; fieldValues.add(id); boolean isUpdate; try { jdbc = new JDBCAgent(); isUpdate = jdbc.execute(sql, fieldValues) > 0; } catch (Exception var15) { log.error(sql); log.error(fieldValues); log.error(var15.getMessage(), var15); throw var15; } finally { if (null != jdbc) { jdbc.close(); } } return isUpdate; } }以这样的形式写一个在java代码中,修改数据库表名为smsserver_out的字段status,条件是如果第一:数据库中字段status的值为U或者Q,第二:user_id等于前端传过来的值,需要考虑的情况为考虑user_id可能是一个也有可能是多个,也有可能为空,为防止sql注入,要采用预编译的方式
可以将下面的代码作为修改数据库表字段的方法,在方法中传入需要修改的参数,方法中使用预编译的方式来防止SQL注入:
```
public static boolean updateData(Long id, String tableName, Map<String, Object> fields, String userId) throws BusinessException, SQLException {
StringBuilder fieldNames = new StringBuilder("");
Iterator<Entry<String, Object>> iter = fields.entrySet().iterator();
if (fields.isEmpty()) {
log.error("更新字段为空");
return true;
} else {
int count = 0;
List<Object> fieldValues = new ArrayList<>();
while (iter.hasNext()) {
++count;
Entry<String, Object> entry = iter.next();
String fieldName = (String) entry.getKey();
Object fieldValue = entry.getValue();
String splitStr = count == fields.size() ? "" : ",";
fieldNames.append(fieldName + "=?" + splitStr);
fieldValues.add(fieldValue);
}
JDBCAgent jdbc = null;
String sql = "update " + tableName + " set " + fieldNames.toString() + " where user_id in (?) and status in ('U','Q')";
if (userId != null && !userId.isEmpty()) {
sql = sql.replace("?", userId);
} else {
sql = sql.replace("?", "''");
}
fieldValues.add(id);
boolean isUpdate;
try {
jdbc = new JDBCAgent();
PreparedStatement stmt = jdbc.getPreparedStatement(sql);
for (int i = 0; i < fieldValues.size(); i++) {
stmt.setObject(i + 1, fieldValues.get(i));
}
isUpdate = stmt.executeUpdate() > 0;
} catch (Exception var15) {
log.error(sql);
log.error(fieldValues);
log.error(var15.getMessage(), var15);
throw var15;
} finally {
if (null != jdbc) {
jdbc.close();
}
}
return isUpdate;
}
}
```
在调用该方法时,传入需要修改的参数即可:
```
Map<String, Object> fields = new HashMap<>();
fields.put("status", "V");
String userId = "123456";
boolean result = updateData(1L, "smsserver_out", fields, userId);
```
其中,1L代表需要修改的记录的ID,"smsserver_out"代表需要修改的表名,fields是Map类型的参数,存储需要修改的字段及其对应的值,userId是前端传过来的用户ID。如果用户ID为空,则使用''来代替。在方法中,使用预编译的方式来防止SQL注入。