Dark Horse Programmer _ Super Comprehensive Javaweb video tutorial vedio\ Dark Horse Programmer _ Ultra Comprehensive Javaweb Tutorial-source note \javaweb Video tutorial _day17-data source \day17_code\day17_1\
Big Data
Goal: Save the MP3 to the database!
Add the following configuration in My.ini!
max_allowed_packet=10485760
1 What is Big Data
The so-called Big data, is the large byte data, or large character data. The following types are available in standard SQL to hold large data types:
Type |
Length |
Tinyblob |
28--1b(256B) |
Blob |
216-1b(64K) |
Mediumblob |
224-1b(16M) |
Longblob |
232-1b(4G) |
Tinyclob |
28--1b(256B) |
Clob |
216-1b(64K) |
Mediumclob |
224-1b(16M) |
Longclob |
232-1b(4G) |
However, in MySQL , tinyclob,clob,mediumclob, Longclob four types, but uses the following four types to handle text Big data:
Type |
Length |
Tinytext |
28--1b(256B) |
Text |
216-1b(64K) |
Mediumtext |
224-1b(16M) |
Longtext |
232-1b(4G) |
first we need to create a table with a Mediumblob ( 16M ) type of field.
CREATE TABLE tab_bin (id INT PRIMARY KEY auto_increment,filenamevarchar (), data mediumblob);
inserting binary data into a database needs to be used PreparedStatement as the original setbinarystream (int, inputsteam) method to complete.
con = Jdbcutils. Getconnection (); string sql = "INSERT into Tab_bin (filename,data) VALUES (?,?)"; pstmt = Con.preparestatement (sql); pstmt.setstring (1, "a.jpg"); inputstream in = new fileinputstream ("f:\\a.jpg"); pstmt.setbinarystream (2, in); pstmt.executeupdate (); |
read binary data, need to use after query resultset getbinarystream () method to get the input stream object. In other words, preparedstatement setxxx () , then resultset getxxx () Span style= "font-family: the song Body;" >
Con = jdbcutils. getconnection (); String sql = "Select Filename,data from Tab_bin where id=?"; pstmt = con.preparestatement (sql); Pstmt.setint (1, 1); rs = Pstmt.executequery (); Rs.next (); String filename = rs.getstring ("filename"); OutputStream out = new fileoutputstream ("f:\\" + filename); InputStream in = Rs.getbinarystream ("Data"); Ioutils. Copy (in, out); Out.close (); |
another way is to wrap the data that you want to store into Blob type, and then call preparedstatement 's setblob () method to set the data
con = jdbcutils. Getconnection (); String sql = "INSERT into Tab_bin (filename,data) VALUES (?,?)"; Pstmt = con.preparestatement (sql); Pstmt.setstring (1, "a.jpg"); File File = new file ("f:\\a.jpg"); byte [] datas = FileUtils. getBytes (file);// Get data from a file Blob blob = new serialblob ( datas);// blob object Pstmt.setblob (2, BLOB);// settings pstmt.executeupdate (); |
Con = jdbcutils. getconnection (); String sql = "Select Filename,data from Tab_bin where id=?"; pstmt = con.preparestatement (sql); Pstmt.setint (1, 1); rs = Pstmt.executequery (); Rs.next (); String filename = rs.getstring ("filename"); File File = new file ("f:\\" + filename); Blob blob = Rs.getblob ("Data"); byte [] datas = blob.getbytes (0, (int) file.length ()); FileUtils. Writebytearraytofile (file, datas); |
The class teacher knocks the code:
PackageCn.itcast.demo4;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.sql.Blob;Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;ImportJavax.sql.rowset.serial.SerialBlob;Importorg.apache.commons.io.IOUtils;Importorg.junit.Test;Importcn.itcast.demo3.JdbcUtils;/*** Big Data *@authorCXF **/ Public classDemo4 {/*** Save the MP3 to the database. * @throwsSQLException *@throwsIOException *@throwsFileNotFoundException*/@Test Public voidFUN1 ()throwsException {/** 1. Get connection * 2. Give the SQL template, create PSTMT * 3. Set the parameters in the SQL template * 4. Call Pstmt's executeupdate () Line*/Connection con=jdbcutils.getconnection (); String SQL= "INSERT into Tab_bin values (?,?,?)"; PreparedStatement pstmt=con.preparestatement (SQL); Pstmt.setint (1, 1); Pstmt.setstring (2, "Streamer flying. mp3"); /*** need to get BLOB * 1. We have the file, the target is blob * 2. First turn the file into byte[] * 3. Use byte[] to create a blob*/ //convert files to byte[] byte[] bytes = Ioutils.tobytearray (NewFileInputStream ("f:/Streamer MP3")); //Create a BLOB using byte[]Blob blob =NewSerialblob (bytes); //Setting ParametersPstmt.setblob (3, BLOB); Pstmt.executeupdate (); } /*** Read mp3 from database *@throwsSQLException*/@Test Public voidFun2 ()throwsException {/** 1. Create connection*/Connection con=jdbcutils.getconnection (); /** 2. Give the SELECT statement template to create the pstmt*/String SQL= "SELECT * FROM Tab_bin"; PreparedStatement pstmt=con.preparestatement (SQL); /** 3. pstmt execute query, get resultset*/ResultSet rs=Pstmt.executequery (); /** 4. Get column data called data in RS*/ if(Rs.next ()) {blob blob= Rs.getblob ("Data"); /** Turn blob into a file on your hard drive! */ /** 1. Get input Stream object through BLOB * 2. Create an output stream object yourself * 3. Write data from the input stream to the output stream*/InputStream in=Blob.getbinarystream (); OutputStream out=NewFileOutputStream ("C:/lgfw.mp3"); Ioutils.copy (in, out); } }}
Big Data-Save MP3 to database and read out "Dark Horse programmer _ ultra-comprehensive javaweb video tutorial Vedio" Day17