用JDBC操縱BLOB和CLOB資料
在訪問Oracle 資料庫,對Oracle 的BLOB 和CLOB 進行操作的時候,當通過Oracle JDBC Driver 來呼叫的時,如下所例:
Driver myDriver = (Driver)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = myDriver.connect("jdbc:oracle:thin:" , props);
Statement stmt = conn.createStatement();
stmt.execute("select blob_content from lobtest where id=1");
ResultSet rs = stmt.getResultSet();
System.out.println("ResultSet result:"+rs);
while ( rs.next() ) ...{
System.out.println("BLOB:"+rs.getBlob("blob_content"));
myBlob = (oracle.sql.BLOB)rs.getBlob("blob_content");
}
返回的資料物件正確,是oracle.sql.BLOB 物件,然後可以使用該物件進行方法訪問。
但當使用Oracle JDBC Driver 配置連線池後,使用DataSource 取得資料庫連線之後,查詢CLOB 資料,賦值給oracle.sql.CLOB 的物件,這是就會報型別轉換錯誤。
在網路上有相應的解決方案,但都是依賴於某一種應用伺服器所提供的oracle連線環境。
下面是一個比較好的解決方案:
1。必須有支援jdbc3.0以上規範的oracle驅動。
2。更新blob欄位資料
public void updateBlob(String tableName, String blobFeild, String pryKey, String pryKeyValue, byte[] blob)
throws IOException, SQLException ...{
Statement stmt = null;
ResultSet rs = null;
try ...{
stmt = conn.createStatement();
String sql = "UPDATE " + tableName + " SET " + blobFeild + "=EMPTY_BLOB() WHERE " + pryKey + "='"
+ pryKeyValue + "'";
// 如果引數blob為null,清空blob值;否則先清空blob值,然後插入新的blob值。
if (blob == null) ...{
stmt.executeUpdate(sql);
} else ...{
stmt.executeUpdate(sql);
sql = "SELECT " + blobFeild + " FROM " + tableName + " WHERE " + pryKey + "='" + pryKeyValue
+ "' FOR UPDATE";
rs = stmt.executeQuery(sql);
if (rs.next()) ...{
Blob inBlob = rs.getBlob(blobFeild);
int len = inBlob.setBytes(1, blob);
log.debug("已成功儲存BLOB大物件:" + len / 1024 + "KB");
}
}
} finally ...{
DbUtils.close(rs);
DbUtils.close(stmt);
}
}
3。獲取blob欄位資料
public byte[] findBlob(String tableName, String blobFeild, String pryKey, String pryKeyValue) throws SQLException ...{
byte[] blob = null;
Statement stmt = null;
String sql = "SELECT " + blobFeild + " FROM " + tableName + " WHERE " + pryKey + "='" + pryKeyValue + "'";
ResultSet rs = null;
try ...{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) ...{
Blob outBlob = rs.getBlob(blobFeild);
if (outBlob != null && outBlob.length() > 0) ...{
blob = outBlob.getBytes(1, (int) outBlob.length());
}
}
} finally ...{
DbUtils.close(rs);
DbUtils.close(stmt);
}
return blob;
}
4。更新clob欄位資料
public void updateClob(String tableName, String clobFeild, String pryKey, String pryKeyValue, String clob)
throws IOException, SQLException ...{
Statement stmt = null;
ResultSet rs = null;
try ...{
stmt = conn.createStatement();
String sql = "UPDATE " + tableName + " SET " + clobFeild + "=EMPTY_CLOB() WHERE " + pryKey + "='"
+ pryKeyValue + "'";
// 如果引數clob為null,清空clob值;否則先清空clob值,然後插入新的clob值。
if (clob == null) ...{
stmt.executeUpdate(sql);
} else ...{
stmt.executeUpdate(sql);
sql = "SELECT " + clobFeild + " FROM " + tableName + " WHERE " + pryKey + "='" + pryKeyValue
+ "' FOR UPDATE";
rs = stmt.executeQuery(sql);
if (rs.next()) ...{
Clob inClob = rs.getClob(clobFeild);
int len = inClob.setString(1, clob);
log.debug("已成功儲存CLOB大物件:" + len / 1024 + "KB");
}
}
} finally ...{
DbUtils.close(rs);
DbUtils.close(stmt);
}
}
5。獲取clob欄位資料
public String findClob(String tableName, String clobFeild, String pryKey, String pryKeyValue) throws SQLException ...{
String clob = "";
Statement stmt = null;
String sql = "SELECT " + clobFeild + " FROM " + tableName + " WHERE " + pryKey + "='" + pryKeyValue + "'";
ResultSet rs = null;
try ...{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) ...{
Clob outClob = rs.getClob(clobFeild);
if (outClob != null && outClob.length() > 0) ...{
clob = outClob.getSubString(1, pryKeyValue.length());
}
}
} finally ...{
DbUtils.close(rs);
DbUtils.close(stmt);
}
return clob;
}
採用以上這種方式可以避免對連線池提供方的依賴。
相關文章
- JDBC 處理CLob和Blob型別資料JDBC型別
- JDBC:java提供的專門操縱資料庫的APIJDBCJava資料庫API
- JDBC複習,oracle的blob,clob的讀寫-zhaiJDBCOracleAI
- 使用SQL*Loader匯入CLOB和BLOB資料使用案例SQL
- 如何檢視ORACLE的LOB(BLOB和CLOB)物件佔用的大小Oracle物件
- CLOB與BLOB的轉換
- Oracle Blob 轉換為ClobOracle
- 我發現我的資料被操縱了……
- MYSQL中的DDL(用來操縱資料庫物件的語言)1MySql資料庫物件
- BLOB及CLOB欄位處理
- 4. mysql資料庫操縱常用命令MySql資料庫
- 使用ADO.NET輕鬆操縱資料庫(一)資料庫
- 使用ADO.NET輕鬆操縱資料庫(二)資料庫
- C# 操縱貼上板 Clipboard(傳送資料、讀取資料、清空資料)C#
- 通過配置hibernate方言包+GBase8s jdbc實現clob、blob、text、byte大物件資料型別操作JDBC物件資料型別
- 研究:比特幣揭露央行貨幣操縱和資本管制比特幣
- 分析:操縱的藝術
- MySQL教程DML資料操縱語言示例詳解鍵塾MySql
- 用SQLLOAD匯入CLOB資料SQL
- BLOB和CLOB的區別以及在ORALCE中的插入和查詢操作
- JDBC處理包含CLOB欄位JDBC
- java jdbc存取oracle clob型別JavaJDBCOracle型別
- Oracle 中LONG RAW BLOB CLOB型別介紹Oracle型別
- 第一篇:操縱MySQL資料庫(1) - 基於MySQLdb庫MySql資料庫
- JavaScript也能操縱cookie函式JavaScriptCookie函式
- cmd 命令操縱檔案管理器、建立(刪除)多級資料夾
- (資料科學學習手札125)在Python中操縱json資料的最佳方式資料科學PythonJSON
- 線上社交網路中的操縱武器
- 第二篇:操縱MySQL資料庫(2) - 基於ORM思想的SQLAlchemy庫MySql資料庫ORM
- 數億WIFI晶片存在資料竊取和流量操縱風險;因存在安全漏洞,沃爾沃研發資料被盜WiFi晶片
- 巧用xmltype解析clob資料XML
- JavaWeb——JDBC八股文、JSBC使用儲存過程、儲存函式、處理CLOB/BLOB型別JavaWebJDBCJS儲存過程儲存函式型別
- MySQL 4.1.0 中文參考手冊 --- 6.4 資料操縱:SELECT, INSERT, UPDATE, DELETE (轉)MySqldelete
- Runtime.getRuntime.exec操縱其他程式
- MySQL 中 blob 和 text 資料型別詳解MySql資料型別
- 關於SQL Server通過OLEDB訪問ORACLE資料表涉及CLOB或BLOB欄位的錯誤提示SQLServerOracle
- 影片操縱中的新AI技術轉向AI
- 用JDBC訪問一個資料庫JDBC資料庫