Step1:Develop a form and give appreciate action value
Product.html
<form action="insert">
Pid:<input type="text" name="pid"><br>
Pname:<input type="text" name="pname"><br>
Price:<input type="text" name="price"><br>
<input type="submit" value="store">
</form>             
Top of Form

Step2:develop a javabean class and it is interact with DBServer
StoreJB.java

package data.store;
import java.sql.*;

public class StoreJB {
          int pid;
          String pname;
          double price;
         
          public void setPid(Integer pid) {
                   this.pid=pid;
          }public Integer getPid() {
                   return pid;
          }
public void setPname(String pname) {
                   this.pname=pname;
          }public String getPname() {
                   return pname;
          }
public void setPrice(Double price) {
                   this.price=price;
          }public Double getPrice() {
                   return price;
          }

          public void storeProduct(){
                   try{
         
Class.forName("com.mysql.jdbc.Driver");
Connection objConnection =  DriverManager.getConnection("jdbc:mysql://localhost:3306/ashu","root","root");
PreparedStatement pstmt = objConnection.prepareStatement("insert into product values(?,?,?)");
                                        pstmt.setInt(1,pid);
                                        pstmt.setString(2,pname);
                                        pstmt.setDouble(3,price);
                                        pstmt.executeUpdate();
                      }
                                      catch(Exception e){
                                                          e.printStackTrace();                                                                          }
}
}

Step3:develop a servlet controller and create a StoreJB object and call that method
ControllerServlet.java

package info.call;
import javax.servlet.http.*;
import javax.servlet.*;
import data.store.*;
public class ControllerServlet extends HttpServlet {
public void service (HttpServletRequest request,HttpServletResponse response) {
          try{
          int pid = Integer.parseInt(request.getParameter("pid"));
          String pname = request.getParameter("pname");
          double price = Double.parseDouble(request.getParameter("price"));
                   data.store.StoreJB sjb = new data.store.StoreJB();
                   sjb.setPid(pid);
                   sjb.setPname(pname);
                   sjb.setPrice(price);
                   sjb.storeProduct();
                   request.setAttribute("pid",pid);
                   request.setAttribute("pname",pname);
                   request.setAttribute("price",price);
          RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
           rd.forward(request,response);
         
                   } catch(Exception e) {
                             e.printStackTrace();
                   }
}
}Bottom of Form

Step4:Develop a success form
display.jsp
Product Details is:<br><br>
PID:<%= request.getAttribute("pid")%><br>
Pname:<%= request.getAttribute("pname")%><br>
Price:<%= request.getAttribute("price")%><br>
<a href ="product.html">New Product</a>

0 comments:

Post a Comment