加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle Java批量导数据

发布时间:2020-12-12 14:34:02 所属栏目:百科 来源:网络整理
导读: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



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:UsersliyangDesktoplaocao3.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();
   }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读