1

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.

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>
Sal00m
  • 2,938
  • 3
  • 22
  • 33
user3781916
  • 19
  • 1
  • 2
  • 7

1 Answers1

1

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>
Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52