java 从数据库下载 BLOB 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16709652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:44:45  来源:igfitidea点击:

Download a BLOB file from database

javadatabaseswingdownloadjbutton

提问by StReeTzZz

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

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

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();

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

所以,我怎么可以下载与一个blob文件ActionListener在我的JButton?我还需要创建另一个语句吗?

Thanks in advance!

提前致谢!

回答by Alberto

You can use this code (but I can't try at the moment). queryis the query, indexthe index column in the SELECTclausole, and fileis the output file.

您可以使用此代码(但我目前无法尝试)。query是查询,index是条款中的索引列SELECTfile是输出文件。

// 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.

同样,我目前无法尝试此代码。在确保它像你想要的那样工作之前测试它。

回答by Eugene

It's not recommended to perform long operations in ActionListener. Try this approach:

不建议在 ActionListener 中执行长时间的操作。试试这个方法:

  • Implement callback for notification that your blob has been loaded
  • Implement thread which perform SQL operations(loading blob)
  • Create ActionListener which starts thread from prev. step
  • 实现回调以通知您的 blob 已加载
  • 实现执行 SQL 操作的线程(加载 blob)
  • 创建从上一个启动线程的 ActionListener。步

Here is an example:

下面是一个例子:

private JButton button = new JButton(new ClickListener());
private LoaderListener blobLoaderListener = new BlobLoaderListener();
private byte[] blob;
private Connection con; // connection code is ommited

private class ClickListener extends AbstractAction {
    @Override
    public void actionPerformed(ActionEvent e) {
        new SQLLoader(blobLoaderListener).start();
    }
}

private class SQLLoader extends Thread {
    private final LoaderListener listener;

    public SQLLoader(LoaderListener listener) {
        this.listener = listener;
    }

    public void run() {
        byte[] blob = null;
        try {
            // create another statement here 
            Statement su = con.createStatement();

            // perform you SQL query, get your 'blog'
            // once you have done, notify listener
            listener.onLoad(blob);
        } catch (Exception e) {
            // TODO: handle exception
            listener.onError();
        }
    }
}

private interface LoaderListener {
    void onLoad(byte[] blob);

    void onError();
}

private class BlobLoaderListener implements LoaderListener {
    @Override
    public void onLoad(byte[] blob) {
        BlobLoader.this.blob = blob;
        // notify UI about changes
    }

    @Override
    public void onError() {
        // TODO 
    }
}