unable to retrieve other information on successful

2019-08-31 06:12发布

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>

标签: sql jsp blob
1条回答
聊天终结者
2楼-- · 2019-08-31 06:56

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.

查看更多
登录 后发表回答