java操作blob

原文地址: http://eric-gcm.iteye.com/blog/937860
package com.allan;
import java.sql.*;
import java.io.*;
public class Storeblobfile {


  public static void main(String[] args) {
    try{
      FileInputStream file = new FileInputStream("C:\\shanshui.jpg");
      Class.forName("com.mysql.jdbc.Driver");
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root");
      PreparedStatement ps = conn.prepareStatement("insert into user values(?,?,?)");
      ps.setString(1,"blob");
      ps.setInt(2,23);
      ps.setBinaryStream(3, file, file.available());
      ps.executeUpdate();
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery("select file from user where name = 'blob'");
      while(rs.next()){
      Blob blob = rs.getBlob(1);
      InputStream in = blob.getBinaryStream();
      FileOutputStream fout = new FileOutputStream("C:\\copy.jpg");
      int b = -1;
      while((b=in.read())!=-1){
            fout.write(b);
       }
     }
    }catch(Exception e){
        System.out.println(e.getMessage());
    }
 }

}

//如果有一blob类型的列“content”,要将content中的数据取出来放到String中:

Blob blob = rs.getBlob("content");
int bolblen = (int) blob.length();
byte[] data = blob.getBytes(1, bolblen);
String content = new String(data);

  • 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、付费专栏及课程。

余额充值