0

JButtonOracle データベースから 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();

ActionListenerでは、どのようにblob ファイルをダウンロードできますJButtonか? また、別のステートメントを作成する必要がありますか?

前もって感謝します!

4

2 に答える 2

4

このコードを使用できます (ただし、現時点では試すことができません)。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();

繰り返しますが、現時点ではこのコードを試すことはできません。希望どおりに動作することを確認する前にテストしてください。

于 2013-05-23T09:31:28.933 に答える
0

ActionListener で長い操作を実行することはお勧めしません。このアプローチを試してください:

  • BLOB が読み込まれたことを通知するためのコールバックを実装する
  • SQL 操作を実行するスレッドを実装する (blob の読み込み)
  • 前からスレッドを開始する ActionListener を作成します。ステップ

次に例を示します。

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 
    }
}
于 2013-05-23T09:33:56.767 に答える