从数据库中以jsp显示图像 [英] Displaying image in jsp from database

查看:98
本文介绍了从数据库中以jsp显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下面的代码,我用谷歌搜索了很多,但是以某种方式我无法弄清楚该错误..关于"img src"标签.

For the code below, i googled lots, but somehow i couldn't figure out the error.. with respect to "img src" tag.

可能存在愚蠢的错误,但是如果有人可以帮助我,那将是很棒的事情.

There might be silly mistake, but it would be great if someone could help me out.

我已将图像保存在图像文件夹中,该文件夹位于项目文件夹中... 数据库在图像"属性下具有图像网址...因此,我试图通过<%=rs.getString("image") %>从数据库中检索网址是否正确?

I have saved my images inside the image folder, which is placed inside the project folder... the database has the image url under "image" attribute... so i am trying to retrieve the url from the database through <%=rs.getString("image") %> is that correct ?

<html>
 <body>
   <%@ page import="java.io.*"%>
   <%@ page import="java.sql.*"%>
   <%@ page import="com.mysql.*"%>
   <%@ page import="java.util.*"%>
   <%@ page import="java.text.*"%>
   <%@ page import="javax.servlet.*"%>
   <%@ page import="javax.servlet.http.*"%>
   <%@ page import="javax.servlet.http.HttpSession"%>
   <%@ page language="java"%>
   <%@ page session="true"%>
   <%@ page import="java.sql.*"%>
<% 
Blob image = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String iurl1=null;

try {
    Class.forName("com.mysql.jdbc.Driver");
    con =           DriverManager.getConnection("jdbc:mysql://localhost:portnumber/dbname","","");
    stmt = con.createStatement();
    rs = stmt.executeQuery("select * from tablename where id = 1");
}
catch (Exception e) {
    out.println("DB problem"); 
    return;
}
finally {
    try {
        rs.close();
        stmt.close();
        con.close();
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
}
%>
    <table border="2">
     <tr><th>DISPLAYING IMAGE</th></tr>
     <tr><td>Image 2</td></tr>
     <tr>
       <td>
         <img src="<%=rs.getString("image") %>" width="500" height="500"/>
       </td>
     </tr>
    </table>

 </body>
</html>

推荐答案

在收到图像本身之前,您已经关闭了连接 将您的代码更改为

You have closed your connection before recieving the image itself Change your code to

 <html>
<body>
    <%@ page import="java.io.*"%>
 <%@ page import="java.sql.*"%>
 <%@ page import="com.mysql.*"%>
 <%@ page import="java.util.*"%>
  <%@ page import="java.text.*"%>
 <%@ page import="javax.servlet.*"%>
 <%@ page import="javax.servlet.http.*"%>
 <%@ page import="javax.servlet.http.HttpSession"%>
 <%@ page language="java"%>
 <%@ page session="true"%>
 <%@ page import="java.sql.*"%>
<% 
Blob image = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String iurl1=null;

try {
Class.forName("com.mysql.jdbc.Driver");
con =           DriverManager.getConnection("jdbc:mysql://localhost:portnumber/dbname","","");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from tablename where id = 1");%>
<table border="2">
<tr><th>DISPLAYING IMAGE</th></tr>
<tr><td>Image 2</td></tr>
<tr><td>
<%while(rs.next()){%>
 <img src="<%=rs.getString("image") %>" width="500" height="500"/>
 <%}%>
</td></tr>
</table>
<%}
catch (Exception e) {
out.println("DB problem"); 
return;
}
finally {
try {
rs.close();
stmt.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
%>
</body>
</html>

或者有更好的方法将其保存在变量中

Or else a better way save it in a variable

 <html>
<body>
    <%@ page import="java.io.*"%>
 <%@ page import="java.sql.*"%>
 <%@ page import="com.mysql.*"%>
 <%@ page import="java.util.*"%>
  <%@ page import="java.text.*"%>
 <%@ page import="javax.servlet.*"%>
 <%@ page import="javax.servlet.http.*"%>
 <%@ page import="javax.servlet.http.HttpSession"%>
 <%@ page language="java"%>
 <%@ page session="true"%>
 <%@ page import="java.sql.*"%>
<% 
Blob image = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String iurl1=null;
String image=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con =           DriverManager.getConnection("jdbc:mysql://localhost:portnumber/dbname","","");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from tablename where id = 1");
 while(rs.next()){
 image = rs.getString("image");
 }
}
catch (Exception e) {
out.println("DB problem"); 
return;
}
finally {
try {
rs.close();
stmt.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
%>
 <table border="2">
  <tr><th>DISPLAYING IMAGE</th></tr>
  <tr><td>Image 2</td></tr>
  <tr><td>
 <img src="<%=image %>" width="500" height="500"/>
 </td></tr>
 </table>

</body>
</html>

这篇关于从数据库中以jsp显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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