mysql中clob和blob的區別:1、含義不同,clob指代的是字符大對象,而blob指代的是二進制大對象;2、作用不同,clob在數據庫中通常用來存儲大量的文本數據,即存儲字符數據,而blob用于存儲二進制數據或文件,常常為圖片或音頻。
本教程操作環境:windows7系統、mysql8版本、Dell G3電腦。
MySQL中的blob和clob的區別
1、含義不同
clob英文全稱:Character Large Object(字符大對象)
blob其全稱:binary large object(二進制大對象)
估計由英文名就能想到他們的作用,所以我們記東西的時候要聯想記憶,不能全靠死記硬背。
2、作用不同
clob在數據庫中通常用來存儲大量的文本數據,即存儲字符數據。
blob用于存儲二進制數據或文件,常常為圖片或音頻。
MySQL中的blob和clob的詳解示例
clob
clob用于存儲大量的文本數據。大字段的操作常常以流的方式處理。
相關類型如下:
類型 | 最大大小 |
---|---|
TinyText? | 255字節 |
Text? | 65535字節(約65K) |
MediumText? | 16 777 215字節(約16M) |
LongText? | 4 294 967 295 (約4G) |
創建person表
CREATE?TABLE?person?( ??name?varchar(20), ??address?text );
插入數據
import?java.io.File; import?java.io.FileReader; import?java.sql.Connection; import?java.sql.DriverManager; import?java.sql.PreparedStatement; import?java.sql.SQLException; ? public?class?clob?{ /** ?*?@param?args ?*/ //驅動程序就是之前在classpath中配置的JDBC的驅動程序的JAR?包中 public?static?final?String?DBDRIVER?=?"com.mysql.jdbc.Driver"; //連接地址是由各個數據庫生產商單獨提供的,所以需要單獨記住 public?static?final?String?DBURL?=?"jdbc:mysql://192.168.0.4:3306/myDB"; //連接數據庫的用戶名 public?static?final?String?DBUSER?=?"root"; //連接數據庫的密碼 public?static?final?String?DBPASS?=?""; public?static?void?main(String[]?args)?throws?Exception?{ Connection?con?=?null;? PreparedStatement?stmt?=?null; try?{ //1、加載數據庫驅動程序 Class.forName(DBDRIVER);? //2、連接數據庫 con?=?DriverManager.getConnection(DBURL,DBUSER,DBPASS);? //3、創建Statement? stmt?=?con.prepareStatement("insert?into?person(name,address)?values(?,?)"); stmt.setString(1,"April"); stmt.setClob(2,?new?FileReader(new?File("D:workinfo.txt")));???????? //4、執行SQL語句 stmt.executeUpdate(); }catch(SQLException?e)?{ //5、異常處理 } finally?{ //6、清理資源 if(con?!=null) { con.close();? } if(stmt!=null) { stmt.close(); } } } }
寫入也可以使用語句
stmt.setClob(2,?new?BufferedReader(new?InputStreamReader(new?ByteArrayInputStream("四川省成都市高新區".getBytes()))));
執行結果
?讀取數據
stmt?=?con.prepareStatement("select?*?from?person"); rs?=?stmt.executeQuery(); while(rs.next()) { Clob?address?=?rs.getClob("address");? Reader?reader?=?address.getCharacterStream();? int?temp?=?0; while((temp?=?reader.read())?!=?-1)?? { System.out.print((char)temp); } reader.close(); }
執行結果
四川省成都市高新區 浙江省杭州市西湖區
blob
blob用于存儲二進制數據,常常為圖片或音頻。
? ?相關類型如下:
類型 | 最大大小 |
---|---|
TinyBlob? | 255字節 |
Blob? | 65535字節(約65K) |
MediumBlob? | 16 777 215字節(約16M) |
LongBlob? | 4 294 967 295 (約4G) |
? ??創建student表
CREATE?TABLE?student?( ??name?varchar(20), ??image?blob );
? ? 插入數據
stmt?=?con.prepareStatement("insert?into?student(name,image)?values(?,?)"); stmt.setString(1,"April"); stmt.setBlob(2,?new?FileInputStream("D:workApril.png")); stmt.executeUpdate();
? ?? 讀取數據
stmt?=?con.prepareStatement("select?*?from?student"); rs?=?stmt.executeQuery(); while(rs.next()) { Blob?image?=?rs.getBlob("image");? InputStream?in?=?image.getBinaryStream();? OutputStream?out?=?new?FileOutputStream("D:workHarris.png"); int?temp?=?0; while((temp?=?in.read())?!=?-1)?? { out.write(temp); } in.close(); out.close(); }
【相關推薦:mysql視頻教程】
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END
喜歡就支持一下吧
相關推薦