oracle导出带有blob字段的数据,把图片从oracle数据库的blob字段中取出来,存在硬盘上...

import java.io.BufferedOutputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.sql.Blob;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.swing.JFileChooser;

import oracle.jdbc.driver.OracleConnection;

public class Read

{

public static void main(String[] args)

{

try {

Read rfdb = new Read ();

rfdb.readFromDB();

} catch (SQLException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

Connection conn;

Statement stmt;

ResultSet rs;

int bufferSize;

public Read ()throws SQLException, ClassNotFoundException

{

String connectString;

Class.forName("oracle.jdbc.driver.OracleDriver");

connectString = "jdbc:oracle:thin:@192.168.1.134:1521:zhpt";

conn = (OracleConnection)

DriverManager.getConnection(connectString, "dzjc","dzjc");

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 img from dzjc_img");

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) {}

}

}

}

}

上篇文章是从硬盘上存到数据库里,这次来个存到数据库里再取出来的存到硬盘上的类,还有一个是,下篇文章再发一个从数据库里取出来再显示在浏览器上的

贴代码先

import java.io.BufferedOutputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.sql.Blob;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.swing.JFileChooser;

import oracle.jdbc.driver.OracleConnection;

public class Read

{

public static void main(String[] args)

{

try {

Read rfdb = new Read ();

rfdb.readFromDB();

} catch (SQLException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

Connection conn;

Statement stmt;

ResultSet rs;

int bufferSize;

public Read ()throws SQLException, ClassNotFoundException

{

String connectString;

Class.forName("oracle.jdbc.driver.OracleDriver");

connectString = "jdbc:oracle:thin:@192.168.1.134:1521:zhpt";

conn = (OracleConnection)

DriverManager.getConnection(connectString, "dzjc","dzjc");

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 img from dzjc_img");

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) {}

}

}

}

}

要保证数据库里有图片,打开的对话框显示为打开而不是保存,没关系,自己找个地方,输个文件名,一点打开,图片就会被保存在那个位置了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值