如何向各種資料庫中寫入blob型別的記錄

2021-04-07 03:12:32 字數 1138 閱讀 4527

一 對於mssql和sybase,方法很簡單,採用statement.setbinarystream方法就可以了

二 對oracle的blob型別的字段,不能直接寫入,有特殊的操作方法:

1、首先插入乙個記錄,該記錄中blob欄位為空

string insert = "insert into test_table (id,name,details) values (?,?,empty_blob());

statement = conn.preparestatement(insert);

statement.setint(1, 1);

statement.setshort(2, "mike");

statement.executeupdate();

2、更新該條記錄,設定blob欄位的值,注意需要先採用select .. for update 的形式將blob欄位從資料庫中取出來,然後為blob欄位設定值,最後再用update語句更新

byte detatils = new byte[1000];

string get_cond_sql_oracle = "select details from test_table where id = ? for update";

ps1 = con.preparestatement(get_cond_sql_oracle);

ps1.setint(1, 1);

rs1 = ps1.executequery();

rs1.next();

oracle.sql.blob blob = ((oracleresultset) rs1).getblob(1);

blob.putbytes(1, detatils );

string update_cond_sql_oracle = "update test_table set details=?  where id= ? ";

ps2 = con.preparestatement(update_cond_sql_oracle);

ps2.setblob(1, blob);

ps2.setint(2, 1);

ps2.executeupdate();

各種資料庫連線

mysql string driver com.mysql.jdbc.driver 驅動程式 string url jdbc mysql localhost 3306 db name 連線的url,db name為資料庫名 string username username 使用者名稱 string ...

各種資料庫連線

mysql string driver org.gjt.mm.mysql.driver 驅動程式 string url jdbc mysql localhost 3306 db name 連線的url,db name為資料庫名 string username username 使用者名稱 strin...

各種資料庫驅動

1 oracle8 8i 9i資料庫 thin模式 class.forname oracle.jdbc.driver.oracledriver newinstance string url jdbc oracle thin localhost 1521 orcl orcl為資料庫的sid strin...