Java and MySQL database large text and binary operations

Source: Internet
Author: User
1. Text (clob)

In MySQL, the maximum varchar value is 65535 bytes. If the value exceeds this range, set it to text.

Longtext can be up to 4 GB

Store text // define SQL statement string SQL = "insert into clob (File) value (?) ";

Preparedstatement PS = conn. preparestatement (SQL );

File file =NewFile ("Res/clobdemo. Java ");

Reader reader =NewFilereader (File );

PS. setcharacterstream (1, reader ,(Int) File. Length ());

Ps.exe cuteupdate ();

// Close the stream.

Reader. Close ();

Read text

String SQL = "SELECT FILE from clob ";

Preparedstatement PS = conn. preparestatement (SQL );

Resultset Rs cmdps.exe cutequery ();

If(Rs. Next ()){

Reader reader = Rs. getcharacterstream (INT columnindex );

// This reader is the stream that reads data from the database and operates this stream to read data.

Bufferedreader BR =NewBufferedreader (R );

Bufferedwriter BW =NewBufferedwriter (NewFilewriter (

"Res/new.txt "));

String S = "";

 While(S = Br. Readline ())! =Null){

Bw. Write (S + "\ n ");

}

Bw. Flush ();

BR. Close ();

R. Close ();

Bw. Close ();

}

2. blob

Blob is used to store large binary data segments, such as the sample, audio, and video. longblob. the maximum size is 4 GB.

Storage blob

Preparedstatement PS = conn. preparestatement ("insert into big_binary (File) values (?) ");

File file =NewFile ("src/CN/itcast/jdbc1/clob_blob/img_0007.jpg ");

Inputstream in =NewFileinputstream (File );

PS. setbinarystream (1, In ,(Int) File. Length ());

Ps.exe cuteupdate ();

Read blob

Preparedstatement PS = conn. preparestatement ("select filefrom big_binary ");

Resultset Rs cmdps.exe cutequery ();

If(Rs. Next ()){


Inputstream in = Rs. getbinarystream (1 );

// This inputstream is the stream that reads data from the database. You can use this stream to read data.

File file = new file ("Res/newgirl.jpg ");
Fileoutputstream Fos = new fileoutputstream (File );
Int num = in. Available ();
Byte buffer [] = new byte [num];
While (in. Read (buffer, 0, num )! =-1 ){
FOS. Write (buffer );
}
System. Out. println ("Left ");
In. Close ();
FOS. Close ();

}

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.