Oracle Java批量导数据

前端之家收集整理的这篇文章主要介绍了Oracle Java批量导数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



package com.ceair.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.sqlException;
import java.util.Scanner;

public class Main2 {
	
	public static void main(String[] args) throws sqlException,ClassNotFoundException,FileNotFoundException {
		   String url = "jdbc:oracle:thin:@192.168.1.190:1521:ORCL"; 
		   String user = "sys as sysdba ";
		   String password = "oracle";
		   StringBuffer sql = new StringBuffer();
		   sql.append("insert into laocao values(to_date(?,'yyyy-mm-dd'),to_date(?,?,?)");
		   Class.forName("oracle.jdbc.driver.OracleDriver");
		   Connection con = (Connection) DriverManager.getConnection(url,user,password);
		   // 关闭事务自动提交
		   con.setAutoCommit(false);
		   Scanner in = new Scanner(new File("C:\\Users\\liyang\\Desktop\\laocao3.txt")) ;
		   int sum = 0 ;
		   while(in.hasNext()){
			    int T = 1000 ;
			    Long startTime = System.currentTimeMillis();
			    PreparedStatement pst = (PreparedStatement) con.prepareStatement(sql.toString());
			    while((T-- > 0) && in.hasNext()){
					String str = in.nextLine() ;
					String[] s = str.split("\t");
					pst.setString(1,s[0]);
					pst.setString(2,s[1]);
					pst.setString(3,s[2]);
					pst.setString(4,s[3]);
					pst.setString(5,s[4]);
					pst.setString(6,s[5]);
					pst.setString(7,s[6]);
					pst.setString(8,s[7]);
					pst.addBatch(); // 把一个sql命令加入命令列表
				    sum++ ;
			    }
			    pst.executeBatch();
			    con.commit(); // 语句执行完毕,提交本事务
			    pst.close();
			    Long endTime = System.currentTimeMillis();
				System.out.println(sum + "  用时:" + (endTime - startTime) + "ms");
			}
		    con.close();
   }

}
原文链接:https://www.f2er.com/oracle/208031.html

猜你在找的Oracle相关文章