日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

BLOB 操作

發布時間:2023/12/13 综合教程 31 生活家
生活随笔 收集整理的這篇文章主要介紹了 BLOB 操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對于數據庫是BLOB類型存儲數據。

BLOB數據插入:

Oracle提供的標準方式: 先插入一個空BLOB對象,然后Update這個空對象。

首先使用empty_blob()函數插入一個空BLOB對象。

然后重新查詢BLOB,使用for update鎖字段,注意關閉連接和回滾或提交。

ResultSet 獲取 BLOB對象。

BLOB.setByte(byte); //這個無效,數據存不進去,不能用

或者 OutputStream os = blob.setBinaryStream(0); // 0設置起始位置

blob.getOutputStream(); //已經deprecated

ResultSet 和 Statement 會在連接關閉時自動關閉,因此無需自己關閉。

    /**
     * 保存附件Blob
     * @param attachId
     * @throws SQLException 
     */
    private void setAttachBlob(long attachId, String fileData) throws SQLException {
        logger.debug(">>>setAttachBlob(long attachId)");
        StringBuilder sql = new StringBuilder();
        sql.append(" select file_data fileData from test_attach where attach_id=" + attachId + "  for update");
        Connection connection = null;
        try {
            byte[] byteStream = Base64.decodeBase64(fileData.getBytes("UTF-8"));
            connection = super.getDbHelper().getConnection();
            connection.setAutoCommit(false);

ResultSet rs = connection.prepareStatement(sql.toString())
.executeQuery();
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("fileBody");
OutputStream os = blob.setBinaryStream(0);
ByteArrayInputStream is = new ByteArrayInputStream(byteStream);
byte[] byteData = new byte[512];
int len = 0;
try {
while((len = is.read(byteData, 0 , 512)) != -1) {
os.write(byteData, 0, len);
}
os.flush();
} catch (IOException e) {
logger.error("file read write error.", e);
} finally {
if(is != null) {
is.close();
}
if(os != null) {
os.close();
}
}
}

            connection.commit();
        } catch (UnsupportedEncodingException e) {
            logger.error("decode error  file get byte...", e);
            e.printStackTrace();
        } catch (DbException e) {
            connection.rollback();
            logger.error("blob save error...", e);
        }finally {
            // 關閉連接
            if(connection != null && !connection.isClosed()) {
                connection.close();
            }
        }
        logger.debug("<<<setAttachBlob(long attachId)");
    }

獲取BLOB比較簡單:

ResultSet rs = st.executeQuery( "select file_data from  test_attach  where  attach_id=103 ");

if (rs.next()) {

   java.sql.Blob blob = rs.getBlob("file_data ");

   InputStream ins = blob.getBinaryStream();

總結

以上是生活随笔為你收集整理的BLOB 操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。