title: Using JSP to store images in Mysql and upload the images online
body: <p>I am still wondering what it is that i am doing wrong,my problem is i dont know if it is my entire sample code which has na error or my connection to the database which has an issues, as of where i stand i am not sure what is making me get this error. (displayed below).I have tried to look here at stack overflow but all i get is loading images using JSP but not adding them to the database. If we have one i couldnt trace please help me with the link. I have included all the libraries needed and my code has no error apart from this output. I came here because i am stranded and need a short review of what you proffesionals think is wrong with my code as done below. I will really appreciate anyhelp given as i am working on a deadline. 
  my Database name is </p>
  
  <blockquote>
    <p>AppDB
    I was wondering should i use the name of the table? to INSERT INTO? which is 
    'contacts'</p>
    
    <p>Error Message</p>
    
    <blockquote>
      <p>HTTP Status 404 - /UploadImageOnWeb/uploadServlet type Status report
              message /UploadImageOnWeb/uploadServlet description The requested<br>
              resource is not available. Apache Tomcat/8.0.23</p>
    </blockquote>
  </blockquote>
  
  <p>Thank you </p>
  
  <blockquote>
    <p>Java Servlet Code</p>
  </blockquote>
  
  <pre><code>import java.io.IOException;
  import java.io.InputStream;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.MultipartConfig;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.Part;
  
  /**
   * Servlet implementation class FileUploadDBServlet
   */
  @MultipartConfig(maxFileSize = 16177215)
  @WebServlet("/FileUploadDBServlet")
  public class FileUploadDBServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      // database connection settings
      /*private String dbURL = "jdbc:mysql://localhost/AppDB";
      private String dbUser = "root";
      private String dbPass = "mypassword";*/
  
      protected void doPost(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
  
          String firstName = request.getParameter("firstName");
          String lastName = request.getParameter("lastName");
  
          InputStream inputStream = null; 
  
  
          Part filePart = request.getPart("photo");
          if (filePart != null) {
  
              System.out.println(filePart.getName());
              System.out.println(filePart.getSize());
              System.out.println(filePart.getContentType());
  
  
              inputStream = filePart.getInputStream();
          }
  
          Connection conn = null;
          String message = null;  
  
          try {
              // connects to the database
              /*DriverManager.registerDriver("com.mysql.jdbc.Driver");
              conn = DriverManager.getConnection(dbURL, dbUser, dbPass);*/
  
             Class.forName("com.mysql.jdbc.Driver");
              conn = DriverManager.getConnection("jdbc:mysql://localhost/AppDB","root","mypassword");
  
  
  
  
              // constructs SQL statement
              String sql = "INSERT INTO contacts (first_name, last_name, photo) values (?, ?, ?)";
              PreparedStatement statement = conn.prepareStatement(sql);
              statement.setString(1, firstName);
              statement.setString(2, lastName);
  
              if (inputStream != null) {
                  // fetches input stream of the upload file for the blob column
                  statement.setBlob(3, inputStream);
              }
  
  
              int row = statement.executeUpdate();
              if (row &gt; 0) {
                  message = "File uploaded and saved into database";
              }
          } catch (SQLException ex) {
              message = "ERROR: " + ex.getMessage();
              ex.printStackTrace();
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } finally {
              if (conn != null) {
                  // closes the database connection
                  try {
                      conn.close();
                  } catch (SQLException ex) {
                      ex.printStackTrace();
                  }
              }
              // sets the message in request scope
              request.setAttribute("Message", message);
  
              // forwards to the message page
              getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
          }
      }
  }
  </code></pre>
  
  <p>My .Jsp class</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;File Upload to Database Demo&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;center&gt;
          &lt;h1&gt;File Upload to Database Demo&lt;/h1&gt;
          &lt;form method="post" action="uploadServlet" enctype="multipart/form-data"&gt;
              &lt;table border="0"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Enter First Name: &lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="firstName" size="20"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Enter Last Name: &lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="lastName" size="20"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Portrait Photo: &lt;/td&gt;
                      &lt;td&gt;&lt;input type="file" name="photo" size="20"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td colspan="2"&gt;
                          &lt;input type="submit" value="Save"&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/form&gt;
      &lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Display Jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Message&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;center&gt;
          &lt;h3&gt;&lt;%=request.getAttribute("Message")%&gt;&lt;/h3&gt;
      &lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>