找了也看了很多相关的文章,测试了n久,终于成功了,在室友面前小小的哼了下庾澄庆的我很快乐被说傻逼......
参考源:
http://bbs.csdn.net/topics/110131616
http://www.verydemo.com/demo_c158_i114.html
好吧,下面先建好数据库
数据库代码如下:
-- 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
);
select * from T_IMAGE for update;
java代码:
package com.neusoft.test;
import java.io.BufferedOutputStream;
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;
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:@localhost:1521:orcl", "system",
"system");
} 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();
//这里要特别注意,应该用BufferedOutputStream,不然图片会写如失败
BufferedOutputStream output = new BufferedOutputStream(os);
// 读取想要存储的图片文件
InputStream is = new FileInputStream("D:\\tp\\1.jpg");
// 依次读取流字节,并输出到已定义好的数据库字段中.D:\tp
int i = 0;
while ((i = is.read()) != -1) {
output.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=2";
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:\\1.jpg");
// BufferedOutputStream output = new BufferedOutputStream(fos);
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();
// 从数据库中检索图片到硬盘
new TestImage().outputImage();
}
}