roadinspectionupdate.java
来自「Java的框架」· Java 代码 · 共 205 行
JAVA
205 行
package mcaps.apps.prrm.roadinspection.dao.jdbc.query;
import javax.sql.DataSource;
import mcaps.apps.prrm.roadinspection.dao.util.RoadInspectionField;
import mcaps.apps.prrm.roadinspection.model.RoadInspection;
import mcaps.apps.prrm.util.TableNameConstants;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;
/**
* This class extends SqlUpdate to perform JDBC Update
* operation to the road inspection table. This class is
* used to set the precompiled UPDATE SQL statement and
* to execute the UPDATE SQL statement.
*
* @author jov
* @date Oct 20, 2005
* @version 1.0.1.0
*/
public class RoadInspectionUpdate extends SqlUpdate {
private int[] paramFieldIdx;
/**
* Default constructor that accepts a datasource object to allow
* the connection to the database for update operation.
* @param ds the datasource object
*/
public RoadInspectionUpdate(DataSource ds) {
setDataSource(ds);
setDefaultParameterTypes();
setSql(getDefaultSQL());
compile();
}
/**
* Constructor that accepts a datasource object and other
* parammeters like whereClause, paramTypes and paramFieldIdx
* to enable the connection to the database for update operation.
* @param ds the datasource object
* @param whereClause where clause to refine the query
* @param paramTypes the array of parameter types
* @param paramFieldIdx the index of the road inspection field as set in
* the RoadInspectionField object
*/
public RoadInspectionUpdate(DataSource ds, String whereClause,
int[] paramTypes,
int[] paramFieldIdx) {
setDataSource(ds);
setParameter(paramTypes,paramFieldIdx);
setSql(getSQL(whereClause));
compile();
}
/**
* Execute the JDBC update operation using the values as
* in the road inspection object
* @param roadInspection the object the holds the field values to be inserted
* @return the number of rows affected by the update
*/
public int update(RoadInspection roadInspection) {
// TODO Exception code If not initialized throw excp
Object[] paramValues = new Object[] {
roadInspection.getDefectType(),
roadInspection.getDefectDetail(),
roadInspection.getRoadName(),
roadInspection.getLocation(),
roadInspection.getSeverity(),
roadInspection.getSummary(),
roadInspection.getSolution(),
roadInspection.getRepairRequired(),
roadInspection.getLastModifiedTime(),
roadInspection.getId()
};
return update(paramValues);
}
/**
* Generates the road inspection default SQL statement.
* @return the default sql statement.
*/
private String getDefaultSQL() {
//Note: No loop was written due to selective field update
StringBuffer sb = new StringBuffer();
sb.append("UPDATE ")
.append(TableNameConstants.ROADINSPECTION_TABLENAME)
.append(" SET ")
.append(RoadInspectionField.FIELDS[RoadInspectionField.DEFECTTYPE])
.append("=?,")
.append(RoadInspectionField.FIELDS[RoadInspectionField.DEFECTDETAIL])
.append("=?,")
.append(RoadInspectionField.FIELDS[RoadInspectionField.ROADNAME])
.append("=?,")
.append(RoadInspectionField.FIELDS[RoadInspectionField.LOCATION])
.append("=?,")
.append(RoadInspectionField.FIELDS[RoadInspectionField.SEVERITY])
.append("=?,")
.append(RoadInspectionField.FIELDS[RoadInspectionField.SUMMARY])
.append("=?,")
.append(RoadInspectionField.FIELDS[RoadInspectionField.SOLUTION])
.append("=?,")
.append(RoadInspectionField.FIELDS[RoadInspectionField.REPAIRREQUIRED])
.append("=?,")
.append(RoadInspectionField.FIELDS[RoadInspectionField.LASTMODIFIEDTIME])
.append("=?")
.append(" ")
.append("WHERE")
.append(" ")
.append(RoadInspectionField.FIELDS[RoadInspectionField.ID])
.append("=?");
return sb.toString();
}
/**
* Sets the default parameter types.
*/
private void setDefaultParameterTypes(){
//Note: No loop was written due to selective field update
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.DEFECTTYPE]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.DEFECTDETAIL]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.ROADNAME]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.LOCATION]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.SEVERITY]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.SUMMARY]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.SOLUTION]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.REPAIRREQUIRED]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.LASTMODIFIEDTIME]));
declareParameter(new SqlParameter(
RoadInspectionField.FIELDTYPES[RoadInspectionField.ID]));
}
/**
* Generates the road inspection custom SQL statement using the array
* of param indexes for selective update.
* @return the default sql statement.
*/
private String getSQL() {
//Note: No loop was written due to selective field update
StringBuffer sb = new StringBuffer();
sb.append("UPDATE").append(" ")
.append(TableNameConstants.ROADINSPECTION_TABLENAME).append(" ")
.append("SET").append(" ");
for (int i = 0; i < paramFieldIdx.length; i++) {
sb.append(RoadInspectionField.FIELDS[paramFieldIdx[i]])
.append("=?");
if (i < paramFieldIdx.length-1){
sb.append(",");
}
}
return sb.toString();
}
/**
* Generates the road inspection custom SQL statement using the
* where clause statement for selective update.
* @param whereClause
* @return the default sql statement.
*/
private String getSQL(String whereClause) {
StringBuffer sb = new StringBuffer();
sb.append(getSQL()).append(" ")
.append("WHERE").append(" ")
.append(whereClause);
return sb.toString();
}
/**
* Sets the custom parameter types for selective update.
* @param paramTypes
* @param paramFieldIdx
*/
private void setParameter(int[] paramTypes, int[] paramFieldIdx) {
//TODO Exception code
// if (paramsTypes.length != paramValues.length){
// throw new Exception("The number of parameter types and parameter value has to be the same");
// }
if (paramTypes != null) {
for (int i = 0; i < paramTypes.length; i++) {
declareParameter(new SqlParameter(paramTypes[i]));
}
}
this.paramFieldIdx = new int[paramFieldIdx.length];
System.arraycopy(paramFieldIdx, 0,
this.paramFieldIdx, 0,
paramFieldIdx.length);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?