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 ();
}