优化代码 String bmid = PubUtil.valueOf(listBXD.get("BXD_BMID")); String year = PubUtil.valueOf(listBXD.get("BXD_BXRQ")).substring(0, 4); String shsj = PubUtil.valueOf(listBXD.get("BXD_SHSJ")); String xmid = PubUtil.valueOf(listBXD.get("BXD_XMID")); String jhbh = PubUtil.valueOf(listBXD.get("BXD_XMJH_JHBH")); //部门年度发生额 String sql = "select ISNUll(sum(BXD_JE),0) as \"je\" from BXD where BXD_BMID = '"+bmid+"' " + "and BXD_SHBZ = 'Y' and DATEPART(YYYY,BXD_BXRQ) = '"+year+"'"; Double deptMoney = PubFunAction.queryForObj(sql, Double.class, 0.0); //更新部门年度发生额 sql = "update DBUDGET set DBUDGET_SJZC = '"+deptMoney+"' where DBUDGET_DEPTID = '"+bmid+"' and DBUDGET_YEAR = '"+year+"'"; PubFunAction.jt.update(sql); //项目年度发生额 sql = "select ISNULL(sum(BXD_JE),0) as \"je\" from BXD where BXD_XMID = '"+xmid+"' " + "and BXD_SHBZ = 'Y' and DATEPART(YYYY,BXD_BXRQ) = '"+year+"'"; Double projectMoney = PubFunAction.queryForObj(sql,Double.class,0.0); //更新项目年度发生额 sql = "update DBUDGETMX set DBUDGETMX_SJZC = '"+projectMoney+"' where DBUDGETMX_XMID = '"+xmid+"' and DBUDGETMX_YEAR = '"+year+"'"; PubFunAction.jt.update(sql); //经济分类年度发生额 for (Map<String, Object> bxdmx : listBXDMX) { String lbid = PubUtil.valueOf(bxdmx.get("BXDMX_BXLB_LBID")); sql = "select ISNULL(sum(BXDMX_BXJE),0) as \"je\" from BXD left join BXDMX on BXDMX_KEY=BXD_KEY " + "where BXDMX_BXLB_LBID = '"+lbid+"' and BXD_SHBZ = 'Y' and DATEPART(YYYY,BXD_BXRQ) = '"+year+"'"; Double economicClassificationMoney = PubFunAction.queryForObj(sql, Double.class, 0.0); } //更新合同计划完成标记 if (!"".equals(jhbh)){ sql = "update XMJH set XMJH_WCBZ = 'Y',XMJH_SJRQ_END = '"+shsj+"' where XMJH_JHBH = '"+jhbh+"'"; PubFunAction.jt.update(sql); }
时间: 2024-03-20 21:39:03 浏览: 84
代码优化方式
可以优化的地方有:
1. 使用StringBuilder来拼接字符串,避免频繁地创建String对象。
2. 使用PreparedStatement来代替Statement,可以避免SQL注入攻击。
3. 将数据库连接池化,避免每次操作都创建和释放连接的开销。
4. 将查询结果缓存起来,避免重复查询。
5. 对于循环中的SQL语句,可以使用批量操作来提高效率。
6. 对于日期格式化操作,可以考虑使用SimpleDateFormat来优化。
7. 对于字符串转换成数字的操作,可以使用Double.parseDouble()方法来代替valueOf()方法。
8. 对于字符串的比较,可以使用equals()方法来代替"=="操作。
综合以上优化建议,可以对代码进行如下改进:
```
StringBuilder sqlBuilder = new StringBuilder();
String bmid = PubUtil.valueOf(listBXD.get("BXD_BMID"));
String year = PubUtil.valueOf(listBXD.get("BXD_BXRQ")).substring(0, 4);
String shsj = PubUtil.valueOf(listBXD.get("BXD_SHSJ"));
String xmid = PubUtil.valueOf(listBXD.get("BXD_XMID"));
String jhbh = PubUtil.valueOf(listBXD.get("BXD_XMJH_JHBH"));
//部门年度发生额
sqlBuilder.append("select ISNUll(sum(BXD_JE),0) as \"je\" from BXD where BXD_BMID = ? and BXD_SHBZ = 'Y' and DATEPART(YYYY,BXD_BXRQ) = ?");
Double deptMoney = PubFunAction.queryForObj(sqlBuilder.toString(), Double.class, 0.0, bmid, year);
//更新部门年度发生额
sqlBuilder.setLength(0);
sqlBuilder.append("update DBUDGET set DBUDGET_SJZC = ? where DBUDGET_DEPTID = ? and DBUDGET_YEAR = ?");
PubFunAction.jt.update(sqlBuilder.toString(), deptMoney, bmid, year);
//项目年度发生额
sqlBuilder.setLength(0);
sqlBuilder.append("select ISNULL(sum(BXD_JE),0) as \"je\" from BXD where BXD_XMID = ? and BXD_SHBZ = 'Y' and DATEPART(YYYY,BXD_BXRQ) = ?");
Double projectMoney = PubFunAction.queryForObj(sqlBuilder.toString(),Double.class,0.0, xmid, year);
//更新项目年度发生额
sqlBuilder.setLength(0);
sqlBuilder.append("update DBUDGETMX set DBUDGETMX_SJZC = ? where DBUDGETMX_XMID = ? and DBUDGETMX_YEAR = ?");
PubFunAction.jt.update(sqlBuilder.toString(), projectMoney, xmid, year);
//经济分类年度发生额
sqlBuilder.setLength(0);
sqlBuilder.append("select ISNULL(sum(BXDMX_BXJE),0) as \"je\" from BXD left join BXDMX on BXDMX_KEY=BXD_KEY where BXDMX_BXLB_LBID = ? and BXD_SHBZ = 'Y' and DATEPART(YYYY,BXD_BXRQ) = ?");
for (Map<String, Object> bxdmx : listBXDMX) {
String lbid = PubUtil.valueOf(bxdmx.get("BXDMX_BXLB_LBID"));
Double economicClassificationMoney = PubFunAction.queryForObj(sqlBuilder.toString(), Double.class, 0.0, lbid, year);
}
//更新合同计划完成标记
if (!"".equals(jhbh)){
sqlBuilder.setLength(0);
sqlBuilder.append("update XMJH set XMJH_WCBZ = 'Y',XMJH_SJRQ_END = ? where XMJH_JHBH = ?");
PubFunAction.jt.update(sqlBuilder.toString(), shsj, jhbh);
}
```
阅读全文