maintestforupdatablesqlquery.java

来自「随书光盘:精通Sping 2.0 的随书源代码」· Java 代码 · 共 61 行

JAVA
61
字号
package org.springframework.samples;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.UpdatableSqlQuery;

/**
 * 
 * @author worldheart
 *
 */
public class MainTestForUpdatableSqlQuery {

	private static final Log log = LogFactory.getLog(MainTestForUpdatableSqlQuery.class);
	
	public static void main(String[] args) {		
		ListableBeanFactory cbf = new ClassPathXmlApplicationContext("ac5.xml");		
		GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(cbf);
		
		DataSource ds = gbfa.getBean("dataSource");
		
		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(4, "Xxx");
		
		OwnersUpdateQuery ouq = new OwnersUpdateQuery(ds);
		List list = ouq.execute("Davis", map);
		
		log.info(list);
	}

}

class OwnersUpdateQuery extends UpdatableSqlQuery {

	public OwnersUpdateQuery(DataSource ds) {
		super(ds, "select city from owners where last_name = ?");
		declareParameter(new SqlParameter(Types.VARCHAR));
		compile();
	}

	protected Object updateRow(ResultSet rs, int rownum, Map context) 
		throws SQLException {
		rs.updateString(1, "" + rs.getString("city") + " " + context.get(4));
		return null;
	}
	
}

⌨️ 快捷键说明

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