oracle中的blob类型,支持大型数据,可以存储最大4g的文件。
在java中blob类型为java.sql.Blob,oracle为oracle.sql.BLOB。
我们可以通过ResultSet的getBlob()方法获取blob类型的数据。
下面演示一遍存储图片的过程,先创建一个存储图片的表:
create table T_IMAGE
(
ID VARCHAR2(4),
IMAGE BLOB
)
DatabaseUtil用来连接数据库
package com.shizhan.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseUtil {
static Connection connection = null;
static String driver = "oracle.jdbc.driver.OracleDriver";
static String url = "jdbc:oracle:thin:localhost:1521:orcl";
static String username ="scott";
static String password ="tiger";
public static Connection getConnection() throws Exception
{
Class.forName(driver);
connection = DriverManager.getConnection(url,username,password);
return connection;
}
public static void close(Connection conn) {//关闭连接对象
if(conn != null) { //如果conn连接对象不为空
try {
conn.close(); //关闭conn连接对象对象
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement pstmt) {//关闭预处理对象
if(pstmt != null) { //如果pstmt预处理对象不为空
try {
pstmt.close(); //关闭pstmt预处理对象
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement pstmt) {//关闭预处理对象
if(pstmt != null) { //如果pstmt预处理对象不为空
try {
pstmt.close(); //关闭pstmt预处理对象
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs) {//关闭结果集对象
if(rs != null) { //如果rs结果集对象不为null
try {
rs.close(); //关闭rs结果集对象
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
实际存储和读取图片的java类
package com.shizhan.main;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.shizhan.util.DatabaseUtil;
public class TestBlob {
private static Connection conn = null;
private Statement pstmt = null;
private ResultSet rs = null;
/**
* 向数据库中插入图片
* @throws Exception
*/
public void inputImage() {
try {
conn = DatabaseUtil.getConnection();
pstmt = conn.createStatement();
conn.setAutoCommit(false);// 取消自动提交功能
OutputStream os = null;
// 插入一个空对象empty_blob()
pstmt.executeUpdate("insert into t_image (id, image) values (2, empty_blob())");
// 锁定数据行进行更新,注意"for update"语句
rs = pstmt.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:\\a.jpg");
// 依次读取流字节,并输出到已定义好的数据库字段中.
int i = 0;
while ((i = is.read()) != -1) {
os.write(i);
}
}
os.flush();
os.close();
conn.commit();
conn.setAutoCommit(true);// 恢复现场
}
catch (Exception e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs);
DatabaseUtil.close(pstmt);
DatabaseUtil.close(conn);
}
}
/**
* 从数据库里检索出图片
*/
public void outputImage() {
try {
String sql = "select image from t_image where id=2";
conn = DatabaseUtil.getConnection();
pstmt = conn.createStatement();
rs = pstmt.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 (Exception e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs);
DatabaseUtil.close(pstmt);
DatabaseUtil.close(conn);
}
}
public static void main(String[] args) {
// 从硬盘提取图片插入到数据库中
new TestBlob().inputImage();
//从数据库中读取图片
new TestBlob().outputImage();
}
}