title: Update/Edit entered data from database - servlet
body: <p>I have prepared a form for inserting, viewing and deleting data
  and now I want to add option to edit that data but either I am following wrong way to do it or I dont know how to do that. </p>
  
  <p>So can any one help me proceeding to edit the entered data ??  Should I give reference to old html page to edit or new page or .. ??
  I know I haven't followed MVC structure but I wanted to do whole coding in single page so waiting for help..
  Thank you.</p>
  
  <p>These are my codes.</p>
  
  <p>Datastore.html</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Data insertion form&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
  
      &lt;form method= "get" action="/datainsert/DataInsertTable"&gt;
  
      &lt;h1&gt;&lt;center&gt;Enter the required information&lt;/center&gt;&lt;/h1&gt;
  
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;Name&lt;/td&gt;
              &lt;td&gt; :- &lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="name"&gt;&lt;/input&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
          &lt;tr&gt;
              &lt;td&gt;Roll Number&lt;/td&gt;
              &lt;td&gt; :- &lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="roll"&gt;&lt;/input&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
          &lt;tr&gt;
              &lt;td&gt;Class &lt;/td&gt;
              &lt;td&gt; :- &lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="clas"&gt;&lt;/input&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
          &lt;tr&gt;
              &lt;td&gt;Mobile Number &lt;/td&gt;
              &lt;td&gt; :- &lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="mono"&gt;&lt;/input&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="submit" value="Submit" width="16" onClick=alert("data stored.");&gt;&lt;/input&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
      &lt;/table&gt;
  
  
      &lt;/form&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>DataInsertTable.java</p>
  
  <pre><code>import java.io.*;
  import java.sql.*;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  
  public class DataInsertTable extends HttpServlet    {
      public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws IOException, ServletException    {
  
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
          PrintWriter pwinsert = res.getWriter();
  
  
          Connection con = null;
          PreparedStatement ps = null;
          ResultSet rs = null;
          Statement st = null;
  
  
          out.println("&lt;html&gt;");
          out.println("&lt;head&gt;");
              out.println("&lt;title&gt;User Data&lt;/title&gt;");
  
              out.println("&lt;script language = JavaScript&gt;");
              out.println("&lt;function f1() {&gt;");
              out.println("&lt;{&gt;");
              out.println("&lt;function f1() {&gt;");
              out.println("&lt;alert('Select data');&gt;");
              out.println("&lt;}&gt;");
              out.println("&lt;/script&gt;");
  
              out.println("&lt;/head&gt;");
  
          out.println("&lt;/head&gt;");
  
          out.println("&lt;body&gt;");
  
          out.println("&lt;center&gt;&lt;u&gt;&lt;h1&gt;User Data&lt;/h1&gt;&lt;/u&gt;");
  
          out.println("&lt;form name='form' &gt;");
  
              out.println("&lt;table border="+2+ "&gt; ");
                  out.println("&lt;tr&gt;");
                      out.println("&lt;td&gt; Select &lt;/td&gt;");
                      out.println("&lt;td&gt; Name &lt;/td&gt;");
                      out.println("&lt;td&gt; Roll No. &lt;/td&gt;");
                      out.println("&lt;td&gt; Class &lt;/td&gt;");
                      out.println("&lt;td&gt; Mobile Number &lt;/td&gt;");
                  out.println("&lt;/tr&gt;");
  
  
                  String nm = req.getParameter("name");
                  String roll = req.getParameter("roll");
                  String clas = req.getParameter("clas");
                  String mono = req.getParameter("mono");
  
  
                  try {
                      Class.forName("oracle.jdbc.driver.OracleDriver");
                  }
                  catch(ClassNotFoundException ex)    {
                      System.out.println("driver not loaded");
                      System.exit(0);
                  }
  
  
                  String URL = "jdbc:oracle:thin:@192.168.106.87:1521:ora11g";
                  String Username = "pratik";
                  String Password = "pratik";
  
  
  
  
  
  
  //Insert                
                  try {
                      con = DriverManager.getConnection(URL,Username,Password);
  
                      if(req.getParameter("choise")==null)    {
                          ps = con.prepareStatement("INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ? )");
                          ps.setString(1,nm); 
                          ps.setString(2,roll);
                          ps.setString(3,clas);
                          ps.setString(4,mono);
  
  
                          int i = ps.executeUpdate();
                          pwinsert.println(i);
  
                          if(i!=0)    {
                              pwinsert.println("data has been stored");
                          }
                          else    {
                              pwinsert.println("data could not be stored");
                          }
                      }
                  }
  
                  catch(Exception e)  {
                      pwinsert.println(e.getMessage());
                  }
  
  
                  String idr=null;
  
  //Delete
  //cond. nt neeeded (null)
  
                  if(req.getParameter("choise")!=null)    {
                      nm = req.getParameter("choise");
  
                      idr = "DELETE from student WHERE name ='"+nm+"'";
  
                      try {
                          st=con.createStatement();
                          rs = st.executeQuery(idr);
                      }
                      catch (Exception e) {
                          e.getMessage();
                          System.out.println("Error " +e);
                      }
  
                      System.out.println("Data se deleted..."); 
  
  
  
                  }
  
  //Update
  
  /*              String updt = "UPDATE student SET name ='"+nm +"',rollno='" + roll + "', class='"+ clas +"', mobileno='"+mono+"' WHERE name ='"+nm +"' ";
  
                  PreparedStatement ps1 = null;  
                  try {
                      ps1=con.prepareStatement(updt);
                  } catch (SQLException e2) {
                      e2.printStackTrace();
                  }  
  
                  int i = 0;
                  try {
                      i = ps1.executeUpdate();
                  } catch (SQLException e1) {
                      e1.printStackTrace();
                  }  
                  if(i==1)  
                      out.println("success");  
                  else  
                      out.println("failed"); 
  
  */
  
  // edit             
                  if(req.getParameter("choise")!=null)    {
                      nm = req.getParameter("choise");
  
                      idr = "UPDATE student SET name = ?, rollno = ?, class = ?, mobileno = ? WHERE name = ?";
  
                      try {
                          st=con.createStatement();
                          int rs1 = st.executeUpdate(idr);
                      }
                      catch (Exception e) {
                          e.getMessage();
                          System.out.println("Error " +e);
                      }
  
                      System.out.println("Data Edited..."); 
  
                  }
                  else    {
  
                      System.out.println("Select row..");
                  }
  
  
  /*              String upd = "UPDATE student SET name = ?, rollno = ?, class = ?, mobileno = ? WHERE name = ?"; 
  
                  PreparedStatement prest = null;
                  try {
                      prest = con.prepareStatement(upd);
                  } catch (SQLException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
                  try {
                      prest.setString(1,"nm");
                  } catch (SQLException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
                  try {
                      prest.setString(2,"roll");
                  } catch (SQLException e2) {
                      // TODO Auto-generated catch block
                      e2.printStackTrace();
                  }
                  try {
                      prest.setString(3,"clas");
                  } catch (SQLException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
                  try {
                      prest.setString(4,"mono");
                  } catch (SQLException e2) {
                      // TODO Auto-generated catch block
                      e2.printStackTrace();
                  }
                  try {
                      prest.executeUpdate();
                  } catch (SQLException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
                  System.out.println("Updating Successfully!");
  
  */              
  
                  idr = "SELECT * FROM student WHERE name IS NOT NULL";
  
  
                  try {
                      st=con.createStatement();
                      rs = st.executeQuery(idr);
                  }
                  catch (Exception e) {
                      e.getMessage();
                      System.out.println("Error " +e);
                  }
  
                  try {
                      while (rs.next())   {
                          out.println("&lt;tr&gt;");
                          out.println("&lt;td&gt;" + "&lt;input type=\"radio\" name=\"choise\" value=\"" + rs.getString(1) + "\" /&gt; &lt;/br&gt;" + "&lt;/td&gt;");
                          out.println("&lt;td&gt;" + rs.getString(1) + "&lt;/td&gt;" + "\t &lt;td&gt;" + rs.getInt(2) + "&lt;/td&gt;" + "\t &lt;td&gt;" + rs.getString(3) + "&lt;/td&gt;" + "\t &lt;td&gt;" + rs.getString(4));
                          out.println("&lt;/tr&gt;");
                      }
  
                      out.println("&lt;tr&gt;");
                      out.println("&lt;td&gt; &lt;/td&gt;");
  {                   
                      out.println("&lt;td&gt;" + "&lt;input type=\"submit\" value=\"Delete Data\" /&gt;" + "&lt;/td&gt;");
  
                      if(req.getParameter("choise") != null)  {
  
                          out.println("submitted.");
                      }
  
                      else    {
                          out.println("&lt;script&gt;alert('Data stored into database')&lt;/script&gt;");
                      }
              }
  
  
  // To Edit 
                      out.println("&lt;td&gt;" + "&lt;input type=\"button\" value=\"Edit \" onClick = f1() /&gt;" + "&lt;/td&gt;");
  
  
                      out.println("&lt;td&gt; &lt;/td&gt;");
                      out.println("&lt;td&gt; &lt;/td&gt;");
                      out.println("&lt;/tr&gt;");
                  }
  
                  catch (Exception e) {
                      e.getMessage();
                      System.out.println("Error" +e);
                  }
  
                  try {
                      rs.close();
                  }
                  catch (Exception e) {
                      e.getMessage();
                      System.out.println("Error" +e);
                  }
  
                  try {
                      st.close();
                  }
                  catch (Exception e) {
                      e.getMessage();
                      System.out.println("Error" +e);
                  }
  
                  out.println("&lt;/table&gt;");
                  out.println("&lt;/form&gt;");
                  out.println("&lt;/center&gt;");
                  out.println("&lt;/body&gt;");
                  out.println("&lt;/html&gt;");
                  out.close();
              }
      }
  </code></pre>