public class Readwritebigdata {/*create database bigdata;use bigdata;create table Bigdata //Create tables (ID varchar (20) Primary Key,pinglun text, (Big data) image blob (binary) ); */public void Insert () {try{ Connection con= Dbhelper.getconnection (); String sql= "INSERT into Bigdata (Id,pinglun) value (?,?)"; PreparedStatement ps=con.preparestatement (SQL); Ps.setstring (1, "A001"); //Access large text, read into the stream using the PreparedStatement object method File F=new file ("Src//mysql/1.txt"); FileReader reader=new FileReader (f); Ps.setcharacterstream (2, Reader,f.length ()); Ps.setbinarystream (Parameterindex, x, length);//insert image, etc. binary int i=ps.executeupdate (); if (i>0) { System.out.println ("Insert Success");} } catch (Exception e) {System.out.println ("fail .....");}} @Testpublic void Test () throws FileNotFoundException, Sqlexception{readwritebigdata r=new readwritebigdata (); R.insert ();}}
Package Mysql;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.sqlexception;import Org.junit.test;//preparedstatement.addbatch () processing batches can only execute one SQL statement (INSERT, UPDATE: ), Multiple data table replication//preparedstetement with more than//statement.addbatch () can handle multiple SQL, can be inserted at the same time, update delete. Batch data, if processing inserts 1 million data, cannot insert 1 million data at a time//can be used for loop, not insert 1000 addbatch (), then Clearbatch () in insert public class Batch {@Testpublic void Test1 () throws SQLException {Connection con=null; PreparedStatement Ps=null;con = Dbhelper.getconnection (); String sql = "INSERT into login (Username,password) VALUES (?,?)"; PS = con.preparestatement (SQL); for (int i=0;i<10;i++) {ps.setstring (1, i+ ""); Ps.setstring (2, i+ ""); Ps.addbatch ();//batch, internally loaded in list collection} Ps.executebatch ();//Execute batch code once} @Testpublic void Test2 () throws sqlexception{//batch processing 1 million data, each execution 1000, in Addbatch (), In Clearbatch (), otherwise memory leaks out long a=system.currenttimemillis (); Connection Con=null; PreparedStatement Ps=null;con = Dbhelper.getconnection (); String sql = "INSERT into login (Username,password) VALUES (?,?)";PS = con.preparestatement (SQL); for (int i=201;i<2000;i++) {ps.setstring (1, i+ ""); Ps.setstring (2, i+ ""); Ps.addbatch (); if (i%1000==0)//Add 1000 data at a time {Ps.executebatch ();//batch, first execute 1000 times ps.clearbatch ();//First join data Empty}} Ps.executebatch ();//Execute Batch code SYSTEM.OUT.PRINTLN ("The time Execute is" + (System.currenttimemillis ()-a));}}
MySQL Access large text and batch data