html - Displaying image in jsp from database -


this question has answer here:

for code below, googled lots, somehow couldn't figure out error.. respect "img src" tag.

there might silly mistake, great if me out.

i have saved images inside image folder, placed inside project folder... database has image url under "image" attribute... trying retrieve url database through <%=rs.getstring("image") %> 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 * tablename id = 1"); } catch (exception e) {     out.println("db problem");      return; } {     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 connection before recieving image change 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 * tablename 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; } { try { rs.close(); stmt.close(); con.close(); } catch (sqlexception e) { e.printstacktrace(); } } %> </body> </html> 

or else better way save in 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 * tablename id = 1");  while(rs.next()){  image = rs.getstring("image");  } } catch (exception e) { out.println("db problem");  return; } { 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> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -