i need help with my code in retrieving the image stored in the database.. what is the problem of this code? it only displays a blank page.. this is the code :

<%@page import = "java.sql.*"%>
<%@page import = "java.io.*"%>
<%
    Connection connection = null;
    //login is the name of the database
    String connectionURL = "jdbc:mysql://localhost:3306/login";
     
    ResultSet rs = null;
     
    PreparedStatement psmnt = null;
     
    InputStream sImage;
    try
    {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL, "root", "root");
    //Student is the table name
    psmnt = connection.prepareStatement("SELECT file_data FROM file WHERE id = ?");
    //In id "6" i have the image.
    psmnt.setString(1, "6");
    rs = psmnt.executeQuery();
    if(rs.next())
    {
    byte[] bytearray = new byte[1048576];
    int size=0;
    sImage = rs.getBinaryStream(1);
    response.reset();
    response.setContentType("image/jpeg");
    while((size=sImage.read(bytearray))!= -1 )
    {
    response.getOutputStream().write(bytearray,0,size);
    }
    response.flushBuffer();
    sImage.close();
    rs.close();
     
    }
    }
    catch(Exception ex)
    {
    out.println(ex);
    }
     
     
    psmnt.close();
    connection.close();
%>

Dani AI

Generated

your code is streaming binary bytes from a JSP. That is fragile because JSPs buffer markup and use a Writer under the hood; even a stray newline can commit text output before you switch to getOutputStream(), which results in a blank page. The reliable fix is to stream the image from a servlet and point your <img> tag at it, as suggested.

Here is a minimal servlet that reads the BLOB and writes it to the response. Note the MIME type, buffer, and try-with-resources. If you are on MySQL Connector/J 5.x, keep com.mysql.jdbc.Driver; for 8.x use com.mysql.cj.jdbc.Driver.

@WebServlet("/image")
public class ImageServlet extends HttpServlet {
  @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    int id = Integer.parseInt(req.getParameter("id"));
    try {
      Class.forName("com.mysql.jdbc.Driver"); // or com.mysql.cj.jdbc.Driver
      try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","root");
           PreparedStatement ps = con.prepareStatement(
             "SELECT mime_type, file_data FROM file WHERE id=?")) {
        ps.setInt(1, id);
        try (ResultSet rs = ps.executeQuery()) {
          if (!rs.next()) { resp.sendError(404); return; }
          String mime = rs.getString(1);
          resp.reset();
          resp.setContentType(mime != null ? mime : "image/jpeg");
          try (InputStream in = rs.getBinaryStream(2);
               ServletOutputStream out = resp.getOutputStream()) {
            byte[] buf = new byte[8192];
            for (int n; (n = in.read(buf)) > 0; ) out.write(buf, 0, n);
          }
        }
      }
    } catch (Exception e) { resp.sendError(500); }
  }
}

Then in your JSP (or any page):

<img src="image?id=6" alt="photo">

Quick checks:

  • Ensure the stored MIME type matches the actual image (jpeg vs png).
  • Do not print anything from the JSP when serving the image.
  • Consider a 404 when no row is found and avoid root/root in production; use a pool and least-privileged user.

for starters you need to change from this to using MVC, separate the view from the model. use servlets for your logic and jsp for display (logic in JSP Bad thing).
Then we can go from there.

check out the FrontController or grab a framework like SpringMVC, Struts, Wicket etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.