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
Download a BLOB file from database
提问by StReeTzZz
I need to create a JButton
to download a BLOB file from oracle database.
This is my JButton
code:
我需要创建一个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 ActionListener
in 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). query
is the query, index
the index column in the SELECT
clausole, and file
is the output file.
您可以使用此代码(但我目前无法尝试)。query
是查询,index
是条款中的索引列SELECT
,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.
同样,我目前无法尝试此代码。在确保它像你想要的那样工作之前测试它。
回答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
}
}