大数据量csv文件导入数据库

  今天客户拿过来一个500多M的csv文件(汗,中国移动导的欠费记录,导出的人也是强人),用Excel直接打开只能看到100多万条记录,超过部分就看不到了。 让我们想想办法,看能不能拆分成多个excel表格。想了很多办法,用PL/sql导入数据库后再分,直接死机了。最后想到了写代码导入。代码如下:

String filePath = "D:/aa2.csv";
		File file = new File(filePath);
		if (file.exists()) {
			int i = 0;
			String strLine = null;
			Statement ps = null;
			Connection conn = null;
			String sql = null;
			try{
				conn = HibernateSessionUtil.getConnection();
				conn.setAutoCommit(false);
				ps = conn.createStatement();
				BufferedReader bufferedreader = new BufferedReader(new FileReader(filePath));
				while ((strLine = bufferedreader.readLine()) != null) {
					i++;
					String [] values = strLine.split(",");//逗号隔开的各个列
					String cell0 = values[0];
					String cell1 = values[1];
					String cell2 = values[2];
					String cell3 = values[3];
					String cell4 = values[4];
					String cell5 = values[5];
					String cell6 = values[6];
					String cell7 = values[7];	
					sql = String.format("INSERT INTO TBLDATA2(CELL0,CELL1,CELL2,CELL3,CELL4,CELL5,CELL6,CELL7,TID) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s',SEQ_TBLID.NEXTVAL)",
							cell0,cell1,cell2,cell3,cell4,cell5,cell6,cell7);
					ps.executeUpdate(sql);
					if(i%500 == 0){
						//500条记录提交一次
						conn.commit();
						System.out.println("已成功提交"+i+"行!");
					}
				}
				if(i%500 != 0){
					//不够500条的再提交一次(其实不用判断,直接提交就可以,不会重复提交的)
					conn.commit();
					System.out.println("已成功提交"+i+"行!");
				}
				
			}catch(Exception ex){
				System.out.println("导出第"+(i+1)+"条时出错,数据是" + strLine);
				System.out.println("出错的sql语句是:" + sql);
				System.out.println("错误信息:");
				ex.printStackTrace();
				try {
					if (conn != null) {
						conn.rollback();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} finally{
				try {
					if (ps != null) {
						ps.close();
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				try {
					if (conn != null){
						conn.close();
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}
		    }
		}


 

最后导了两个多小时,700多万条数据。
导入到数据库就好办了,50万或者100万导成一个excel表都可以。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值