unable to retrieve other information on successful retrievation of blob from database using jsp?

故事扮演 提交于 2019-12-12 04:13:01

问题


I have successfully inserted details of a student and his image through .jsp program in to sql database. The problem is when I retrieve back the stored information of the student through another .jsp program only image is displaying, no other information of the student.

Can any one help me with the codes to retrieve stored blob image and other information and display together?

I am using oracle database 11g.

It has student table named studdetail. it has five columns, column 1 to column 4 are varchar2 and fifth column is blob. I have successfully able to insert values through html and jsp program into the database. But the problem arises when I retrieve back the information. It only displays the image but no other information.

Retrieve jsp code: show.jsp

<%@page import="java.io.OutputStream"%>
<%@page import="java.sql.Blob"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
       try{
                Class.forName("oracle.jdbc.OracleDriver");
                Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:db","xxxx","apple");



                PreparedStatement ps=con.prepareStatement("select * from studdetail");
                ResultSet rs=ps.executeQuery();
                while(rs.next()){ %>
                <table><tr><th>student id:</th><td><%=rs.getString(1) %></td></tr> 
                    <tr><th>student name:</th><td><%=rs.getString(2) %></td></tr>
                    <tr><th>student branch code</th><td><%=rs.getString(3) %></td></tr>
                    <tr><th>student contact number</th><td><%=rs.getString(4) %></td></tr>
                    <tr><th>students image</th><td>
                <%
                    Blob bl=rs.getBlob(5);
                    byte[] image=bl.getBytes(1, (int)bl.length());
                    response.setContentType("image/bmp");
                    OutputStream o = response.getOutputStream();
                    o.write(image);
                    o.flush();
                    o.close();
             }
                %></td></tr>
                </table> 
                    <%
               con.close();
           }catch(Exception e){
          out.print(e);
          }

     %>
    </body>
</html>

回答1:


You have missed the img tag in your jsp to render the image.

<%@page import="java.io.OutputStream"%>
<%@page import="java.sql.Blob"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
       try{
                Class.forName("oracle.jdbc.OracleDriver");
                Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:db","xxxx","apple");



                PreparedStatement ps=con.prepareStatement("select * from studdetail");
                ResultSet rs=ps.executeQuery();
                while(rs.next()){ %>
                <table><tr><th>student id:</th><td><%=rs.getString(1) %></td></tr> 
                    <tr><th>student name:</th><td><%=rs.getString(2) %></td></tr>
                    <tr><th>student branch code</th><td><%=rs.getString(3) %></td></tr>
                    <tr><th>student contact number</th><td><%=rs.getString(4) %></td></tr>
                    <tr><th>students image</th><td>**<img src="
                <%
                    Blob bl=rs.getBlob(5);
                    byte[] image=bl.getBytes(1, (int)bl.length());
                    response.setContentType("image/bmp");
                    OutputStream o = response.getOutputStream();
                    o.write(image);
                    o.flush();
                    o.close();
             }
                %>"></img></td></tr>
                </table> 
                    <%
               con.close();
           }catch(Exception e){
          out.print(e);
          }

     %>
    </body>
</html>

Whatever you are doing is a crude way to retrieve images from DB and display in a web page. There are better ways to do it. I would have specified the source of my image attribute to a servlet that would have retrieved the image blob from DB.



来源:https://stackoverflow.com/questions/22802040/unable-to-retrieve-other-information-on-successful-retrievation-of-blob-from-dat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!