2013-05-23 3 views
0

oracle 데이터베이스에서 BLOB 파일을 다운로드하려면 JButton을 작성해야합니다. 이 내 JButton 코드 :데이터베이스에서 BLOB 파일 다운로드

이 클래스는 이미 데이터베이스에 연결되어 있지만 여기 내 코드의 일부입니다
JButton btnsave = new JButton("Save"); 
btnsave.setBounds(478, 542, 120, 23); 
getContentPane().add(btnsave); 
btnsave.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) { 


} 
}); 

:

그래서 난에 ActionListener와 BLOB 파일을 다운로드 할 수있는 방법
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? 또 다른 성명서를 작성해야합니까?

미리 감사드립니다.

답변

3

이 코드를 사용할 수 있습니다 (하지만 지금은 시도 할 수 없습니다). query은 이라는 쿼리이며 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(); 

다시 말해서이 코드는 지금 시도 할 수 없습니다. 원하는대로 작동하는지 테스트하기 전에 테스트하십시오.

+0

어떻게 내가 ActionListner이를 추가 할 수 있습니다

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 } } 
? – StReeTzZz

+0

'actioPerformed (ActionEvent)'구현에 코드를 넣는다. – Alberto

0

ActionListener에서 긴 작업을 수행하지 않는 것이 좋습니다. 이 방법을 시도해보십시오

  • 가 당신의 덩어리가
  • 는 SQL 작업 (로드 BLOB)
  • 는 이전부터 스레드를 시작 ActionListener를 만들기를 수행 할 스레드를 구현로드 된 알림 콜백을 구현합니다. 여기

단계은 예입니다

관련 문제