title: How to insert image in MySQL database using Servlet and JSP in Tomcat 7
body: <p>I'm trying to insert an image in a MySQL database using Servlet and JSP in Tomcat 7. When I click on the save button, it displays null. I am not getting any errors.</p>
  
  <p>Also I set <code>commons-fileupload.jar</code> file and <code>commons-io.jar</code> file. If you have some demonstration code, please give me. </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;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="FileUploadDBServlet" enctype="multipart/form-data"&gt;
          &lt;table border="0"&gt;
              &lt;tr&gt;
                  &lt;td&gt;First Name: &lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="firstName" size="50"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Last Name: &lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="lastName" size="50"/&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="50"/&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
        &lt;/form&gt;
      &lt;/center&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>FileUploadDBServlet.java</strong>:</p>
  
  <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;
  
  @WebServlet("/FileUploadDBServlet")
  @MultipartConfig(maxFileSize = 16177215)    // upload file's size up to 16MB
  public class FileUploadDBServlet extends HttpServlet {
  
      // database connection settings
      private String dbURL = "jdbc:mysql://localhost:3306/AppDB";
      private String dbUser = "root";
      private String dbPass = "root";
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          // gets values of text fields
          String firstName = request.getParameter("firstName");
          String lastName = request.getParameter("lastName");
  
          InputStream inputStream = null; // input stream of the upload file
  
          // obtains the upload file part in this multipart request
          Part filePart = request.getPart("photo");
          if (filePart != null) {
              // prints out some information for debugging
              System.out.println(filePart.getName());
              System.out.println(filePart.getSize());
              System.out.println(filePart.getContentType());
  
              // obtains input stream of the upload file
              inputStream = filePart.getInputStream();
          }
  
          Connection conn = null; // connection to the database
          String message = null;  // message will be sent back to client
  
          try {
  
                Class.forName("com.mysql.jdbc.Driver").newInstance();
              conn = DriverManager.getConnection(dbURL,dbUser,dbPass);
  
  
              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);
              }
  
              // sends the statement to the database server
              int row = statement.executeUpdate();
              if (row &gt; 0) {
                  message = "File uploaded and saved into database";
              }
          } catch (Exception  ex) {
              message = "ERROR: " + ex.getMessage();
               ex.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><strong>web.xml</strong>:</p>
  
  <pre><code>&lt;web-app&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;FileUploadDBServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;FileUploadDBServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;FileUploadDBServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/FileUploadDBServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>