在数据库中显示jsp中的图像

时间:2014-06-27 06:19:07

标签: html database jsp

对于下面的代码,我搜索了很多,但不知怎的,我无法弄清楚错误..关于" img src"标签。

可能存在愚蠢的错误,但如果有人可以帮助我,那就太棒了。

我已将图像保存在图像文件夹中,图像文件夹位于项目文件夹中... 数据库的图像网址在&#34;图像&#34;属性...所以我试图通过<%=rs.getString("image") %>从数据库中检索网址是正确的吗?

<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>

1 个答案:

答案 0 :(得分:1)

您在接收图像之前已关闭连接 将您的代码更改为

 <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>

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

 <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>