Blob Type으로 저장된 DB 이미지를 JSP로 보여주는 비기를 공개한다.
<%@ page language="java" contentType="image/jpeg" %> <%@ page import="java.util.*, java.sql.*, java.io.*" %> <% String sql = ""; Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; OutputStream output = response.getOutputStream(); InputStream input = null; try { con = DriverManager.getConnection("Jdbc:oracle:thin:@서버주소:PORT:SID","USER","PASSWORD"); sql =" select imagedata from imageTable where id = '00000' "; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); if (rs.next()) { input = rs.getBinaryStream("imagedata"); int byteRead; while((byteRead = input.read()) != -1) { output.write(byteRead); } input.close(); } } catch(Exception e) { out.print(e); } finally { try {if (rs != null) rs.close();} catch (Exception ex) {} try {if (pstmt != null) pstmt.close();} catch (Exception ex) {} try {if (con != null) con.close();} catch (Exception ex) {} } input.close(); output.flush(); output.close(); %> Tags: connection jdbc sql
|