roaddefectupdate.java
来自「Java的框架」· Java 代码 · 共 197 行
JAVA
197 行
package mcaps.apps.prrm.roaddefect.dao.jdbc.query;
import javax.sql.DataSource;
import mcaps.apps.prrm.roaddefect.dao.util.RoadDefectField;
import mcaps.apps.prrm.roaddefect.model.RoadDefect;
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 defect table. This class is
* used to set the precompiled UPDATE SQL statement and
* to execute the UPDATE SQL statement.
*
* @author jov
* @date Sep 23, 2005
* @version 1.0.1.0
*/
public class RoadDefectUpdate 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 RoadDefectUpdate(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 defect field as set in
* the RoadDefectField object
*/
public RoadDefectUpdate(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 defect object
* @param roadDefect the object the holds the field values to be inserted
* @return the number of rows affected by the update
*/
public int update(RoadDefect roadDefect) {
// TODO Exception code If not initialized throw excp
Object[] paramValues = new Object[] {
roadDefect.getDefectType(),
roadDefect.getDefectDetail(),
roadDefect.getRoad().getId(),
roadDefect.getLocation(),
roadDefect.getSeverity(),
roadDefect.getStatus(),
// roadDefect.getCoordinates(),
roadDefect.getContact().getId(),
roadDefect.getLastModifiedTime(),
roadDefect.getId() };
return update(paramValues);
}
/**
* Generates the road defect 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.ROADDEFECT_TABLENAME)
.append(" SET ")
.append(RoadDefectField.FIELDS[RoadDefectField.DEFECTTYPE])
.append("=?,")
.append(RoadDefectField.FIELDS[RoadDefectField.DEFECTDETAIL])
.append("=?,")
.append(RoadDefectField.FIELDS[RoadDefectField.ROADID])
.append("=?,")
.append(RoadDefectField.FIELDS[RoadDefectField.LOCATION])
.append("=?,")
.append(RoadDefectField.FIELDS[RoadDefectField.SEVERITY])
.append("=?,")
.append(RoadDefectField.FIELDS[RoadDefectField.STATUS])
.append("=?,")
.append(RoadDefectField.FIELDS[RoadDefectField.CONTACTID])
.append("=?,")
.append(RoadDefectField.FIELDS[RoadDefectField.LASTMODIFIEDTIME])
.append("=?")
.append(" ")
.append("WHERE")
.append(" ")
.append(RoadDefectField.FIELDS[RoadDefectField.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(
RoadDefectField.FIELDTYPES[RoadDefectField.DEFECTTYPE]));
declareParameter(new SqlParameter(
RoadDefectField.FIELDTYPES[RoadDefectField.DEFECTDETAIL]));
declareParameter(new SqlParameter(
RoadDefectField.FIELDTYPES[RoadDefectField.ROADID]));
declareParameter(new SqlParameter(
RoadDefectField.FIELDTYPES[RoadDefectField.LOCATION]));
declareParameter(new SqlParameter(
RoadDefectField.FIELDTYPES[RoadDefectField.SEVERITY]));
declareParameter(new SqlParameter(
RoadDefectField.FIELDTYPES[RoadDefectField.STATUS]));
declareParameter(new SqlParameter(
RoadDefectField.FIELDTYPES[RoadDefectField.CONTACTID]));
declareParameter(new SqlParameter(
RoadDefectField.FIELDTYPES[RoadDefectField.LASTMODIFIEDTIME]));
declareParameter(new SqlParameter(
RoadDefectField.FIELDTYPES[RoadDefectField.ID]));
}
/**
* Generates the road defect 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.ROADDEFECT_TABLENAME).append(" ")
.append("SET").append(" ");
for (int i = 0; i < this.paramFieldIdx.length; i++) {
sb.append(RoadDefectField.FIELDS[this.paramFieldIdx[i]])
.append("=?");
if (i < this.paramFieldIdx.length-1){
sb.append(",");
}
}
return sb.toString();
}
/**
* Generates the road defect 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 + -
显示快捷键?