无法使用JSP从数据库中检索到有关成功检索Blob的其他信息? [英] unable to retrieve other information on successful retrievation of blob from database using jsp?

查看:75
本文介绍了无法使用JSP从数据库中检索到有关成功检索Blob的其他信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过.jsp程序成功地将一个学生的详细信息及其图像插入到sql数据库中.问题是,当我通过另一个.jsp程序检索学生的存储信息时,仅显示图像,而没有其他学生的信息.

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?

我正在使用oracle数据库11g.

I am using oracle database 11g.

它有一个名为studdetail的学生表.它有五列,第1列到第4列是varchar2,第五列是blob.我已经能够通过html和jsp程序成功将值插入数据库.但是,当我取回信息时,就会出现问题.它仅显示图像,而没有其他信息.

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.

检索jsp代码: show.jsp

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>

推荐答案

您错过了jsp中的img标签来渲染图像.

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>

无论您做什么,都是从DB检索图像并将其显示在网页中的一种粗略方法.有更好的方法可以做到这一点.我将把我的图像属性的源指定给一个从数据库检索图像blob的servlet.

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.

这篇关于无法使用JSP从数据库中检索到有关成功检索Blob的其他信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆