1. MySQL中,BLOB是一個二進制大型對象,是一個能夠存儲大量數據的容器,它能容納不一樣大小的數據。java
2.BLOB字段適用於存儲大量的二進制數據,如圖像、視頻、音頻,文件等。sql
3. MySQL的四種BLOB類型(除了在存儲的最大信息量上不一樣外,他們是等同的):數據庫
4. 實際使用中根據須要存入的數據大小定義不一樣的BLOB類型。須要注意的是:若是存儲的文件過大,數據庫的性能會降低。函數
主要操做:性能
InputStream inputStream = new FileInputStream("阿阮.jpg");
preparedStatement.setBlob(7,inputStream);學習
例:spa
@Test public void test14() { Connection conn = null; PreparedStatement preparedStatement = null; try { //獲取數據庫鏈接 conn = JDBCTools.getConnection(); //設置 SQL語句 String sql = "INSERT INTO Student(Sno, Sname, Ssex, Sage, Sdept, S_entrance, S_picture) " + "VALUES(?,?,?,?,?,?,?)"; //爲佔位符賦值 preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, "201307081"); preparedStatement.setString(2, "阿阮"); preparedStatement.setString(3, "女"); preparedStatement.setInt(4, 18); preparedStatement.setString(5, "GJ"); preparedStatement.setDate(6, Date.valueOf("2013-07-08")); InputStream inputStream = new FileInputStream("阿阮.jpg"); preparedStatement.setBlob(7,inputStream); //執行 SQL語句 preparedStatement.executeUpdate(); } catch(Exception e) { e.printStackTrace(); } finally { JDBCTools.release(null,preparedStatement,conn); } }
主要操做:.net
Blob picture = resultSet.getBlob(7);
InputStream in = picture.getBinaryStream();
OutputStream out = new FileOutputStream("11.jpg");
byte [] buffer = new byte[1024];
for(int len = in.read(buffer); len != -1; len = in.read(buffer)) {
out.write(buffer,0,len);
out.flush();
}
out.close();
in.close();code
例:視頻
@Test public void test15() { Connection conn = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { //1. 獲取鏈接 conn = JDBCTools.getConnection(); String sql = "SELECT * FROM Student " + "WHERE Sname='阿阮'"; //2. 獲取Statement preparedStatement = conn.prepareStatement(sql); //4. 執行查詢,獲得ResultSet resultSet = preparedStatement.executeQuery(); //5. 處理ResultSet while(resultSet.next()) { //按列讀取 String no = resultSet.getString(1); String name = resultSet.getString(2); String sex = resultSet.getString(3); int age = resultSet.getInt(4); String dept = resultSet.getString(5); Date entrance = resultSet.getDate(6); Blob picture = resultSet.getBlob(7); System.out.println(no+" "+name+" "+sex+" " +age+" "+dept+" "+entrance); InputStream in = picture.getBinaryStream(); OutputStream out = new FileOutputStream("11.jpg"); byte [] buffer = new byte[1024]; for(int len = in.read(buffer); len != -1; len = in.read(buffer)) { out.write(buffer,0,len); out.flush(); } out.close(); in.close(); } } catch(Exception e) { e.printStackTrace(); } finally { JDBCTools.release(resultSet,preparedStatement,conn); } }
JDBC學習筆記:
1. 獲取數據庫鏈接 http://my.oschina.net/daowuming/blog/704243
2. 經過Statement執行更新、查詢操做 http://my.oschina.net/daowuming/blog/704384
3. 使用PrepareStatement http://my.oschina.net/daowuming/blog/704432
4. 使用ResultSetMetaData 對象處理結果集元數據 http://my.oschina.net/daowuming/blog/704487
5. 使用DatabaseMetaData獲取數據庫信息 http://my.oschina.net/daowuming/blog/704553
6. BLOB ----當前----
7. 處理事務與隔離級別 http://my.oschina.net/daowuming/blog/704611
8. 批量處理 http://my.oschina.net/daowuming/blog/704641
9. 數據庫鏈接池 http://my.oschina.net/daowuming/blog/704700
10. 調用函數與存儲過程 http://my.oschina.net/daowuming/blog/704813