티스토리 뷰


/*이전글에서 Java로 저당된 이미지 화일을 Jsp로 불러와 화면에 보여주는 루틴 */
<%@ 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:mysql://localhost/디비명","root","디비암호");
    sql =" select FILE from tbl_test where id = 5 ";
    pstmt = con.prepareStatement(sql);
    rs = pstmt.executeQuery();
    if (rs.next()) {
        input = rs.getBinaryStream("FILE");
        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();
%>