The transmission of JDBC's Big data content
What is big data content?
In the database, there is a record, many fields in the record is a few characters enough, if you want to put a novel into the database, this novel is certainly not a few characters, but composed of tens of thousands of words, the novel data we can say is big data, life of course there are all kinds of big data: film, music , pictures, etc...
Large Characters Data content operation
Large character content: usually refers to very long characters of the type of file, such as novels, stories and so on, the content has a character composition.
Here's a look at the big data types in MySQL and Oracle
Type of data |
Data size |
Mysql |
Oracle |
Character |
Small |
Char,varchar |
Varchar2 |
|
Big |
Text/longtext |
Clob |
Bytes |
Big |
Bit,blob,longblob |
Blob |
1. Store large character data in the database (text data in MySQL Type field)
@Test Public voidWRITEINDB ()throwsException {//Get ConnectionsConnection =sqlutil.getconnection (); //Get ObjectPreparedStatement PreparedStatement =Connection.preparestatement ("INSERT into book values (?)"); //prepare a reader for reading local filesReader reader =NewFileReader (NewFile ("E:/test.txt")); //set Big Data parametersPreparedstatement.setclob (1, reader); //Execute SQL statementpreparedstatement.executeupdate (); //Close ResourceReader.close (); Sqlutil.close (PreparedStatement, connection); }
2. Read the large character file from the database to the local
@Test Public voidReadfromdb ()throwsException {//Get ConnectionsConnection =sqlutil.getconnection (); //Creating ObjectsPreparedStatement PreparedStatement =Connection.preparestatement ("Select content from book"); //Setting Parameters//preparedstatement.setobject (1, "book"); //Get ResultsResultSet res =Preparedstatement.executequery (); //get large character content in string form while(Res.next ()) {String content= res.getstring ("Content"); SYSTEM.OUT.PRINTLN (content); } //Close ResourceSqlutil.close (PreparedStatement, connection); }
There is a second way to get results:
@Test Public voidReadfromdb ()throwsException {//Get ConnectionsConnection =sqlutil.getconnection (); //Creating ObjectsPreparedStatement PreparedStatement =Connection.preparestatement ("Select content from book"); //Setting Parameters//preparedstatement.setobject (1, "book"); //Get ResultsResultSet res =Preparedstatement.executequery (); FileWriter FileWriter=NewFileWriter (NewFile ("D:/11021.txt")); //using CLOB objects if(Res.next ()) {Clob Clob= Res.getclob ("Content"); Reader Reader=Clob.getcharacterstream (); //then write the reader to a local file. Char[] cr =New Char[1024]; intLen = 0; while(len = Reader.read (CR))!=-1) {Filewriter.write (CR,0, Len); } reader.close (); } //Close ResourceFilewriter.close (); Sqlutil.close (PreparedStatement, connection); }
The above is the large character file read and write ~ below we demonstrate to the operation of the big-byte file ~
4. Write large-byte files to the database
@Test Public voidWRITEINDB ()throwsException {//Get ConnectionsConnection =sqlutil.getconnection (); //Get ObjectPreparedStatement PreparedStatement =Connection.preparestatement (Insert into book values (?,?)); //prepare a InputStream for reading local filesInputStream in =NewFileInputStream (NewFile ("F:/computer.jpg")); //set Big Data parametersPreparedstatement.setobject (1, 1); Preparedstatement.setblob (2, in); //You can also use this//Preparedstatement.setbinarystream (2, in); //Execute SQL statementpreparedstatement.executeupdate (); //Close ResourceIn.close (); Sqlutil.close (PreparedStatement, connection); }
5. Read the large byte file from the database to the local
@Test Public voidReadfromdb ()throwsException {//Get ConnectionsConnection =sqlutil.getconnection (); //Creating ObjectsPreparedStatement PreparedStatement =Connection.preparestatement ("Select content from book where id=?"); //Setting ParametersPreparedstatement.setint (1, 1); //Get ResultsResultSet res =Preparedstatement.executequery (); FileOutputStream out=NewFileOutputStream (NewFile ("D:/999.jpg")); //using BLOB objects if(Res.next ()) {//blob blob = Res.getblob ("content"); //InputStream in = Blob.getbinarystream ();//that's fine.InputStream in= Res.getbinarystream ("Content"); //then write the reader to a local file. byte[] buf =New byte[1024]; intLen = 0; while(len = In.read (buf))!=-1) {out.write (buf,0, Len); } in.close (); Out.close (); } //Close ResourceSqlutil.close (PreparedStatement, connection); }
The transmission of JDBC's Big data content