?? writeblobtodb.java.bak
字號:
package jdbcblob;import javax.swing.*;import java.io.*;import java.sql.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class WriteBlobToDb { Connection conn; Statement stmt; ResultSet rs; int bufferSize; public WriteBlobToDb() throws SQLException, ClassNotFoundException { Class.forName("oracle.jdbc.driver.OracleDriver") ; String sourceURL="jdbc:oracle:thin:@localhost:1521:cnaloral"; String user="system"; String password="cnaloral8"; conn=DriverManager.getConnection(sourceURL,user,password) ; stmt=conn.createStatement() ; } public void sendToDb(){ String pathname,name; int amount=0; OutputStream out=null; BufferedInputStream in=null; JFileChooser chooser =new JFileChooser(); int returnVal=chooser.showOpenDialog(null); if (returnVal==JFileChooser.APPROVE_OPTION) { pathname=chooser.getSelectedFile() .getAbsolutePath(); name=chooser.getSelectedFile() .getName() ; chooser=null; } else { System.out.println("No file selected for write to db!"); System.out.println("Program terminating!"); return; }; try { conn.setAutoCommit(false) ; stmt.executeUpdate("insert into media values('"+name.trim()+"',empty_blob())") ; rs=stmt.executeQuery("select data from media where trim(name)='"+name+"' for update") ; if (rs.next()) { Blob blob=rs.getBlob(1); out=((oracle.sql.BLOB)blob).getBinaryOutputStream(); bufferSize=((oracle.sql.BLOB )blob).getBufferSize(); in=new BufferedInputStream(new FileInputStream(pathname),bufferSize); byte[] b=new byte[bufferSize]; int count=in.read(b,0,bufferSize); while (count!=-1) { out.write(b,0,count); amount+=count; System.out.println("Processed "+amount+" bytes."); count=in.read(b,0,bufferSize); }; System.out.println("Processed "+amount+"bytes.Finished!"); out.close(); out=null; in.close() ; in=null; conn.commit(); }; } catch (Exception ex) { ex.printStackTrace() ; try { conn.rollback();}catch (Exception ignored) {} } finally { if (out!=null) try{out.close() ;} catch(Exception ignored){}; if (in!=null) try{in.close() ;} catch(Exception ignored){}; }; } public static void main(String[] args) { try { WriteBlobToDb w = new WriteBlobToDb(); w.sendToDb(); } catch(Exception e) { e.printStackTrace(); } finally { System.exit(0); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -