日期:2014-05-16  浏览次数:20368 次

利用Java向Oracle中插入图片(BLOB)文件

我介绍的是用JDBC的方法进行操作,想用Java来操作Oracle必须(我不知道有没有其他方法)要有classes12.jar(zap),这个东西在网上搜,一搜一大把,如果上不了网,那就去Oracle的安装目录去找吧,举个例子,我的Oracle是10G,classes12.jar的藏身之处就在这里:oracle\product\10.2.0\db_1\jdbc\lib.费话少说,现在来晒晒我的代码.

?

哎?稍等...在晒代码之前还有一个准备工作,那就是建立数据库表啊,呵呵.

建立表和索引(索引有无均可)的脚本如下:

-- Create table
create table T_IMAGE
(
  ID    VARCHAR2(4),
  IMAGE BLOB
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate indexes 
create unique index PK_IMAGE on T_IMAGE (ID)
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
?

?

package com.neusoft.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.tomcat.dbcp.dbcp.ConnectionFactory;

public class TestImage {

	private static Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;

	static {
		try {
			// 加载Oracle驱动
			Class.forName("oracle.jdbc.driver.OracleDriver");
			// 获得连接
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@172.16.225.170:1521:orcl", "scott",
					"tiger");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭所有与数据库相关的连接
	 * 
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public void closeAll(ResultSet rs, Statement stmt, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 向数据库中插入图片
	 */
	public void inputImage() {
		try {
			stmt = conn.createStatement();
			conn.setAutoCommit(false);// 取消自动提交功能
			OutputStream os = null;
			// 插入一个空对象empty_blob()
			stmt.executeUpdate("insert into t_image (id, image) values (2, empty_blob())");
			// 锁定数据行进行更新,注意"for update"语句
			rs = stmt.executeQuery("select image from t_image where id=2 for update");
			if (rs.next()) {
				// 得到java.sql.Blob对象后强制转换为oracle.sql.BLOB
				oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("image");
				// 通过getBinaryOutputStream()方法获得向数据库中插入图片的"管道"
				os = blob.getBinaryOutputStream();
				// 读取想要存储的图片文件
				InputStream is = new FileInputStream("E:\\新建文件夹\\6Q52OO3R00DE0005.jpg");
				// 依次读取流字节,并输出到已定义好的数据库字段中.
				int i = 0;
				while ((i = is.read()) != -1) {
					os.write(i);
				}
			}
			os.flush();
			os.close();
			conn.commit();
			conn.setAutoCommit(true);// 恢复现场
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭相应数据库连接
			closeAll(rs, stmt, conn);
		}
	}

	/**
	 * 从数据库里检索出图片
	 */
	public void outputImage() {
		try {
			String sql = "select image from t_image where id=1";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				oracle.sql.BLOB b = (oracle.sql.BLOB) rs.getBlob(1);
				InputStream is = b.getBinaryStream();
				FileOutputStream fos = new FileOutputStream("E:\\outputImage.jpg");
				int i = 0;
				while ((i = is.read()) != -1) {
					fos.write(i);
				}
				fos.flush();
				fos.close();
				is.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs, stmt, conn);
		}
	}
	
	public static void main(String[] args) {
		// 从硬盘提取图片插入到数据库中
		// new TestImage().inputImage();
		// 从数据库中检索图片到硬盘