Announcement Announcement Module
No announcement yet.
jsp error in connecting with msyql Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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="*" %>

    <% // 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( {
    byte[] bytearray = new byte[1048576];
    int size=0;
    sImage = rs.getBinaryStream(1);
    while((!= -1 ){
    response.getOutputStream().write(bytearray,0,size) ;
    catch(Exception ex){
    out.println("error :"+ex);
    finally {
    // close all the connections.
    type Exception report


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


    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(
    org.springframework.web.servlet.FrameworkServlet.d oGet(
    javax.servlet.http.HttpServlet.service(HttpServlet .java:734)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:847)$ VirtualFilterChain.doFilter( 343) FilterSecurityInterceptor.invoke(FilterSecurityInt FilterSecurityInterceptor.doFilter(FilterSecurityI$ VirtualFilterChain.doFilter( 355) ranslationFilter.doFilter(ExceptionTranslationFilt$ VirtualFilterChain.doFilter( 355) nagementFilter.doFilter(SessionManagementFilter.ja va:100)$ VirtualFilterChain.doFilter( 355) onymousAuthenticationFilter.doFilter(AnonymousAuth$ VirtualFilterChain.doFilter( 355) tyContextHolderAwareRequestFilter.doFilter(Securit$ VirtualFilterChain.doFilter( 355) estCacheAwareFilter.doFilter(RequestCacheAwareFilt$ VirtualFilterChain.doFilter( 355)$ VirtualFilterChain.doFilter( 355) stractAuthenticationProcessingFilter.doFilter(Abst$ VirtualFilterChain.doFilter( 355) gout.LogoutFilter.doFilter($ VirtualFilterChain.doFilter( 355) ontextPersistenceFilter.doFilter(SecurityContextPe
    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.

  • #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.


    • #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