最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解析使用jdbc,hibernate處理clob/blob字段的詳解

 更新時(shí)間:2013年05月16日 09:39:13   作者:  
本篇是對(duì)使用jdbc,hibernate處理clob/blob字段進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

(1)不同數(shù)據(jù)庫(kù)中對(duì)應(yīng)clob,blob的類型:
mysql中 : clob對(duì)應(yīng)text  blob對(duì)應(yīng)blob
db2/oracle中 clob對(duì)應(yīng)clob blob對(duì)應(yīng)blob

(2)domain中對(duì)應(yīng)類型:
clob 對(duì)應(yīng) String   blob 對(duì)應(yīng) byte[]
clob 對(duì)慶 java.sql.Clob blob 對(duì)應(yīng) java.sql.Blob

(3)hibernate配置文件中對(duì)應(yīng)類型:
clob > clob   blob > binay

也可以直接使用數(shù)據(jù)庫(kù)提供類型,例如:oracle.sql.Clob,oracle.sql.Blob。

2、jdbc操作clob (以oracle為例)
首先操作clob/blob不像操作varchar類型那樣簡(jiǎn)單,插入步驟一般為兩步:第一步插入一個(gè)空值,第二步鎖住此行,更新clob/blob字段.

復(fù)制代碼 代碼如下:

//插入空值
conn.setAutoCommit(false);
String sql = "insert into file(name,file_content) values("jack",EMPTY_CLOB());
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.executeUpdate();
//鎖住此行
String sql = "select file_content from file where name='jack' for update";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
oracle.sql.Clob clob = (oracle.sql.Clob)rs.getClob(1);
java.io.OutputStream writer = clob.getAsciiOutputStream();
byte[] temp = newFileContent.getBytes();
writer.write(temp);
writer.flush();
writer.close();
//
pstmt.close();


讀取內(nèi)容:
oracle.sql.Clob clob = rs.getClob("file_content");
if(null!=clob)
{
     Reader is = clob.getCharacterStream();
     BufferedReader br = new BufferedReader(is);
     String s = br.readLine();
    while (s != null)
    {
        content += s + "<br>"; 
        s = br.readLine();
    }
}


3、jdbc操作blob
復(fù)制代碼 代碼如下:

conn.setAutoCommit(false);
String sql = "insert into photo(name,photo) values("jack",empty_blob());
pstmt = conn.prepareStatement(sql);
pstmt = conn.executeUpdate();
//
sql = "select photo from photo where name='jack'";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery(sql);
if(rs.next())
     oracle.sql.Blob blob = (oracle.sql.Blob)rs.getBlob(1);
//write to a file
File file = new File("c:\\test.rar");
FileInputStream fin = new FileInputStream(file);
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
byte[] data = new Byte[blob.getBufferSize()];
while ((count = fin.read(data)) != -1)
{
         total += count;
         out.write(data, 0, count);
}

4、hibernateth處理clob
復(fù)制代碼 代碼如下:

MyFile file = new Myfile();
file.setName("jack");
file.setContent(hibernate.createClob(""));
session.save(file);
session.flush();
session.refresh(file,LockMode.UPGRADE);
oracle.sql.Clob clob = (oracle.sql.Clob)file.getContent();
Writer pw = clob.getCharacterOutputStream();
pw.write(longText);//寫入長(zhǎng)文本
pw.close();
session.close();

5、使用hibernate處理blob:
復(fù)制代碼 代碼如下:

原理基本相同:
Photo photo = new Photo();
photo.setName("jack");
photo.setPhoto(hibernate.createBlob(""))://放一個(gè)空值
session.save(photo);
session.flush();
//
session.refresh(photo,LockMode.UPGRADE); //鎖住此對(duì)象
oracle.sql.Blob blob = photo.getPhoto();//取得此blob的指針
OutputStream out = blob.getBinaryOutputStream();   
//寫入一個(gè)文件
File f = new File("c:\\test.rar");
FileInputStream fin = new FileInputStream(f);   
int count = -1, total = 0;
byte[] data = new byte[(int)fin.available()];
out.write(data);     
fin.close();
out.close();
session.flush();

相關(guān)文章

  • 在springboot文件中如何創(chuàng)建mapper.xml文件

    在springboot文件中如何創(chuàng)建mapper.xml文件

    這篇文章主要介紹了在springboot文件中如何創(chuàng)建mapper.xml文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • java GUI實(shí)現(xiàn)五子棋游戲

    java GUI實(shí)現(xiàn)五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了java GUI實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • struts2 中文亂碼的解決辦法分享

    struts2 中文亂碼的解決辦法分享

    這篇文章主要介紹了struts2 中文亂碼的解決辦法,需要的朋友可以參考下
    2014-02-02
  • java使用stream判斷兩個(gè)list元素的屬性并輸出方式

    java使用stream判斷兩個(gè)list元素的屬性并輸出方式

    這篇文章主要介紹了java使用stream判斷兩個(gè)list元素的屬性并輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringCloudConfig之client端報(bào)錯(cuò)Could?not?resolve?placeholder問(wèn)題

    SpringCloudConfig之client端報(bào)錯(cuò)Could?not?resolve?placeholder問(wèn)

    這篇文章主要介紹了SpringCloudConfig之client端報(bào)錯(cuò)Could?not?resolve?placeholder?‘from‘?in?value?“${from}“問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2022-12-12
  • Spring中的事務(wù)控制知識(shí)總結(jié)

    Spring中的事務(wù)控制知識(shí)總結(jié)

    我們講了轉(zhuǎn)賬方法存在著事務(wù)問(wèn)題,當(dāng)在業(yè)務(wù)層方法更新轉(zhuǎn)入賬戶時(shí)發(fā)現(xiàn)異常,更新收款方賬戶則會(huì)出錯(cuò).當(dāng)時(shí)是通過(guò)自定義事務(wù)管理器進(jìn)行整體事務(wù)的處理.其實(shí)Spring 提供了業(yè)務(wù)層的事務(wù)處理解決方案,并且 Spring 的事務(wù)控制都是基于 AOP 的,需要的朋友可以參考下
    2021-06-06
  • Java8函數(shù)式接口java.util.function速查大全

    Java8函數(shù)式接口java.util.function速查大全

    因?yàn)镴ava8引入了函數(shù)式接口,在java.util.function包含了幾大類函數(shù)式接口聲明,這篇文章主要給大家介紹了關(guān)于Java8函數(shù)式接口java.util.function速查的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Java一維數(shù)組和二維數(shù)組元素默認(rèn)初始化值的判斷方式

    Java一維數(shù)組和二維數(shù)組元素默認(rèn)初始化值的判斷方式

    這篇文章主要介紹了Java一維數(shù)組和二維數(shù)組元素默認(rèn)初始化值的判斷方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • java 中自定義OutputFormat的實(shí)例詳解

    java 中自定義OutputFormat的實(shí)例詳解

    這篇文章主要介紹了java 中 自定義OutputFormat的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • Java連接SAP RFC實(shí)現(xiàn)數(shù)據(jù)抽取的示例詳解

    Java連接SAP RFC實(shí)現(xiàn)數(shù)據(jù)抽取的示例詳解

    這篇文章主要為大家學(xué)習(xí)介紹了Java如何連接SAP RFC實(shí)現(xiàn)數(shù)據(jù)抽取的功能,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以了解下
    2023-08-08

最新評(píng)論

胶南市| 天柱县| 赫章县| 资阳市| 平湖市| 宁阳县| 宜都市| 师宗县| 雷山县| 安塞县| 邵武市| 淅川县| 淮安市| 东港市| 阿拉尔市| 当阳市| 郯城县| 宝山区| 阳曲县| 平湖市| 隆尧县| 大英县| 安阳市| 垣曲县| 江孜县| 仪征市| 隆林| 保靖县| 望谟县| 高碑店市| 阜新| 南丰县| 洞头县| 常宁市| 南宫市| 桂东县| 申扎县| 三穗县| 禄劝| 天台县| 嫩江县|