从数据库中以jsp显示图像
对于下面的代码,我用谷歌搜索了很多,但是以某种方式我无法弄清楚该错误..关于“ img src”标签。
可能有愚蠢的错误,但是如果有人可以帮助我,那将是很棒的。
我已经将我的图像保存在图像文件夹中,该图像文件夹位于项目文件夹中…数据库在“图像”属性下具有图像URL …所以我尝试通过数据库从数据库中检索URL
<%=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>
-
接收图像之前,您已经关闭了连接。将代码更改为
<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>
-
如何在JSP页面中从数据库检索和显示图像?
2021-02-02 关注 0 浏览286 1答案
-
从数据库检索图像,并使用JSTL在JSP中显示它。
2021-02-01 关注 0 浏览104 1答案
-
如何从数据库检索图像并通过Servlet在JSP中显示?
2021-02-01 关注 0 浏览154 1答案
-
从数据库中将图像写入一个jsp
2021-02-02 关注 0 浏览84 1答案
-
使用spring MVC从数据库显示图像
2021-02-02 关注 0 浏览92 1答案
-
如何使用Spring MVC从MySQL数据库显示图像
2021-02-02 关注 0 浏览95 1答案
-
如何从数据库检索图像并将其放置在JSP上?
2021-02-01 关注 0 浏览102 1答案
-
如何从MySQL数据库检索图像并显示在html标签中
2021-02-02 关注 0 浏览97 1答案
-
如何在一个jsp页面上显示来自mysql数据库的图像?
2021-02-01 关注 0 浏览106 1答案
-
在ASP MVC中显示数据库中的图像
2021-02-02 关注 0 浏览76 1答案