从数据库下载BLOB文件 [英] Download a BLOB file from database

查看:50
本文介绍了从数据库下载BLOB文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个JButton以便从oracle数据库下载BLOB文件. 这是我的JButton代码:

I need to create a JButton to download a BLOB file from oracle database. This is my JButton code:

JButton btnsave = new JButton("Save");
btnsave.setBounds(478, 542, 120, 23);
getContentPane().add(btnsave);
btnsave.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {


}
});

该类已经连接到数据库,但这是我的代码的一部分:

This class is already connected to database, but here is part of my code:

Connection con;


String link="*******************************************";
       String u="user";
       String p="pw";
       Class.forName("oracle.jdbc.driver.OracleDriver");
       conn=DriverManager.getConnection(link,u,p);


Statement su=con.createStatement();

那么我该如何在我的JButton中下载带有ActionListener的blob文件?我还需要创建另一条语句吗?

So how can i download a blob file with an ActionListener in my JButton? I also need to create another statement?

提前谢谢!

推荐答案

您可以使用此代码(但目前无法尝试). query是查询,indexSELECT clausole中的索引列,而file是输出文件.

You can use this code (but I can't try at the moment). query is the query, index the index column in the SELECT clausole, and file is the output file.

// take the result of the query
ResultSet rs = su.executeQuery(query);
while(rs.next()) { // for each row
   // take the blob
   Blob blob = rs.getBlob(index);
   BufferedInputStream is = new BufferedInputStream(blob.getBinaryStream());
   FileOutputStream fos = new FileOutputStream(file);
   // you can set the size of the buffer
   byte[] buffer = new byte[2048];
   int r = 0;
   while((r = is.read(buffer))!=-1) {
      fos.write(buffer, 0, r);
   }
   fos.flush();
   fos.close();
   is.close();
   blob.free();
}
su.close();

同样,我目前无法尝试此代码.在确保它可以像您期望的那样工作之前对其进行测试.

Again, I can't try this code at the moment. Test it before be sure it works like you want.

这篇关于从数据库下载BLOB文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆