java对Blob读写

package com.you.sister;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class BlobTest {

	public static Connection conn;
	
	public static Connection getConn() throws Exception {
		FileInputStream fis = new FileInputStream(new File("jdbc.properties"));
		Properties prop = new Properties();
		prop.load(fis);
		String driver = prop.getProperty("jdbc.driver");
		String url = prop.getProperty("jdbc.url");
		String username = prop.getProperty("jdbc.username");
		String password = prop.getProperty("jdbc.password");
		Class.forName(driver);
		return  DriverManager.getConnection(url, username, password);
	}
	
	public static void main(String[] args) throws Exception {
		conn = getConn();
		readBlob();
		writeBlob();
		conn.close();
	}
	
	/**
	 * 从数据库中读大对象出来
	 * 保存在本地
	 */
	public static void readBlob() {
		try {
			String readSql = "select * from emp where empno = ?";
			PreparedStatement ps = conn.prepareStatement(readSql);
			ps.setInt(1, 7369);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Blob image = rs.getBlob("image");
				DataOutputStream dos = 
					// 在FileOutputStream中指定文件输出路径
					new DataOutputStream(new FileOutputStream(7369 + "_image.jpeg"));
				InputStream fis = image.getBinaryStream();
				int out;
				byte[] outByte = new byte [100];
				// 将blob对象输入流写入本地输出流中
				while ((out = fis.read(outByte)) != -1) {
					dos.write(outByte);
				}
				fis.close();
				dos.flush();
				dos.close();
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 将大对象文件保存进数据库中
	 */
	public static void writeBlob() {
		try {
			BufferedInputStream fis = 
				new BufferedInputStream(new FileInputStream(new File("D:\\Tulips.jpg")));
			// 如果是新插入字段,则将大对象对应字段插入为empty_clob();
			// 如果是修改,则可以先update 该行数据,将大对象对应字段设置为empty_clob();
			String writeSql = "select * from emp where empno = ? for update";
			PreparedStatement ps = conn.prepareStatement(writeSql);
			ps.setInt(1, 7499);
			conn.setAutoCommit(false);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				oracle.sql.BLOB image = (oracle.sql.BLOB)rs.getBlob("image");
				BufferedOutputStream bos = new BufferedOutputStream(image.getBinaryOutputStream());
				int c;
				// 将实际文件中的内容以二进制的形式来输出到blob对象对应的输出流中
				while ((c = fis.read()) != -1) {
					bos.write(c);
				}
				fis.close();
				bos.close();
			}
			conn.commit();
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


从数据库中读blob对象出来相对比较容易

从本地向数据库写blob对象相对难一些,主要注意两点:

1  要处理的blob字段必须先设置为empt_clob()

2  使用事务控制向数据库的写入操作

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
使用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数据并将其写入本地文件系统。在实际应用中,您需要根据具体需求进行适当的更改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值