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 加入博采
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 加入博采