数据的批处理

一.jdbc写法
package org.inspur.aaDemo;

import java.sql.*;

public class Test {

	public Connection t() {

		Connection con = null;
		String url = "jdbc:oracle:thin:@10.122.11.2:1521:zypw";
		String user = "zypwt";
		String password = "zypwt";
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			try {
				con = DriverManager.getConnection(url, user, password);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return con;
	 	}
	public static void main(String[] args) throws SQLException {
		Test tt = new Test();
		Connection con = tt.t();
		PreparedStatement pstmt = null;
		pstmt = con.prepareStatement("insert into chg_zwxx (zw_id,zb,zw_mc,p_mc,fq_id,zwt_id,chg_id,cjsj) values(?,?,?,?,?,?,?,sysdate)");
		for (int i = 1; i < 100; i++) {
			pstmt.setString(1, i + "");
			pstmt.setString(2, "号");
			pstmt.setString(3, "试");
			pstmt.setString(4, "排");
			pstmt.setString(5, "区");
			pstmt.setString(6, "图");
			pstmt.setString(7, "管");
			pstmt.addBatch();
		}
		pstmt.executeBatch();
		con.close();
	}
}


二.spring中

	public Integer update(final List list){
		
    	BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
    		
        	public int getBatchSize() {
            	return list.size();
            }
            public void setValues(PreparedStatement ps, int index)
            	throws SQLException {    
                PaiBjView view = (PaiBjView) list.get(index);
                	ps.setString(1,view.getP_mc());
                	ps.setString(2,view.getP_lx());
                	ps.setString(3,view.getP_id());
            }	
        };
        String update="UPDATE CHG_P SET P_MC=?,P_LX=? WHERE P_ID=? ";
        JdbcTemplate jt = new JdbcTemplate(getCommDao().getDataSource());
        	jt.batchUpdate(update, setter);
        	System.out.println("执行完更新操作");
        	return null;
	}

你可能感兴趣的:(spring,oracle,sql,jdbc)