JDBC Handles Big Data

Source: Internet
Author: User

1. Handle large text
1  PackageCom.demo;2 3 ImportJava.io.File;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileReader;6 ImportJava.io.FileWriter;7 Importjava.io.IOException;8 ImportJava.io.Reader;9 Importjava.sql.Connection;Ten Importjava.sql.PreparedStatement; One ImportJava.sql.ResultSet; A Importjava.sql.SQLException; -  - Importorg.junit.Test; the  - Importcom.utils.DButils; -  - //JDBC to save large text data +  -  Public classDemo1 { + @Test A      Public voidInsert ()throwsSQLException, filenotfoundexception{ atConnection con =NULL; -PreparedStatement st =NULL; -ResultSet result =NULL; -         Try { -Con =dbutils.getconnection (); -String sql = "INSERT into Testclob (Id,resume) VALUES (?,?)"; inSt =con.preparestatement (SQL); -St.setstring (1, "1"); to              +File File =NewFile ("Src/1.txt"); -FileReader reader =Newfilereader (file); the              *             //set columns for large text $St.setcharacterstream (2, Reader, File.length ());Panax Notoginseng             intnum =st.executeupdate (); -             if(num>0){ theSystem.out.println ("Insert succeeded"); +             } A}finally{ the dbutils.release (Con, St, result); +         } -     } $      $     //reading large text data - @Test -      Public voidRead ()throwsSQLException, ioexception{ theConnection con =NULL; -PreparedStatement st =NULL;WuyiResultSet result =NULL; the          -         Try { WuCon =dbutils.getconnection (); -String sql = "Select Id,resume from Testclob where id= ' 1 '"; AboutSt =con.preparestatement (SQL); $result =st.executequery (); -             if(Result.next ()) { -                 //String resume = Result.getstring ("Resume"), cannot be saved with string, takes up too much memory -Reader reader = Result.getcharacterstream ("Resume"); AFileWriter writer =NewFileWriter ("C:\\1.text"); +                 Try{ the                     intLen = 0; -                     CharBuffer[] =New Char[1024]; $                      while(Len=reader.read (buffer)) >0){ theWriter.write (buffer, 0, Len); the                     } the}finally{ the                     if(reader!=NULL){ - reader.close (); in                     } the writer.close (); the                 } About             } the}finally{ the dbutils.release (Con, St, result); the         } +     } -      the}

2. Processing binary files
1  PackageCom.demo;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.FileNotFoundException;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 ImportJava.io.InputStream;9 ImportJava.io.OutputStream;Ten Importjava.sql.Connection; One Importjava.sql.PreparedStatement; A ImportJava.sql.ResultSet; - Importjava.sql.SQLException; -  the Importorg.junit.Test; -  - Importcom.utils.DButils; -  + //JDBC Access binary files -  Public classDemo2 { + @Test A      Public voidInsert ()throwsSQLException, filenotfoundexception{ atConnection con =NULL; -PreparedStatement st =NULL; -ResultSet result =NULL; -         Try{ -Con =dbutils.getconnection (); -String sql = "INSERT into Testblob (id,image) VALUES (?,?)"; inSt =con.preparestatement (SQL); -St.setstring (1, "1"); toFile File =NewFile ("Src/1.jpg"); +FileInputStream in =Newfileinputstream (file); -St.setbinarystream (2, in, File.length ()); the st.executeupdate (); *}finally{ $ dbutils.release (Con, St, result);Panax Notoginseng         } -     } the      + @Test A      Public voidRead ()throwsSQLException, ioexception{ theConnection con =NULL; +PreparedStatement st =NULL; -ResultSet result =NULL; $         Try{ $Con =dbutils.getconnection (); -String sql = "Select Id,image from Testblob where id= ' 1 '"; -St =con.preparestatement (SQL); theresult =st.executequery (); -             if (Result.next ()) {InputStream in = Result.getbinarystream ("image"); OutputStream                      out = new FileOutputStream ("C:\\1.jpg"), try{54 int len = 0;55 byte[] buffer = new byte[1024];56 while ((Len=in.read (buffer)) >0) {in.                         Write (buffer, 0, Len),}59}finally{60 if (in!=null) {61 In.close ();}63 if (out!=null) {Out.clo Se ();}66}67} the}finally{ the dbutils.release (Con, St, result); -         } in     } the}

JDBC Handles Big 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.