⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maintestforbatchpreparedstatementsetter.java

📁 随书光盘:精通Sping 2.0 的随书源代码
💻 JAVA
字号:
package test;

import java.sql.PreparedStatement;
import java.sql.SQLException;

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.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.InterruptibleBatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter;

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

	private static final Log log = LogFactory.getLog(MainTestForBatchPreparedStatementSetter.class);
	
	public static void main(String[] args) {		
		ListableBeanFactory cbf = new ClassPathXmlApplicationContext("ac2.xml");		
		GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(cbf);
		
		JdbcTemplate jt = gbfa.getBean("jdbcTemplate");
		
		final int[] no = new int[]{7369,7499,7521,7566,7654,7698};
		
		jt.batchUpdate("update emp set sal = ? where empno = ?", 
					new BatchPreparedStatementSetter(){
							public void setValues(PreparedStatement ps, int i) throws SQLException {
								ps.setInt(1, no[i]);
								ps.setFloat(2, no[i]);
							}
							public int getBatchSize() {
								return no.length;
							}
					});
		
		jt.batchUpdate("update emp set sal = ? where empno = ?", 
				new InterruptibleBatchPreparedStatementSetter(){
							public void setValues(PreparedStatement ps, int i) throws SQLException {
								ps.setInt(1, no[i]);
								ps.setFloat(2, no[i]);
							}
							public int getBatchSize() {
								return no.length;
							}
							public boolean isBatchExhausted(int i) {
								if(i == 3) return true;
								return false;
							}
				});

		jt.batchUpdate("update emp set sal = ? where empno = ?", 
				new AbstractInterruptibleBatchPreparedStatementSetter(){
						@Override
						protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
							if(i < no.length && i != 3){
								ps.setInt(1, no[i]);
								ps.setFloat(2, no[i]);
								return true;
							} else{
								return false;
							}
						}
				});		
	}

}

⌨️ 快捷键说明

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