Oracle中大文本數(shù)據(jù)類型
Clob 長(zhǎng)文本類型 (MySQL中不支持,使用的是text)
Blob 二進(jìn)制類型
MySQL數(shù)據(jù)庫
Text 長(zhǎng)文本類型
TINYTEXT: 256 bytes
TEXT: 65,535 bytes => ~64kb
MEDIUMTEXT: 16,777,215 bytes => ~16MB
LONGTEXT: 4,294,967,295 bytes => ~4GB
Blob 二進(jìn)制類型
例如:
建表
CREATE TABLE test(
id INT PRIMARY KEY AUTO_INCREMENT,
content LONGTEXT, -- 文本字段
img LONGBLOB -- 圖片字段
);
存儲(chǔ)文本時(shí)是以字符類型存儲(chǔ),存儲(chǔ)圖片時(shí)是以二進(jìn)制類型存儲(chǔ),具體使用的設(shè)置參數(shù)方法,和獲取數(shù)據(jù)方法不同。
例如:
// 存儲(chǔ)文本時(shí)
// 存儲(chǔ)時(shí),設(shè)置參數(shù)為字符流 FileReader reader
pstmt.setCharacterStream(1, reader);
// 獲取參數(shù)時(shí)
// 方式1:
Reader r = rs.getCharacterStream("content");
// 獲取長(zhǎng)文本數(shù)據(jù), 方式2:
System.out.print(rs.getString("content"));
// 存儲(chǔ)二進(jìn)制圖片時(shí)
// 設(shè)置參數(shù)為2進(jìn)制流 InputStream in
pstmt.setBinaryStream(1, in);
// 獲取2進(jìn)制流
InputStream in = rs.getAsciiStream("img");
/**
* 保存照片
*
*/
@Test
public void test2(){
String sql = "insert into test(img) values(?)";
try{
con = JDBCUtil.getConnection();
pstmt = con.prepareStatement(sql);
// 設(shè)置參數(shù)
// 獲取文本
File file = new File("f:/a.jpg");
InputStream in = new FileInputStream(file);
// 設(shè)置參數(shù)為2進(jìn)制流
pstmt.setBinaryStream(1, in);
// 執(zhí)行sql
pstmt.executeUpdate();
in.close();
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
JDBCUtil.close(con, pstmt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 獲取照片
*
*/
@Test
public void test3(){
String sql = "select * from test where id=?;";
try{
con = JDBCUtil.getConnection();
pstmt = con.prepareStatement(sql);
// 設(shè)置參數(shù)
pstmt.setInt(1, 2);
// 執(zhí)行查詢
rs = pstmt.executeQuery();
while(rs.next()){
byte[] buff = new byte[1024];
InputStream in = rs.getAsciiStream("img");
int l=0;
OutputStream out = new FileOutputStream(new File("f:/1.jpg"));
while((l=in.read(buff))!=-1){
out.write(buff, 0, l);
}
in.close();
out.close();
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
JDBCUtil.close(con, pstmt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
您可能感興趣的文章:- MySQL實(shí)現(xiàn)類似Oracle序列的方案
- mysql實(shí)現(xiàn)sequence功能的代碼
- Can''t connect to local MySQL through socket ''/tmp/mysql.sock''解決方法
- Mysql常用函數(shù)大全(分類匯總講解)
- 利用MySQL主從配置實(shí)現(xiàn)讀寫分離減輕數(shù)據(jù)庫壓力
- mysql+spring+mybatis實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離的代碼配置
- 如何徹底刪除mysql服務(wù)(清理注冊(cè)表)詳解
- 將圖片儲(chǔ)存在MySQL數(shù)據(jù)庫中的幾種方法
- Ubuntu上mysql的安裝及使用(通用版)
- insert和select結(jié)合實(shí)現(xiàn)"插入某字段在數(shù)據(jù)庫中的最大值+1"的方法