java(功能篇) java(mysql)数据库 实现数据批量插入

熊孩纸 阅读:691 2021-03-31 22:56:13 评论:0
package com.emc.database; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.List; 
 
import com.emc.model.DyLun; 
import com.emc.service.DyLunService; 
 
public class Demo { 
	public static final String DBDRIVER = "com.mysql.jdbc.Driver"; 
	    public static final String DBURL = "jdbc:mysql://192.168.0.32:3306/iiiis"; 
	    public static final String DBUSER = "root"; 
	    public static final String DBPASS = "Vixuan724"; 
	    //java 除法取整函数 
	    public static int getMyInt(int a,int b) { 
	    	return(((double)a/(double)b)>(a/b)?a/b+1:a/b); 
	    	} 
	    //批处理处理信息量 
	    private static final int processing=50; 
	/** 
	 * @param args 
	 */ 
	public static void main(String[] args) throws Exception { 
		 
		// TODO Auto-generated method stub 
		Connection conn=null; 
		        PreparedStatement ps=null; 
		        String sql="insert into TB_DY_LUN (device_Id,VOLUME_ID_IN_ARRAY,VOLUME_NAME,VOLUME_TYPE,UPDATE_FLAG,NUMBER_OF_BLOCK,BLOCK_SIZE,RAID_LEVEL,HEALTH_STATE,VOLUME_ID_IN_HOST,update_time) values(?,?,?,?,?,?,?,?,?,?,?)"; 
		        Class.forName(DBDRIVER); 
		        conn=DriverManager.getConnection(DBURL, DBUSER, DBPASS); 
		         ps=conn.prepareStatement(sql); 
		          
		         DyLunService service = new DyLunService(); 
		     	List<DyLun> list = service 
		     			.getdylun("C:\\data\\emc\\Symm_StorageVolume.xml"); 
		     	//批处理循环次数 
		         int k=getMyInt(list.size(),processing); 
		         for(int j=0;j<k;j++){ 
		        	 //批处理数量 
		        		for(int i = 0; i < 50; i++){ 
		        			int result=j*50+i; 
		        			System.out.println("结果数:"+result); 
		        			if(result<list.size()){ 
		        				DyLun lun = list.get(result); 
					    		String s="select * from TB_DY_LUN where VOLUME_ID_IN_ARRAY='"+lun.getVolumeIdInArray()+"'"; 
					    		ResultSet set=DataBase.select(s); 
					    		if (set.next()){ 
					    			//存在同名主键不做处理 
					    		}else{ 
					    			ps.setString(1 ,lun.getDeviceId() ); 
					    			ps.setString(2, lun.getVolumeIdInArray()); 
					    			ps.setString(3,lun.getVolumeName() ); 
					    			ps.setString(4, lun.getVolumeType()); 
					    			ps.setString(5, lun.getUpdateFlag()); 
					    			ps.setInt(6, lun.getNumberOfBlock()); 
					    			ps.setInt(7, lun.getBlockSize()); 
					    			ps.setString(8, lun.getRaidLevel()); 
					    			ps.setString(9, lun.getHealthState()); 
					    			ps.setString(10, lun.getVolumeIdInHost()); 
					    			Date date = new Date(); 
					    			SimpleDateFormat dateFormat = new SimpleDateFormat( 
					    					"yyyy/MM/dd HH:mm:ss"); 
					    			ps.setString(11, dateFormat.format(date.getTime()).toString()); 
					    			ps.addBatch(); 
					    		} 
		        				 
		        			} 
				    	 
				    		 
				    	} 
		        		 int trmp[]=ps.executeBatch(); 
				         System.out.println("更新了"+trmp.length+"条数据。。"); 
		        	  
		         } 
		      
		         
		        ps.close(); 
		        conn.close(); 
	} 
 
} 

标签:mysql
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号