java 读取 blob

package blobtest;

import javax.swing.*;
import java.io.*;
import java.sql.*;
//import DriverData;

import oracle.jdbc.driver.*;
import oracle.sql.*;
//import oracle.ord.im.*;

// Package for JDBC classes
import java.sql.Connection;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.Blob;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import oracle.jdbc.pool.OracleDataSource;

public class ReadAudioFromDB {
Connection conn;
Statement stmt;

ResultSet rs;
int bufferSize;

public ReadAudioFromDB()

throws SQLException, ClassNotFoundException {

String connectString;
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:oci8:@";
conn = (OracleConnection)
DriverManager.getConnection(connectString, "blobtest",
"1234");

stmt = conn.createStatement();

}

public void readFromDB() {

String pathname, name;
int amount = 0;
InputStream in = null;
BufferedOutputStream out = null;

JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
pathname = chooser.getSelectedFile().getAbsolutePath();
name = chooser.getSelectedFile().getName();
chooser = null;
}
else {
System.out.println("没有找到文件!");

System.out.println("程序非正常结束...");
return;
}

try {
rs = stmt.executeQuery(
"select BlobFile from audio where cavern_no='285' and document_id=1");
if (rs.next()) {
Blob blob = rs.getBlob(1);
in = blob.getBinaryStream();
bufferSize = ( (oracle.sql.BLOB) blob).getBufferSize();

out = new BufferedOutputStream(new FileOutputStream(pathname),
(int) bufferSize);

byte[] b = new byte[ (int) bufferSize];
int count = in.read(b, 0, (int) bufferSize);

while (count != -1) {
out.write(b, 0, count);
amount += count;
System.out.println("已经处理了" + amount + "字节.");
count = in.read(b, 0, (int) bufferSize);
}
System.out.println("成功处理完毕字节数:" + amount);
out.close();
out = null;
in.close();
in = null;

}
else {
System.out.println("文档:" + name + "未找到.");
}

}

catch (Exception e) {
e.printStackTrace();
}

finally {
if (out != null) {
try {
out.close();
}
catch (Exception ignored) {}
}
if (in != null) {
try {
in.close();
}
catch (Exception ignored) {}
}
}

}

}

=======
package blobtest;

import javax.swing.*;
import java.io.*;
import java.sql.*;
//import DriverData;

import oracle.jdbc.driver.*;
import oracle.sql.*;
//import oracle.ord.im.*;

// Package for JDBC classes
import java.sql.Connection;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.Blob;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import oracle.jdbc.pool.OracleDataSource;

public class WriteAudioToDB {
Connection conn;
Statement stmt;

ResultSet rs;
int bufferSize;

public WriteAudioToDB() throws SQLException, ClassNotFoundException {

String connectString;
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:oci8:@";
conn = (OracleConnection)
DriverManager.getConnection(connectString, "blobtest",
"1234");

stmt = conn.createStatement();

}

public void sendToDb() {
String pathname, name;
int amount = 0;
OutputStream out = null;
BufferedInputStream in = null;

JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
pathname = chooser.getSelectedFile().getAbsolutePath();
name = chooser.getSelectedFile().getName();
chooser = null;
}
else {
System.out.println("没有找到文件!");

System.out.println("程序非正常结束...");
return;
}
try {
conn.setAutoCommit(false);
stmt.executeUpdate("insert into document(cavern_no,audio_id,TITLE ,

ARCHITECTURE_ATTR_VAL ,PAINTED_SCULPTURE_ATTR_VAL,

FRESCO_ATTR_VAL,FOKAN_ATTR_VAL,BLOBFILE )

values("+"'285',2,'"+name+"','1111','111101','10101','10001',empty_blob())");
rs = stmt.executeQuery(
"select BlobFile from document where cavern_no='285' and document_id=1");

if (rs.next()) {
Blob blob = rs.getBlob("BlobFile");
out = ( (oracle.sql.BLOB) blob).getBinaryOutputStream();
bufferSize = ( (oracle.sql.BLOB) blob).getBufferSize();
in = new BufferedInputStream(new FileInputStream(pathname), bufferSize);

byte[] b = new byte[bufferSize];
int count = in.read(b, 0, bufferSize);
while (count != -1) {
out.write(b, 0, count);
amount += count;
System.out.println("处理了" + amount + "字节.");
count = in.read(b, 0, bufferSize);

System.out.println("处理了" + amount + "字节,成功.");

}

out.close();
out = null;
in.close();
in = null;
conn.commit();

}

}

catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
}
catch (Exception ignored) {}
}
finally {
if (out!=null) try{out.close();} catch(Exception igored){}
if (in!=null) try{in.close();} catch(Exception igored){}
}


}

}
转自 http://junsheng.itpub.net/post/860/8559 csdn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值