MySQL Access large text and batch data

Source: Internet
Author: User

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

Related Article

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.