百万级文本数据入oracle 数据库

百万级文本数据入oracle 数据库(亲测可用)

话不多说,直接上代码

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
 /**
  * 批量插入demo
  * @author 杨永琪
  * @date 2021年8月11日
  */
public class BatchDemo {
	
	private static String DRIVERCLASS="oracle.jdbc.driver.OracleDriver";
	private static String URL="jdbc:oracle:thin:@192.168.137.199:1521:helowin";
	private static String USERNAME="root";		
	private static String PASSWORD="root"; 		
	
	/**
	 * 获取数据库连接
	 */
	public static Connection getConnection() throws ClassNotFoundException, SQLException{
		 Class.forName(DRIVERCLASS); 
		 Connection conn =DriverManager.getConnection(URL, USERNAME, PASSWORD);
		 return conn;
	}
	
	public static void main(String[] arg) throws Exception {
 
		Long beginTime = System.currentTimeMillis();
		// 要导入的数据
		String fileDir = "E:\\yyq\\临时文件\\temp\\XXX.dat";
 
		InputStream is = new FileInputStream(fileDir);
		InputStreamReader inr = new InputStreamReader(is, "UTF-8");
		BufferedReader bf = new BufferedReader(inr);
		String dataLine = "";
		
		Connection conn = getConnection();
		//不要自动提交事务
		conn.setAutoCommit(false);
		String sql = "insert into yycw (preds,msisdn,hprovince,hprovince_name,hcity,hcity_name,user_dinner,in_net_month,brb_no,kd_province,kd_province_name,day)"
				+ "values (?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement pst = conn.prepareStatement(sql);
		int total = 0;//数据量
		int i = 0;//批量插入
		while ((dataLine = bf.readLine()) != null) {
			i++;
			total++;
			String[] str = dataLine.split("\t");//分隔符
			pst.setString(1, str[0]);
			pst.setString(2, str[1]);
			pst.setString(3, str[2]);
			pst.setString(4, str[3]);
			pst.setString(5, str[4]);
			pst.setString(6, str[5]);
			pst.setString(7, str[6]);
			pst.setString(8, str[7]);
			pst.setString(9, str[8]);
			pst.setString(10, str[9]);
			pst.setString(11, str[10]);
			pst.setString(12, str[11]);
			pst.addBatch();
			// 可以设置不同的大小;如50,100,500,1000等等
			if (i > 0 && i % 2000 == 0) {
				pst.executeBatch();
				conn.commit();
				pst.clearBatch();
				i=0;
			}
		}
		// 剩余的部分
		pst.executeBatch();
		conn.commit();
		pst.clearBatch();
		pst.close();
		conn.close();
		bf.close();
		System.out.println("总数据条数为:"+total);
		Long endTime = System.currentTimeMillis();
		System.out.println("读取文件耗时:"+(endTime - beginTime) / 1000 + "秒");
	}
}

执行效果
在这里插入图片描述

所用jar包
ojdbc14.jar

  • 28
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值