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();
%>

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.