title: How to insert image to MySql database using spring mvc?
body: <p>I'm going to develop a e-commerce store for my semester project and I'm using spring mvc, hibernate and jsp for this purpose. Now it's required to upload image from client side and put it in the MySql database. I have already tried but somehow can't do this job. </p>
  
  <p>This is how i created database</p>
  
  <pre><code>create table PRODUCT_INFORMATION(
  productName varchar(255) not null primary key,
  productShortInformation varchar(500),
  productFullInformation varchar(60000),
  productImage longblob
  );
  </code></pre>
  
  <p>This is my model class ProductInformation.java</p>
  
  <pre><code>@Entity
  @Table(name="PRODUCT_INFORMATION")
  public class ProductInformation {
      @Id
      private String productName;
      private byte[] productImage;
      private String productShortInformation;
      private String productFullInformation;
      //getter and setter
  }
  </code></pre>
  
  <p>This is my controller class AddProductController.java</p>
  
  <pre><code>@Controller
  @RequestMapping(value="/manager")
  public class AddProductController extends HttpServlet {
  
      @RequestMapping("/addProduct.html")
      public ModelAndView AddingProduct() {
          ModelAndView model = new ModelAndView("AddProduct");        
          return model;
      }
  
      @RequestMapping("/addingProductSuccess.html")
      public ModelAndView addProductSuccess(HttpServletRequest request)  {
          ModelAndView model = new ModelAndView("AddingProductSuccess");
          ProductInformation productInformation = new ProductInformation();
  
          productInformation.setProductName(request.getParameter("productName"));
          productInformation.setProductShortInformation(request.getParameter("productShortInformation"));
          productInformation.setProductFullInformation(request.getParameter("productFullInformation"));
  
          try {
              SessionFactory sessionFactory  = new Configuration().configure().buildSessionFactory();
              org.hibernate.Session session = sessionFactory.openSession();
              session.beginTransaction();
              session.save(productInformation);
              session.getTransaction().commit();
              session.close();
          } catch(Exception e) {
              System.out.println("Exception on storing in database: " + e.toString());
              return new ModelAndView("AddProduct");
          }
  
          return model;
      }
  
  }
  </code></pre>
  
  <p>This is a view AddProduct.jsp</p>
  
  <pre><code>&lt;form action="/E-CommerceDemo/manager/addingProductSuccess.html" /&gt;
      &lt;table&gt;
          &lt;tr&gt;&lt;td&gt;&lt;input type="text" name="productName" /&gt;   &lt;/td&gt; &lt;/tr&gt;
          &lt;tr&gt;&lt;td&gt;&lt;input type="text" name="productShortInformation" /&gt;  &lt;/td&gt; &lt;/tr&gt;
          &lt;tr&gt;&lt;td&gt;&lt;input type="text" name="productFullInformation" /&gt;   &lt;/td&gt; &lt;/tr&gt;
          &lt;tr&gt;&lt;td&gt;&lt;input type="file" name="productImage" /&gt; &lt;/td&gt; &lt;/tr&gt;
          &lt;tr&gt;&lt;td&gt;&lt;input type="submit"/&gt;&lt;/td&gt;&lt;/tr&gt;
  `   &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Now in the AddProduct.jsp File when i click submit button i want to save all the information be saved in the database. I tried this operation in the controller class( name = AddProductController.java) addProductSuccess() fucntion. It inserts productName, productShortInformation and productFullInformation. But can't find out how to insert image. Thanks in advance.</p>