當前位置:首頁 » 文件管理 » oracle上傳blob

oracle上傳blob

發布時間: 2022-07-26 12:23:06

1. oracle BLOB上傳文件

1.首先你需要保證插入的欄位是一個BLOB空值
如果是存在的請初始化成 empty_blob();
update yourtablename t set t.yourcol = empty_blob()
where xxx = xxx ;
如果是不存在的請
insert 的時候插入一個empty_blob()空值。

2.然後查詢出這條FOR UPDATE
select t.yourcol
into b_content
from yourtablename t
where XXX = XXX for update;
3.引用dbms_lob編輯大欄位,c_content是你的文件內容
DBMS_LOB.OPEN(b_content, DBMS_LOB.LOB_READWRITE);
DBMS_LOB.WRITE(b_content, n_length, 1, UTL_RAW.cast_to_raw(c_content));
DBMS_LOB.CLOSE(b_content);
4.最後
commit;

2. oracle 如何將圖片批量插入或者上傳到資料庫里(blob類型)

准備用什麼語言寫?可以用C寫的,OCI編程。DEMO中有的。

3. oracle資料庫中我採用blob 欄位類型

blob 是存儲大數據處理的,一般語句網上都有,存儲blob一般採用的是上傳的方式,比如說,一條記錄要插入資料庫,最後一個欄位是blob型的,插入的時候必需注意的是(創建一個空的blob空間),如果不這樣做的話,基本上很難實現存儲。

4. 如何往oracle中的blob欄位寫入照片數據

往oracle裡面類型為blob寫入時,必須先插入一個empty_blob,實行update……
具體java裡面寫入blob的代碼如下:

public class applyPhotoBLOB {
final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";

public static void main(String[] args) {
// TODO Auto-generated method stub
Connection connSDC = null;
Connection conn = null;
String sConnStr = "jdbc:oracle:thin:@127 0.0 1 1521:sle";
String sConnStrSDC = "jdbc:oracle:thin:@10 10 8.12:1521:rac2";
String sDBUid = "test";
String sDBPwd = "test";
String sDBUidSDC = "sdcmanager";
String sDBPwdSdc = "sdcmanager_888";

try
{
applyPhotoBLOB apply = new applyPhotoBLOB();
connSDC = apply.getConn(sConnStrSDC,sDBUidSDC,sDBPwdSdc);

if(connSDC!=null)
{
apply.testBOLB(connSDC);
}

System.out.println("處理完成!");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(conn!=null) conn.close();
if(connSDC!=null) connSDC.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

public void testBOLB(Connection conn) throws Exception
{
String strsql = "Insert Into BKS_XSZPXX(XH,ZPLXM,ZP) Values('3071801040','1',empty_blob())";
updateTable1(strSQL,conn);
conn.setAutoCommit(false);
strSQL = "Select ZP from BKS_XSZPXX where XH='3071801040' For Update";

Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement();
rs = stmt.executeQuery(strSQL);
rs.next();
BLOB blob = (BLOB) rs.getBlob("ZP");
OutputStream os = blob.getBinaryOutputStream();// 建立輸出流
BufferedOutputStream output = new BufferedOutputStream(os);
BufferedInputStream input = new BufferedInputStream(new File("F:/3071801040.jpg").toURL().openStream());
byte[] buff = new byte[2048000]; //用做文件寫入的緩沖
int bytesRead;
while(-1 != (bytesRead = input.read(buff, 0, buff.length)))
{
output.write(buff, 0, bytesRead);
//System.out.println(bytesRead);
}
output.close();
input.close();
rs.close();
conn.commit();
conn.setAutoCommit(true);
stmt.close();
}

private int updateTable1(String strSQL,Connection conn) throws Exception
{
PreparedStatement stmt = null;
int result = 0;
try
{

stmt = conn.prepareStatement(strSQL);
result = stmt.executeUpdate();
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
stmt.close();
}
return result ;
}

public Connection getConn(String StrConn,String uid,String pwd) throws Exception
{
Connection conn = null;
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(StrConn,uid,pwd);
}
catch (Exception e)
{
throw new Exception(e.getMessage());
}
return conn;
}

}

另外:放入business裡面的時候,其zp最好定義為InputStream
轉載,僅供參考。

5. 怎麼在oracle中存放blob類型

BLOB和CLOB都是大欄位類型,BLOB是按二進制來存儲的,而CLOB是可以直接存儲文字的。其實兩個是可以互換的的,或者可以直接用LOB欄位代替這兩個。但是為了更好的管理ORACLE資料庫,通常像圖片、文件、音樂等信息就用BLOB欄位來存儲,先將文件轉為二進制再存儲進去。而像文章或者是較長的文字,就用CLOB存儲,這樣對以後的查詢更新存儲等操作都提供很大的方便。

6. 如何把 上傳的文件 轉成二進制存入oracle中的BLOB欄位上,懇請高手們指導一下

更新數據的時候,寫insert語句的時候,不更新blob欄位,blob欄位對應的數據用 empty_blob() 代替就行。

(注意:在執行上面那個 sql 之前,一定要把 connection 設置成不自動提交: conn.setAutoCommit(false); )

最後單獨出來blob欄位:
//把 blob 欄位取出來
String sql = "select ANNEX from market_info_collect_t where info_id='"
+ infoId + "' for update ";
Statement stt=null;
stt = conn.createStatement();
rs = stt.executeQuery(sql);
if (rs.next()) {
blob = (oracle.sql.BLOB) rs.getBlob("ANNEX");
outStream = blob.getBinaryOutputStream();
instream = myFile.getContentStream();
byte[] data = new byte[instream.available()];
instream.read(data);
outStream.write(data, 0, data.length);
}
instream.close();
outStream.flush();
outStream.close();

上面這段是我以前寫過的代碼中的一部分,你看一下,希望對你能有幫助。。。

7. 給oracle中的存儲過程傳遞一個Blob類型的欄位,java代碼怎麼寫

現在 在看到你的問題 。 我把代碼貼給你
條件准備

Oracle資料庫中有表如下
CREATE TABLE BOOK(

ID NUMBER PRIMARY KEY,

F BLOB

);

ConnectionManager類不解釋。

package org.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* 此類掩飾java操作Oracle BLOB類型
* @author wanghao
*
*/
public class SaveFile {

private final static String PSQL = "insert into BOOK(ID,F) " + "values(?,EMPTY_BLOB())";

private final static String SSQL = "select F from BOOK where ID = ? for update";

private final static String USQL = "update BOOK set F = ? where ID = ?";

/**
* 將文件存入資料庫
* @param file 要存入資料庫的文件
* @param id 為了掩飾方便,手動輸入 ID
* @throws Exception
*/
public void save(File file,int id) throws Exception {
Connection conn = ConnectionManager.getConnection();
conn.setAutoCommit(false);
PreparedStatement pstmt = null;
PreparedStatement pstmt2 = null;
try {
pstmt = conn.prepareStatement(PSQL);
pstmt2 = conn.prepareStatement(USQL);
pstmt.setInt(1, id);
try {
pstmt.executeUpdate();// 在資料庫中插入空對象
} catch (SQLException ex) {
ex.printStackTrace();
}
pstmt = conn.prepareStatement(SSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();// 查詢新插入的記錄
oracle.sql.BLOB pc = null;
while (rs.next()) {
pc = (oracle.sql.BLOB) rs.getBlob(1);
}
byte[] data = file2Byte(file);
pc.putBytes(1, data);
pstmt2.setBlob(1, pc);
pstmt2.setInt(2, id);
pstmt2.executeUpdate();
conn.commit();
} finally {
try {
pstmt.close();
pstmt2.close();
} catch (Exception EE) {
}
}
}

/**
* 將文件轉成byte數組
* @param file 被轉成 byte數組的file
* @return
* @throws Exception
*/
public byte[] file2Byte(File file) throws Exception{
FileInputStream fin = new FileInputStream(file);
byte[] b = new byte[(int) file.length()];
fin.read(b);
fin.close();
return b;
}

/**
* 從資料庫中將文件寫入到本地硬碟
* @param id
* @throws Exception
*/
public void readFile(int id) throws Exception{
byte[] b = new byte[1024];
Connection conn = ConnectionManager.getConnection();
PreparedStatement pstmt = null;
String sql = "select F from BOOK where ID = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();// 查詢新插入的記錄
oracle.sql.BLOB pc = null;
while (rs.next()) {
pc = (oracle.sql.BLOB) rs.getBlob(1);
}
InputStream in = pc.getBinaryStream();
OutputStream os = new FileOutputStream(new File("T:\\io\\java_temp.pdf"));
int len = 0;
while((len=in.read(b))!=-1){
os.write(b,0,len);
}
os.flush();
os.close();
in.close();
rs.close();
pstmt.close();
conn.close();
}

public static void main(String[] args) throws Exception{
SaveFile sf = new SaveFile();
// File file = new File("T:\\io\\java.pdf");
int id = 2;
// sf.save(file, id);
sf.readFile(id);
}
}
這是我根據網路上的例子改寫的,比較清晰,希望對你有幫助!

熱點內容
java漢諾塔遞歸演算法 發布:2025-04-02 06:28:40 瀏覽:126
可執行文件是編譯鏈接後生成的文 發布:2025-04-02 04:36:44 瀏覽:174
電腦文件加密軟體免費 發布:2025-04-02 03:02:51 瀏覽:806
php圖片管理 發布:2025-04-02 03:01:11 瀏覽:266
然後弄編程 發布:2025-04-02 02:54:06 瀏覽:114
解壓室俱樂部 發布:2025-04-02 02:47:04 瀏覽:282
安卓哪裡下載文豪野犬 發布:2025-04-02 02:45:04 瀏覽:790
優酷安卓怎麼免廣告 發布:2025-04-02 02:30:07 瀏覽:834
安卓系統怎麼把繁體字改為簡體字 發布:2025-04-02 02:14:39 瀏覽:326
androidpos機 發布:2025-04-02 01:40:54 瀏覽:374