開發者學堂課程【JDBC資料庫開發入門:大資料】學習筆記,與課程緊密聯系,讓使用者快速學習知識。
課程位址: https://developer.aliyun.com/learning/course/29
大資料
目标:把MP3轉移到資料庫。
在my.ini中添加如下配置!
max. allowed packet=10485760
大資料位元組的類型:
類型 tinyblobe blobe mediumblobe longblobe | 長度 2-1B (256B) 215.1B (64K) 224-1B (16M) 232-1B (4G) |
字元類型:
tinydlobe clobe mediumdlobe longclob | 25-18 (256B) 215-18 (64K) 241B (16M) 2-1B (4G) |
具體儲存步驟:
需要得到Blob:⑴有的是檔案,目标是Blob⑵先把檔案變成byte[]⑶再使用byte建立Blob
把mp3儲存到資料庫中:⑴得到Connection⑵給出sql模闆,建立pstmt⑶設定sql模闆中的參數⑷調用pstmt的executeUpdate()執行
例如:
Connection con = JdbcUtils. getConnection() ;
string sql = "insert into tab_ bin values(?,?, ?)";
Preparedstatement pstmt = con. prepareStatement (sql);
pstmt.setInt(1, 1);
pstmt.setstring(2, "流光飛舞.mp3");
Blob blob = null;
pstmt . setBlo(3,blob )
pstmt. executeUpdate();
把檔案轉換成byte[]
byte[] bytes . IoUtils. toByteArzay(new PileInputstream ("F:/流光飛舞 .mp3"));
Blob blob . nul 1
patmt . setBlob(3, blob) ;
pa tmt . executeUpdate()
從資料庫中讀取mp3
1. 建立Cohnectlon
Connection con = JdbcUtils. getConnection();
2.給出select語 句橫闆,建立pstmt
String sq1 = "select from tab bin";
Preparedstatement pstmt = con.prepareStatement(sq1) ;
.3. pstmt執行查詢,得到Resultset
ResultSet rs = pstmt .executeQuery() ;
4.擷取rs中名為data的列資料
if(rs.next()) {
Blob blob = rs.getBlob ("data");
.把Blob變成硬碟上的檔案!
1.通過Blob得到輸入流對象
2.自己建立輸出流對象
3.把輸入流的資料寫入到輸出流中
InputStream in = blob.getBinaryStream() ;
OutputStream out = new FileOutputStream("c:/1gfw .mp3");
Ioutils.copy(in, out) ;