java oracle blob字段的插入

29 篇文章 0 订阅
/* 插入BLOB对象 */
public static String reqInfo(String qry,String cxfaid,String fwfid,String pf,String xtestflag){
Connection con=null;
Statement stat=null;
String send="qry="+qry+"&cxfaid="+cxfaid+"&fwfid="+fwfid+"&pf="+pf+"&xtestflag="+xtestflag;
System.out.println("zwreq ReqDao.class send= "+send);
String id=getGuid();
String sql="insert into mutual(id,Send,Receive) values ('"+id+"',EMPTY_BLOB(),EMPTY_BLOB())";
System.out.println("sql= "+sql);
String result="false";
try {
con=ConnManager.getConnection();
stat=con.createStatement();
int count = stat.executeUpdate(sql);
if (count > 0) {
blobModify(con, stat, send,
"send", "mutual", id,
"id");
}
} catch (Exception e) {
// TODO Auto-generated catch block
result="ReqDao.reqInfo 错误:"+e.getMessage();
e.printStackTrace();
} finally {
try {
if (stat != null) {
stat.close();
}
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
}
}
return result;
}
/* 修改BLOB对象(是在原CLOB对象基础上进行覆盖式的修改) */
public static void blobModify(Connection con, Statement stmt,
String infile, String blobColumnName, String tabName, String id,
String idColumName) throws SQLException {
byte[] b=infile.getBytes();
/* 设定不自动提交 */
boolean defaultCommit = con.getAutoCommit();
con.setAutoCommit(false);
try {
/* 查询CLOB对象并锁定 */
String sql = "SELECT " + blobColumnName + " FROM " + tabName
+ " WHERE " + idColumName + "='" + id + "' FOR UPDATE";
// System.out.println("clobModify String sql= "+sql);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
/* 获取此CLOB对象 */
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs
.getBlob(blobColumnName);
// System.out.println(clob);
// System.out.println("infile= "+infile);
OutputStream blobWriter = blob.getBinaryOutputStream();
blobWriter.write(b,0,b.length);
blobWriter.flush();
blobWriter.close();
}
/* 正式提交 */
con.commit();
} catch (Exception ex) {
/* 出错回滚 */
con.rollback();
ex.printStackTrace();
}
/* 恢复原提交状态 */
con.setAutoCommit(defaultCommit);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java连接Oracle数据库并处理BLOB数据,可以采用以下步骤: 1. 导入Oracle JDBC驱动程序。您可以从Oracle官方网站上下载适用于您的Oracle版本的JDBC驱动程序。 2. 使用JDBC API连接Oracle数据库。 3. 使用SELECT语句查询包含BLOB数据的表,并指定需要查询的BLOB列。 4. 在查询结果中,获取BLOB列的引用或句柄。 5. 使用JDBC API读取BLOB数据并将其转换为所需的格式。 6. 如果需要将BLOB数据保存到本地文件系统,可以使用Java IO流将BLOB数据写入本地文件。 以下是一个使用Java读取BLOB数据的例子: ``` import java.io.FileOutputStream; import java.io.InputStream; import java.sql.*; public class ReadBlobExample { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { // 导入Oracle JDBC驱动程序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 使用JDBC API连接Oracle数据库 conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "username", "password"); // 使用SELECT语句查询包含BLOB数据的表 pstmt = conn.prepareStatement("SELECT blob_column FROM table_name WHERE id = ?"); pstmt.setInt(1, 1); // 执行查询操作 rs = pstmt.executeQuery(); // 获取查询结果中的BLOB列 if (rs.next()) { Blob blob = rs.getBlob("blob_column"); // 获取BLOB数据的输入流 InputStream in = blob.getBinaryStream(); // 创建输出流,将BLOB数据写入本地文件 FileOutputStream out = new FileOutputStream("file_name"); byte[] buffer = new byte[1024]; int len = -1; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); in.close(); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭数据库连接和相关资源 try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 此代码示例使用Java JDBC API连接Oracle数据库,读取BLOB数据并将其写入本地文件系统。在实际应用中,您需要根据具体需求进行适当的更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值