Requirement: Have a large TXT file (1,000,000 data), read the useful database from TXT and save to Oracle database
Using Java implementations:
1. Read the file
2. Insert the database into the database after connection
PackageCom.test.IF.service;ImportJava.io.File;Importjava.io.IOException;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;Importjava.sql.SQLException;Importorg.apache.commons.io.FileUtils;ImportOrg.apache.commons.io.LineIterator; Public classReadcustomerfile {intidx; Connection Conn=NULL; PreparedStatement pstmt=NULL; //read using the Fileutils class of the Commons-io.jar package Public voidreadtxtfilebyfileutils (String fileName) {File file=NewFile (fileName); DbConnection (); Try{lineiterator Lineiterator= Fileutils.lineiterator (file, "GB2312")); while(Lineiterator.hasnext ()) {String line=Lineiterator.nextline (); //row Data Conversion arraystring[] Custarray = Line.split ("\\|"); Insertcustinfo (Custarray); Thread.Sleep (100); } } Catch(IOException e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); } finally{dbdisconnection (); } } //inserting into the database Public voidinsertcustinfo (string[] strarray) {Try{stringbuffer sqlbf=NewStringBuffer (); Sqlbf.setlength (0); Sqlbf.append ("INSERT into Temp_cust_info (Cust_no, cust_nm, mob_no1) \ n"); Sqlbf.append (The VALUES (? \ n "); Sqlbf.append (" , ? \ n "); Sqlbf.append (" , ?) \ n "); Pstmt=conn.preparestatement (sqlbf.tostring ()); IDX= 1; Pstmt.clearparameters (); Pstmt.setint (idx+ +, Integer.parseint (strarray[0])); Pstmt.setstring (idx+ +, strarray[1]); Pstmt.setstring (idx+ +, strarray[2]); Pstmt.executeupdate (); } Catch(SQLException e) {e.printstacktrace (); } finally { if(Pstmt! =NULL) { Try{pstmt.close (); } Catch(SQLException e) {e.printstacktrace (); } } } } //connecting to a database PublicConnection dbConnection () {Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); String URL= "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL"; String User= "Scott"; String Password= "Goodluck"; Conn=drivermanager.getconnection (URL, user, password); System.out.println ("Connection Open!" "); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } returnConn; } //Close the database Public voiddbdisconnection () {if(Conn! =NULL) { Try{conn.close (); System.out.println ("Connection off! "); } Catch(SQLException e) {e.printstacktrace (); } } } Public Static voidMain (string[] args) {readcustomerfile RCF=NewReadcustomerfile (); Rcf.readtxtfilebyfileutils ("D:\\test\\customer_info.txt"); }}
Confirm Database Data:
Java reads the TXT file and saves the data to the database