
案例十、从数据库中读取面片 i国ort java.8l.*料 inport java.1o.* public class GetBlob{ public static void min(String[]args) Preparedstatenent stat null: ResultSet rs null: Connection conn null: FileOutputStream fos null: try( Class.forNane(oracle.jdbe.driver.OracleDriver): String url-"jdbc:oracle:thin:@localhost:1521 :nitpro"; conn DriverManager.getConnection (url."system',"manager"): String sql="select fron Student_List where Student_ID='id0o1'": stat conn.prepareStatement (sgl): rs stnt.executeQuery () File file new File("C:/tmp/rgb.jpg") fos new FileOutputStrean(file): rs.nextO: InputStrean is rs.getBinaryStrean(3): int len 0: byte b[]nev byte[4*1024]: while((len-is.read(b))!=-1) fos,write(b,0.len) fos.flush(); is.close0:
案例十、从数据库中读取图片 import java.sql.*; import java.io.*; public class GetBlob{ public static void main(String[] args){ PreparedStatement stmt = null; ResultSet rs = null; Connection conn = null; FileOutputStream fos = null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); String url= "jdbc:oracle:thin:@localhost:1521:nitpro"; conn = DriverManager.getConnection(url,"system","manager"); String sql="select * from Student_List where Student_ID='id001'"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); File file = new File("C:/tmp/rgb.jpg"); fos = new FileOutputStream(file); rs.next(); InputStream is = rs.getBinaryStream(3); int len = 0; byte b[] = new byte[4*1024]; while((len=is.read(b))!=-1) { fos.write(b,0,len); } fos.flush(); is.close();

stat.close () fas.close(); Icatch (SQLExeeption e)[ //System err.println(e); e.printStackTrace(): catch(I0Exception el) [ el.printstackTrace(: catch(ClassNotFoundExceptioa cfe) I cfe.printstackTrace(: 1 finally if (stmt!=null) try stat,close ( catch(SQLException ea) if (rs!=mull) try
stmt.close(); fos.close(); }catch(SQLException e){ //System.err.println(e); e.printStackTrace(); } catch(IOException e1) { e1.printStackTrace(); } catch(ClassNotFoundException cfe) { cfe.printStackTrace(); } finally { if (stmt!=null) { try { stmt.close(); } catch(SQLException ea) { } } if (rs!=null) { try {

rs.close(); 1 catch(Exception ea) 1
rs.close(); } catch(Exception ea) { }