Write a servlet which accepts product details from html form and stores the product details into database?



Product database:
create table Product
(
pid number (4),
pname varchar2 (15),
price number (6, 2)
);

web.xml:
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>DatabaServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/getdata</url-pattern>
</servlet-mapping>
</web-app>

DarabaServ.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class DatabaServ extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  String pid1 = req.getParameter("prodata_pid");
  String pname = req.getParameter("prodata_pname");
  String price1 = req.getParameter("prodata_price");
  int pid = Integer.parseInt(pid1);
  float price = Float.parseFloat(price1);
  try {
   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
   Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:
    Hanuman ","
    scott ","
    tiger ");
    PreparedStatement ps = con.prepareStatement("insert into Product values (?,?,?)"); ps.setInt(1, pid); ps.setString(2, pname); ps.setFloat(3, price); int i = ps.executeUpdate(); pw.println("<h4>" + i + " ROWS INSERTED..."); con.close();
   } catch (Exception e) {
    res.sendError(503, "PROBLEM IN DATABASE...");
   }
  }
  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
   doGet(req, res);
  }
 }


No comments:

Post a Comment