Java reads the TXT file and saves the data to the database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.