【问题标题】:Download a BLOB file from database从数据库下载 BLOB 文件
【发布时间】:2016-12-30 01:12:54
【问题描述】:

我需要创建一个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) {


}
});

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

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 文件?我还需要创建另一个语句吗?

提前致谢!

【问题讨论】:

    标签: java database swing download jbutton


    【解决方案1】:

    您可以使用此代码(但我目前无法尝试)。 query是查询,indexSELECT子句中的索引列,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();
    

    同样,我目前无法尝试此代码。在确保它按您想要的方式工作之前对其进行测试。

    【讨论】:

    • 如何将它添加到 ActionListner?
    • 你把代码放在actioPerformed(ActionEvent)实现中。
    【解决方案2】:

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

    • 实现回调以通知您的 blob 已加载
    • 实现执行 SQL 操作的线程(加载 blob)
    • 创建 ActionListener,它从 prev 开始线程。步骤

    这是一个例子:

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

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 2016-09-02
      • 2011-07-24
      • 2023-03-21
      • 2017-01-06
      • 1970-01-01
      • 2017-08-20
      • 2013-03-20
      相关资源
      最近更新 更多