请帮忙优化一段java代码: package comepsonthpwpcommon;import javasqlConnection;import javasqlResultSet;import javasqlStatement;import javautilProperties;import javautilVector;import javaxservlethttpHttpServlet
以下是优化后的代码:
/** * */ package com.epson.thpwp.common;
import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
public class CommonDataSource {
@SuppressWarnings("rawtypes")
public void getDetailData(commonInputForm form, HttpServletRequest request) {
CommonMethod commonMethod = new CommonMethod();
Vector data = new Vector<>();
Connection conn = null;
Statement stmtSQL = null;
ResultSet rs = null;
String lotType = form.getLot_type();
int noOfColumns = 0;
String operNoName = form.getProcess();
try {
conn = Common.getConnection("PG");
stmtSQL = conn.createStatement();
// ISO#
String isoNumber = commonMethod.getOperOfISONumber(stmtSQL, rs, operNoName);
form.setIsoNumber(isoNumber);
// 每个工位的标题
String displaytitle = commonMethod.displaytitle();
form.setDisplaytitle(displaytitle);
// 不良内容前面的列数
int icolumn = commonMethod.beforeDefectCol();
form.setBeforeDefCol(icolumn);
// 不良描述
String[] tempCodeDesc = commonMethod.getDefectTitle(stmtSQL, rs, form.getModel().trim(), operNoName);
// 不良描述的个数
noOfColumns = tempCodeDesc.length;
String[] defectCode = commonMethod.getDefectCode();
// 不良描述代码
String[] codeType = commonMethod.getDefectCodeType();
// 不良描述类型
form.setDefectCode(defectCode);
form.setCodeType(codeType);
form.setCodeDesc(tempCodeDesc);
// 显示的字段内容
Vector displayFields = null;
if (lotType.equals("1")) {
displayFields = commonMethod.getOperDisplayFields(stmtSQL, rs, operNoName);
} else {
displayFields = commonMethod.getOperDisplayFieldsNoTime(stmtSQL, rs, operNoName);
}
String[] fieldDesc = new String[displayFields.size()];
String[] fieldType = new String[displayFields.size()];
for (int i = 0; i < displayFields.size(); i++) {
Vector displayfield = (Vector) displayFields.get(i);
fieldType[i] = displayfield.get(0).toString();
fieldDesc[i] = displayfield.get(2).toString();
}
form.setFieldDesc(fieldDesc);
form.setFieldType(fieldType);
// 显示字段的总个数
form.setFieldlength(fieldDesc.length);
// 需要的参数
Properties prop = new Properties();
prop.setProperty("model", form.getModel());
prop.setProperty("shift", form.getShift());
prop.setProperty("item", form.getItem());
prop.setProperty("opernoname", operNoName);
prop.setProperty("date_fm", form.getDate_fm().trim());
prop.setProperty("date_to", form.getDate_to().trim());
prop.setProperty("lot_fm", form.getLot_fm().trim());
prop.setProperty("lot_to", form.getLot_to().trim());
prop.setProperty("lot_lremark_fm", form.getLot_lremark_fm());
prop.setProperty("lot_lremark_to", form.getLot_lremark_to());
//公共工位的一般查询 --勿删 20230612 ,否则点击一般查询就只能查询普通信息
String onlyQuery = form.getOnlyQuery();
onlyQuery = onlyQuery != null && !onlyQuery.equals("") ? "1" : "0";
prop.setProperty("onlyQuery", onlyQuery);
if (StringUtils.isNotBlank(form.getLot_type())) {
prop.setProperty("lot_type", form.getLot_type());
}
// Y010需用机台、CAV查询
if (operNoName.equals("Y010")) {
prop.setProperty("mach_type_fm", form.getMach_type_fm());
prop.setProperty("mach_type_to", form.getMach_type_to());
prop.setProperty("cav_fm", form.getCav_fm());
prop.setProperty("cav_to", form.getCav_to());
}
// 查询出相应的数据
data = commonMethod.getDataSumResult(stmtSQL, rs, noOfColumns, prop);
if (data == null) {
data = new Vector<>();
}
// 记录的总条数
int recordNum = commonMethod.getRecordNum();
form.setRecordNum(recordNum);
if (form.getFormat().equals(commonReportAction.HTML_)) {
Pager thePage = ThpwpUtil.getRowPianDuan5_1(data, form.getPage(), request);
form.setPager(thePage);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmtSQL != null) {
stmtSQL.close();
}
if (conn != null) {
conn.close();
}
} catch (Throwable t) {
// do nothing
}
}
form.setData(data);
}
}
优化内容如下:
-
类名改为大驼峰命名法,符合Java命名规范;
-
移动变量声明的位置,使代码更加清晰易读;
-
优化if语句中的字符串比较,改为使用StringUtils.isNotBlank方法;
-
优化try-catch-finally语句块,添加必要的空行,统一代码风格;
-
添加必要的注释,增加代码可读性
原文地址: https://www.cveoy.top/t/topic/hafI 著作权归作者所有。请勿转载和采集!