Download a BLOB file from database

喜欢而已 提交于 2021-02-08 10:17:10

问题


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

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

Thanks in advance!


回答1:


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.




回答2:


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

  • 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

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


来源:https://stackoverflow.com/questions/16709652/download-a-blob-file-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!