contactinsert.java
来自「Java的框架」· Java 代码 · 共 118 行
JAVA
118 行
package mcaps.apps.prrm.roaddefect.dao.jdbc.query;
import mcaps.apps.prrm.roaddefect.dao.util.ContactField;
import mcaps.apps.prrm.roaddefect.model.Contact;
import mcaps.apps.prrm.util.TableNameConstants;
import javax.sql.DataSource;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
/**
* This class extends SqlUpdate to perform JDBC Insert
* operation to the contact table. This class is
* used to set the precompiled INSERT SQL statement and
* to execute the INSERT SQL statement.
*
* @author jov
* @date Sep 23, 2005
* @version 1.0.1.0
*/
public class ContactInsert extends SqlUpdate {
private DataFieldMaxValueIncrementer incrementer;
/**
* Default constructor that accepts a datasource object to allow
* the connection to the database for insert operation.
* @param ds the datasource object
*/
public ContactInsert(DataSource ds){
setDataSource(ds);
setDefaultParameterTypes();
setSql(getDefaultSQL());
compile();
}
/**
* Sets the incrementer.
* @param incrementer The incrementer to set.
*/
public void setIncrementer (DataFieldMaxValueIncrementer incrementer) {
this.incrementer = incrementer;
}
/**
* Execute the JDBC insert operation using the values as
* in the contact object
* @param contact the object the holds the field values to be inserted
* @return returns the id in which created for the newly
* inserted record
*/
public Integer insert(Contact contact){
// TODO Exception code If not initialized throw excp
Integer id = new Integer(incrementer.nextIntValue());
Object[] paramValues = new Object[]{
id,
contact.getFirstName(),
contact.getLastName(),
contact.getBlock(),
new Integer(contact.getFloor()),
new Integer(contact.getUnit()),
contact.getRoadName(),
contact.getPostcode(),
contact.getCountry(),
contact.getTelephone(),
contact.getEmail(),
contact.getLastModifiedTime()
};
update(paramValues);
return id;
}
/**
* Generates the defact SQL statement.
* @return the default sql statement.
*/
private String getDefaultSQL() {
StringBuffer sb = new StringBuffer();
sb.append("INSERT INTO ").append(TableNameConstants.CONTACT_TABLENAME)
.append(" (");
int fieldNum = ContactField.MAX;
for (int i = 0; i <= fieldNum; i++){
sb.append(ContactField.FIELDS[i]);
if (i < fieldNum) {
sb.append(", ");
}
}
sb.append(") ").append("VALUES ").append("(");
for (int i = 0; i <= fieldNum; i++) {
sb.append("?");
if (i < fieldNum) {
sb.append(", ");
}
}
sb.append(")");
return sb.toString();
}
/**
* Sets the default parameter types.
*/
protected void setDefaultParameterTypes(){
for (int i=0; i <= ContactField.MAX; i++){
declareParameter(new SqlParameter(ContactField.FIELDTYPES[i]));
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?