ORACLE的BLOB存取

本文提供了一个使用Java操作Oracle数据库中BLOB类型的示例,包括如何将文件写入数据库及从数据库读取文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ORACLE的BLOB存取- -
Tag: BLOB存取

就一个例子,周日在一个书店看书,就参照写个例子,呵呵

/*
* Created on 2005-7-11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.yaowj.database;

import java.sql.*;

import javax.sql.*;
import java.io.*;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.BLOB;
import com.yaowj.database.DBbean;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

/**
* @author ywj
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/

public class BlobBeanTest {

static Logger logger = Logger.getLogger(BlobBean.class.getName());
Connection conn = null;
public BlobBeanTest(){
}
/**
*
* @author ywj
* 写入Blob到数据库
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public boolean addBlob(String fileName){
boolean flag = true;
PreparedStatement pStmt=null;
ResultSet rs=null;
BLOB blob = null;
try{
conn=getConnection();
conn.setAutoCommit(false);
//logger.info(" the con is "+con);
pStmt=conn.prepareStatement("insert into blobtable values(empty_blob())");
System.out.println("000000000000000");
pStmt.executeUpdate();
pStmt=conn.prepareStatement("select blobvalue from blobtable for update");
System.out.println("111111111111111");
rs=pStmt.executeQuery();
while(rs.next()){
blob = ((OracleResultSet)rs).getBLOB(1);
//logger.info(" blob.length() is "+blob.length());
}
System.out.println("222222222222222");
File binaryFile = new File(fileName);
//logger.info(fileName+"'s length = "+binaryFile.length());
FileInputStream instream = new FileInputStream(binaryFile);
OutputStream outstream = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
//logger.info("chunk size is "+chunk);
byte[] buffer = new byte[chunk];
int length = -1;
while((length=instream.read(buffer))!=-1)
outstream.write(buffer,0,length);
instream.close();
outstream.close();
conn.commit();
}catch(SQLException e){
e.printStackTrace();
//logger.error(" SQLException "+e);
flag = false;
}catch(FileNotFoundException e){
e.printStackTrace();
//logger.error(" FileNotFoundException "+e);
flag = false;
}catch(IOException e){
e.printStackTrace();
//logger.error(" IOException "+e);
flag = false;
}finally{
try{
rs.close();
pStmt.close();
conn.close();
}catch(Exception e){e.printStackTrace();}
}
return flag;
}
/**
*
* @author ywj
* 从数据库读取Blob数据至文件
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public boolean readBlob(String fileName){
java.util.Date d1 = new java.util.Date();
System.out.println("********"+d1);
boolean flag = true;
PreparedStatement pStmt=null;
ResultSet rs=null;
BLOB blob = null;
try{
conn=getConnection();
conn.setAutoCommit(false);
//logger.info(" the con is "+con);
pStmt=conn.prepareStatement("select blobvalue from blobtable");
rs=pStmt.executeQuery();
while(rs.next()){
blob = ((OracleResultSet)rs).getBLOB(1);
//logger.info(" blob.length() is "+blob.length());
}
FileOutputStream file_out = new FileOutputStream(new File(fileName));
//InputStream blob_in = blob.getBinaryStream();
BufferedInputStream blob_in = new BufferedInputStream(blob.getBinaryStream());
int temp;
while((temp=blob_in.read())!=-1)
file_out.write(temp);
file_out.close();
blob_in.close();
conn.commit();
}catch(SQLException e){
e.printStackTrace();
//logger.error(" SQLException "+e);
flag = false;
}catch(FileNotFoundException e){
e.printStackTrace();
//logger.error(" FileNotFoundException "+e);
flag = false;
}catch(IOException e){
e.printStackTrace();
//logger.error(" IOException "+e);
flag = false;
}finally{
try{
rs.close();
pStmt.close();
conn.close();
}catch(Exception e){e.printStackTrace();}
}
java.util.Date d2 = new java.util.Date();
System.out.println("********"+d2);
return flag;
}


/**

这里取得数据库链接,oracle,其他数据库请做相应修改

*/
public Connection getConnection(){
Connection conn = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:lsscc";
String name = "bbs";
String password = "bbsadmin";
conn = DriverManager.getConnection(url,name,password);
System.out.println("the conn is "+conn);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}


/**

测试

*/
public static void main(String[] args){
BlobBeanTest bbt = new BlobBeanTest();
//这里请写文件的存放路径
boolean b1 = bbt.addBlob("C:\\test\\music.mp3");
boolean b2 = bbt.readBlob("C:\\test\\music1.mp3");
System.out.println(b1);
System.out.println(b2);
}

}

- 作者: wolfzha 访问统计:710 2005年07月11日, 星期一 17:53 加入博采
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值