Use JDBC to process big data and big text
Use JDBC to process Big Data
Basic concept: big data is also called lob (large objects). lob is divided into clob and blob.
COLB is used to store large text
Blob is used to store binary data, such as images, sounds, and binary files.
In MySQL, There is only blob, no clob, and MySQL uses text to store large text
Text can be divided into tinytext, text, mediumtext, and longtext.
Blob can be divided into tinyblob, blob, mediumblob, and longblob.
Use JDBC to process large text
For the text type in MySQL, you can set it as follows:
Preparedstatement. setcharacterstream (index, reader, length );
// Note that length must be set and set to int type.
// Storage of big text data
Public voidInsert (){
Connection con =Null;
Preparedstatement ST =Null;
Resultset rs =Null;
// Obtain the connection
Try{
Con = dbmanager.Getconnection();
String SQL = "insert into testclob (resume) value (?) ";
St = con. preparestatement (SQL );
// Read data from the file and use the stream
// Obtain the specified stream object through reflection
// Obtain the resource file object ----> obtain path
File F =NewFile ("1.txt ");
// Automatically read 1.TxtFile Content, stored in the database table
St. setcharacterstream (1,NewFilereader (F), F. Length (); // set the input stream object for the first object
IntResult=st.exe cuteupdate ();
If(Result> 0 ){
System.Out. Println ("inserted successfully ");
}Else{
System.Out. Println ("insertion failed ");
}
}Catch(Sqlexception e ){
//TodoAuto-generated Catch Block
E. printstacktrace ();
} // Release the resource
Catch(Filenotfoundexception e ){
//TodoAuto-generated Catch Block
E. printstacktrace ();
}
Finally{
Dbmanager.Release(Con, St, RS );
}
}
Public voidFind (){
// Read records
Connection con =Null;
Preparedstatement ST =Null;
Resultset rs =Null;
// Obtain the connection
Try{
Con = dbmanager.Getconnection();
String SQL = "select resume from testclob where id = 1 ";
St = con. preparestatement (SQL );
Rs1_st.exe cutequery ();
If(Rs. Next ()){
// If any result is returned, read the result set.
/* StringStr= Rs. getstring ("resume ");
System. Out. println (Str);*/
Reader reader = Rs. getcharacterstream ("resume"); // read the stream object by column name.
// Read 1024 bytes each time
CharBuff [] =New char[1024];
IntLen = 0;
// Directly output the read content displayed in the console window
Reader. Read (buff); // The returned value is the actual number of bytes read.
While(LEN = reader. Read (buff)> 0 ){
System. Out. println (new string (buff, 0,Len));
}
// Create a new file and write the read data to the new file
Filewriter writer =NewFilewriter ("3.txt ");
While(LEN = reader. Read (buff)> 0 ){
Writer. Write (buff, 0, Len );
}
Writer. Close ();
Reader. Close ();
}
}Catch(Sqlexception e ){
//TodoAuto-generated Catch Block
E. printstacktrace ();
}Catch(Ioexception e ){
//TodoAuto-generated Catch Block
E. printstacktrace ();
}
// Resource release
Finally{
Dbmanager.Release(Con, St, RS );
}
Additional: Connection of dbmanager. Java to the database
Public classDbmanager {
Private StaticStringUsername;
Private StaticStringPassword;
Private StaticStringURL;
Private StaticStringDriver;
Static{
Try{
Inputstream in = dbmanager.Class. Getclassloader (). getresourceasstream ("config/DBCP. properties ");
Properties prop =NewProperties ();
Prop. Load (in );
Driver= Prop. getproperty ("driverclassname ");
URL= Prop. getproperty ("url ");
Username= Prop. getproperty ("username ");
Password= Prop. getproperty ("password ");
Class.Forname(Driver);
}Catch(Exception e ){
Throw newExceptionininitializererror (E );
}
}
Public staticConnection getconnection ()
ThrowsSqlexception {
ReturnDrivermanager.Getconnection(URL,
Username,Password);
}
Public static voidRelease (connection Conn, statement St, resultset RS ){
If(RS! =Null){
Try{
Rs. Close ();
}Catch(Exception e) {e. printstacktrace ();}
Rs =Null;
}
If(St! =Null){
Try{
St. Close ();
}Catch(Exception e) {e. printstacktrace ();}
St =Null;
}
If(Conn! =Null){
Try{
Conn. Close ();
}Catch(Exception e) {e. printstacktrace ();}
Conn =Null;
}
}
}