Advertisement
Guest User

Untitled

a guest
Oct 11th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package com.product;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.*;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.annotation.WebServlet;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. /**
  13.  * Servlet implementation class ProductServlet
  14.  */
  15. @WebServlet("/ProductServlet")
  16. public class ProductServlet extends HttpServlet {
  17.     private static final long serialVersionUID = 1L;
  18.        
  19.     /**
  20.      * @see HttpServlet#HttpServlet()
  21.      */
  22.     public ProductServlet() {
  23.         super();
  24.        
  25.     }
  26.  
  27.     /**
  28.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  29.      */
  30.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  31.         Connection conn = null;
  32.         Statement stmt = null;
  33.         ResultSet rs = null;
  34.        
  35.         String productId = request.getParameter("productId");
  36.         String productName = request.getParameter("productName");
  37.         String quantity = request.getParameter("quantity");
  38.         String price = request.getParameter("price");
  39.         String category = request.getParameter("category");
  40.        
  41.         String addProd = productId + ", " + productName + ", " + quantity + ", " + price + " ," + " ," + category + " ,";
  42.        
  43.         try{
  44.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  45.             String connectionUrl = "jdbc:mysql://localhost:3306/assignment2";
  46.             String connectionUser = "root";
  47.             String connectionPassword = "admin";
  48.             conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
  49.            
  50.        
  51.         String sql = "INSERT into product values (" + addProd + ")";
  52.         stmt.executeUpdate(sql);
  53.          
  54.         }catch (Exception e)
  55.            {
  56.             e.printStackTrace();       
  57.            }
  58.        
  59.         response.setContentType("text/html");
  60.         PrintWriter out = response.getWriter();
  61.     }
  62.  
  63.     /**
  64.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  65.      */
  66.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  67.         // TODO Auto-generated method stub
  68.         doGet(request, response);
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement