对于下面的代码,我用谷歌搜索了很多,但是以某种方式我无法弄清楚该错误..关于“ img src”标签。
可能有愚蠢的错误,但是如果有人可以帮助我,那将是很棒的。
我已经将我的图像保存在图像文件夹中,该图像文件夹位于项目文件夹中…数据库在“图像”属性下具有图像URL …所以我尝试通过数据库从数据库中检索URL <%=rs.getString("image") %>是正确的吗?
<%=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>