contactupdate.java

来自「Java的框架」· Java 代码 · 共 104 行

JAVA
104
字号
package mcaps.apps.prrm.roaddefect.dao.jdbc.query;

import javax.sql.DataSource;

import mcaps.apps.prrm.roaddefect.dao.util.ContactField;
import mcaps.apps.prrm.roaddefect.model.Contact;
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 contact 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 ContactUpdate extends SqlUpdate {

	
	/**
	 * Default constructor that accepts a datasource object to allow
	 * the connection to the database for update operation.
	 * @param ds the datasource object
	 */
	public ContactUpdate(DataSource ds){		
		setDataSource(ds);
		setDefaultParameterTypes();
		setSql(getDefaultSQL());
		compile();
	}
	
	/**
	 * Execute the JDBC update operation using the values as
	 * in the contact object
	 * @param contact the object the holds the field values to be inserted
	 * @return the number of rows affected by the update  
	 */
	public int update(Contact contact){
//	TODO Exception code If not initialized throw excp
		Object[] paramValues = new Object[]{
				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(),
				contact.getId()
		};
		return update(paramValues);
	}
	
	/**
	 * Generates the contact default SQL statement.
	 * @return the default sql statement.
	 */
	private String getDefaultSQL(){
		StringBuffer sb = new StringBuffer();
		sb.append("UPDATE")
			.append(" ")
			.append(TableNameConstants.CONTACT_TABLENAME)
			.append(" ")
			.append("SET")
			.append(" ");
			
		for (int i = 1; i<ContactField.FIELDS.length; i++){
			sb.append(ContactField.FIELDS[i]).append("=?");
			if (i < ContactField.FIELDS.length-1){
				sb.append(",");
			}
		}
			
		sb.append(" ")
			.append("WHERE")
			.append(" ")
			.append(ContactField.FIELDS[ContactField.ID])
			.append("=?");
		return sb.toString();
	}
	
	/**
	 * Sets the default parameter types.
	 */
	protected void setDefaultParameterTypes() {
		//Note: Update does not update ID, thus starts with index 1.
		//However id is used as last parameter.
		for (int i = 1; i<ContactField.FIELDTYPES.length; i++){
			declareParameter(new SqlParameter(ContactField.FIELDTYPES[i]));
		}
		//Where id = ?
		declareParameter(new SqlParameter(ContactField.FIELDTYPES[ContactField.ID]));
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?