PackageCom.tongxiang.item.base.dao;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Arrays;Importorg.apache.commons.lang.StringUtils; Public classimportfromsql{Private Static FinalString URL = "Jdbc:oracle:thin: @xxx. Xxx.xxx.xxx:1521:orcl"; Private Static FinalString user = "User"; Private Static FinalString PASSWORD = "PASSWORD"; Private StaticConnection CONN =NULL; Static { Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); CONN=drivermanager.getconnection (URL, USER, PASSWORD); } Catch(SQLException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } } Public Static voidMain (string[] args)throwsException {Try { //Insert File Data format//C:\\test.sql// -------------------------- //INSERT INTO (...) values (...); //INSERT INTO (...) values (...); //INSERT INTO (...) values (...); // --------------------------String sqlfile = "C:\\test.sql"; //Number of batches per batch intbatchcnt = 100; Insertsqlbacth (CONN, Sqlfile, batchcnt); } finally{conn.close (); } } /*** Incoming connection to execute SQL script file so that it is in the same thing as a database operation outside of it * *@paramDatabase Connection *@paramFile path *@paramNumber of batches per batch **/ Public Static voidINSERTSQLBACTH (Connection conn, String Sqlfile,intBATCHCNT)throwsException {Statement stmt=NULL; //get a file from a given locationFile File =NewFile (Sqlfile); BufferedReader Reader=NULL; Try{Reader=NewBufferedReader (Newfilereader (file)); //every time you read a file's cacheString temp =NULL; inti = 0; intMoD = 0; stmt=conn.createstatement (); while(temp = Reader.readline ())! =NULL) {i++; Stmt.addbatch (Stringutils.chomp (Temp.trim (),";")); MoD= i%batchcnt; if(mod = = 0) { int[] rows =Stmt.executebatch (); Conn.commit (); System.out.println ("Row Count:" +arrays.tostring (rows)); Stmt.close (); stmt=conn.createstatement (); } } if(MoD! = 0) { int[] rows =Stmt.executebatch (); Conn.commit (); System.out.println ("Row Count:" +arrays.tostring (rows)); Stmt.close (); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { //Close File Stream if(Reader! =NULL) { Try{reader.close (); } Catch(IOException e) {e.printstacktrace (); } } } }}
Simple bulk reading of external insert and inserting db