Announcement Announcement Module
Collapse
No announcement yet.
jsp error in connecting with msyql Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • jsp error in connecting with msyql

    Hi, I have written a simple jsp script to retrieve image data from mysql database, but the jsp thrown null exceptions.

    Here is the jsp script:
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>

    <% // declare a connection by using Connection interface Connection connection = null;
    /* Create string of connection url within specified format with machine
    name, port number and database name. Here machine name id localhost
    and database name is mahendra. */

    String connectionURL = "jdbc:mysql://localhost:3306/houseware";

    /*declare a resultSet that works as a table resulted by execute a specified
    sql query. */

    ResultSet rs = null;

    // Declare statement.
    PreparedStatement psmnt = null;

    // declare InputStream object to store binary stream of given image.
    InputStream sImage;
    Connection connection = null;

    try {
    // Load JDBC driver "com.mysql.jdbc.Driver"
    Class.forName("com.mysql.jdbc.Driver").newInstance ();

    /* Create a connection by using getConnection() method that takes
    parameters of string type connection url, user name and password to
    connect to database. */
    connection = DriverManager.getConnection(connectionURL, "root", "xxxxxxx");

    /* prepareStatement() is used for create statement object that is
    used for sending sql statements to the specified database. */

    psmnt = connection.prepareStatement("SELECT image_data FROM category_images WHERE category_id = ?");
    psmnt.setInt(1, 5499); // here integer number '11' is image id from the table
    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) ;
    }
    }
    }
    catch(Exception ex){
    out.println("error :"+ex);
    }
    finally {
    // close all the connections.
    rs.close();
    psmnt.close();
    connection.close();
    }
    %>
    Exceptions:
    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is org.apache.tiles.impl.CannotRenderException: ServletException including path '/WEB-INF/layouts/standard.jsp'.
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:734)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:847)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 343)
    org.springframework.security.web.access.intercept. FilterSecurityInterceptor.invoke(FilterSecurityInt erceptor.java:109)
    org.springframework.security.web.access.intercept. FilterSecurityInterceptor.doFilter(FilterSecurityI nterceptor.java:83)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.access.ExceptionT ranslationFilter.doFilter(ExceptionTranslationFilt er.java:97)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.session.SessionMa nagementFilter.doFilter(SessionManagementFilter.ja va:100)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.authentication.An onymousAuthenticationFilter.doFilter(AnonymousAuth enticationFilter.java:78)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.servletapi.Securi tyContextHolderAwareRequestFilter.doFilter(Securit yContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.savedrequest.Requ estCacheAwareFilter.doFilter(RequestCacheAwareFilt er.java:35)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.authentication.http://www.BasicAuthenticationFilter...lter.java:177)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.authentication.Ab stractAuthenticationProcessingFilter.doFilter(Abst ractAuthenticationProcessingFilter.java:188)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.authentication.lo gout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355)
    org.springframework.security.web.context.SecurityC ontextPersistenceFilter.doFilter(SecurityContextPe rsistenceFilter.java:79)
    I am sure it is my jsp is not correct. But I can't find anything wrong with it.
    Any help/suggestion is very appreciated.
    Thanks
    Sam

  • #2
    Please tell me this is a joke or a bad home work assignment?! You aren't really writing java code in a JSP let alone data access code?!

    I strongly suggest you take a look at Spring MVC (your view should basically be dumb and only render something not retrieve). I strongly suggest you take a look at some spring tutorials (or at least some basic code structuring tutorial) before progressing.

    Comment


    • #3
      right, this is just a lazy implementation, which is used for a quick test whether the content of an image stored in database is viewable / correct.

      Yes spring mvc is good, but I am having trouble to map an uri template variable path to a controller. And I couldn't found a complete spring mvc tutorial or example to show me how to do it.

      If you have other suggestion, please send through.
      Best regards
      sam

      Comment

      Working...
      X