0

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();
%>
2
Contributors
1
Reply
6
Views
5 Years
Discussion Span
Last Post by ejosiah
0

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.

Edited by ejosiah: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.