mysql中clob和blob的區(qū)別:1、含義不同,clob指代的是字符大對象,而blob指代的是二進制大對象;2、作用不同,clob在數(shù)據(jù)庫中通常用來存儲大量的文本數(shù)據(jù),即存儲字符數(shù)據(jù),而blob用于存儲二進制數(shù)據(jù)或文件,常常為圖片或音頻。
本教程操作環(huán)境:windows7系統(tǒng)、mysql8版本、Dell G3電腦。
MySQL中的blob和clob的區(qū)別
1、含義不同
clob英文全稱:Character Large Object(字符大對象)
blob其全稱:binary large object(二進制大對象)
估計由英文名就能想到他們的作用,所以我們記東西的時候要聯(lián)想記憶,不能全靠死記硬背。
2、作用不同
clob在數(shù)據(jù)庫中通常用來存儲大量的文本數(shù)據(jù),即存儲字符數(shù)據(jù)。
blob用于存儲二進制數(shù)據(jù)或文件,常常為圖片或音頻。
MySQL中的blob和clob的詳解示例
clob
clob用于存儲大量的文本數(shù)據(jù)。大字段的操作常常以流的方式處理。
相關(guān)類型如下:
類型 | 最大大小 |
---|---|
TinyText | 255字節(jié) |
Text | 65535字節(jié)(約65K) |
MediumText | 16 777 215字節(jié)(約16M) |
LongText | 4 294 967 295 (約4G) |
創(chuàng)建person表
CREATE TABLE person ( name varchar(20), address text );
插入數(shù)據(jù)
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 */ //驅(qū)動程序就是之前在classpath中配置的JDBC的驅(qū)動程序的JAR 包中 public static final String DBDRIVER = "com.mysql.jdbc.Driver"; //連接地址是由各個數(shù)據(jù)庫生產(chǎn)商單獨提供的,所以需要單獨記住 public static final String DBURL = "jdbc:mysql://192.168.0.4:3306/myDB"; //連接數(shù)據(jù)庫的用戶名 public static final String DBUSER = "root"; //連接數(shù)據(jù)庫的密碼 public static final String DBPASS = ""; public static void main(String[] args) throws Exception { Connection con = null; PreparedStatement stmt = null; try { //1、加載數(shù)據(jù)庫驅(qū)動程序 Class.forName(DBDRIVER); //2、連接數(shù)據(jù)庫 con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); //3、創(chuàng)建Statement stmt = con.prepareStatement("insert into person(name,address) values(?,?)"); stmt.setString(1,"April"); stmt.setClob(2, new FileReader(new File("D:\work\info.txt"))); //4、執(zhí)行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("四川省成都市高新區(qū)".getBytes()))));
執(zhí)行結(jié)果
讀取數(shù)據(jù)
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(); }
執(zhí)行結(jié)果
四川省成都市高新區(qū) 浙江省杭州市西湖區(qū)
blob
blob用于存儲二進制數(shù)據(jù),常常為圖片或音頻。
相關(guān)類型如下:
類型 | 最大大小 |
---|---|
TinyBlob | 255字節(jié) |
Blob | 65535字節(jié)(約65K) |
MediumBlob | 16 777 215字節(jié)(約16M) |
LongBlob | 4 294 967 295 (約4G) |
創(chuàng)建student表
CREATE TABLE student ( name varchar(20), image blob );
插入數(shù)據(jù)
stmt = con.prepareStatement("insert into student(name,image) values(?,?)"); stmt.setString(1,"April"); stmt.setBlob(2, new FileInputStream("D:\work\April.png")); stmt.executeUpdate();
讀取數(shù)據(jù)
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:\work\Harris.png"); int temp = 0; while((temp = in.read()) != -1) { out.write(temp); } in.close(); out.close(); }
【