需要修改代码

时间:2013-12-27 09:02:10

标签: java jdbc

以下代码需要修改我们可以使用常量文件,如果是,那么我们如何在单独的文件中隔离字符串,以便循环继续,然后性能不受影响

for (int j = 0; j < subconfigListRLC.size(); j++) {
    StringBuffer sqlQuery = new StringBuffer();
    test = (SubConfigurationDetailsObject) subconfigListRLC.get(j);
    if (test.getFlag().equalsIgnoreCase("T")) {
        sqlQuery = sqlQuery.append("update SUB_CONFIG set TSTED = " 
                + test.getSubConfigurationIndexNo() + " , Q_SUB_INDX = 0 "
                + "where BASE_ENG_KEY = '" + test.getBaseEngineKey() + "' "
                + "AND MODEL_YEAR = '" + modelYear + "' "
                + "AND RLHP_LVW = '" + test.getRoadLoadHorsepowerValue() + "' "
                + "AND LVW_TEST_WT_WO_CONT = '" + test.getEtwValue() + "' "
                + "AND INERTIA_WT_CLASS = '" + test.getInertiaWeightClassNo() + "' "
                + "AND TEST_GROUP_ID = " + test.getTestGroupId() + " "
                + "AND ENGINE_CODE = '" + test.getEngineCode() + "' "
                + "AND AXLE_RATIO = '" + test.getAxleRatioValue() + "'");
    } else if (test.getFlag().equalsIgnoreCase("U")) {
        sqlQuery = sqlQuery.append("update SUB_CONFIG set Q_SUB_INDX = " 
                + test.getSubConfigurationIndexNo() + " "
                + "where BASE_ENG_KEY = '" + test.getBaseEngineKey() + "' "
                + "AND MODEL_YEAR = '" + modelYear + "' "
                + "AND RLHP_LVW = '" + test.getRoadLoadHorsepowerValue() + "' "
                + "AND LVW_TEST_WT_WO_CONT = '" + test.getEtwValue() + "' "
                + "AND INERTIA_WT_CLASS = '" + test.getInertiaWeightClassNo() + "' "
                + "AND TEST_GROUP_ID = " + test.getTestGroupId() + " "
                + "AND ENGINE_CODE = '" + test.getEngineCode() + "' "
                + "AND AXLE_RATIO = '" + test.getAxleRatioValue() + "'");
    }
    //System.out.println("Query----------->"+sqlQuery.toString());
    processor.getUpdateAccessor().executeUpdateSql(sqlQuery.toString());
}

2 个答案:

答案 0 :(得分:1)

使用Java PreparedStatement来构建查询。然后,您可以将PreparedStatement的查询字符串存储在属性文件中。在进入循环之前将两个可能的查询字符串读入变量(事实上,您可能希望在进入循环之前构建两个PreparedStatements - 取决于您是否始终使用它们)。然后,您可以调用clearParamaters,然后设置新参数,执行,重复。

答案 1 :(得分:1)

当你要求确切的细节时,就像这样。在PreparedStatement上搜索javadocs。 Javadocs总是值得一读。

String sql = "update SUB_CONFIG set TSTED = ? , Q_SUB_INDX = ? " +
    "where BASE_ENG_KEY = ? " +
    "AND MODEL_YEAR = ? " +
    "AND RLHP_LVW = ? " +
    "AND LVW_TEST_WT_WO_CONT = ? " +
    "AND INERTIA_WT_CLASS = ? " +
    "AND TEST_GROUP_ID = ? " +
    "AND ENGINE_CODE = ? " +
    "AND AXLE_RATIO = ?");
PreparedStatement statement = connection.createPreparedStatement(sql);
for (SubConfigurationDetailsObject test: subconfigListRLC) {
    if (test.getFlag().equalsIgnoreCase("T")) {
        statement.setIntParam(1, test.getSubConfigurationIndexNo());
        statement.setIntParam(2, 0);
    } else if (test.getFlag().equalsIgnoreCase("U")) {
        statement.setIntParam(1, 0);
        statement.setIntParam(2, test.getSubConfigurationIndexNo());
    } else {
        continue;
    }
    statement.set...Param(3, ...);
    ...
    statement.executeUpdate();
 }

您可能只需要一个PreparedStatement,结果确实更快。 BTW。最好使用StringBuilder而不是StringBuffer。