title: Servlet Update, Insert, Retrieve, Delete - Database Connectivity unable to perform operations
body: <p>I have made a program for insertion, viewing, deletion, updation of stored data in database. </p>
  
  <p>But since last 3-4 hours I am getting error :- javax.servlet.ServletException: Servlet execution threw an exception</p>
  
  <p>I know what the mistake is, every time I make blunder in web.xml file. As I am always confused with configuring web.xml file. So I get "resource not available error" can any one help me with it. I have posted the web.xml file
  Can any one help me with solving this problem ??</p>
  
  <p>web.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"&gt;
    &lt;display-name&gt;&lt;/display-name&gt;
    &lt;servlet&gt;
      &lt;description&gt;This is the description of my J2EE component&lt;/description&gt;
      &lt;display-name&gt;This is the display name of my J2EE component&lt;/display-name&gt;
      &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;MyServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet&gt;
      &lt;description&gt;This is the description of my J2EE component&lt;/description&gt;
      &lt;display-name&gt;This is the display name of my J2EE component&lt;/display-name&gt;
      &lt;servlet-name&gt;DataInsert&lt;/servlet-name&gt;
      &lt;servlet-class&gt;DataInsertTable&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet&gt;
      &lt;description&gt;This is the description of my J2EE component&lt;/description&gt;
      &lt;display-name&gt;This is the display name of my J2EE component&lt;/display-name&gt;
      &lt;servlet-name&gt;DataUpdate&lt;/servlet-name&gt;
      &lt;servlet-class&gt;DataUpdateTable&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/MyServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;DataInsert&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/DataInsert&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;DataUpdate&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/DataUpdate&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>This is my code :- </p>
  
  <p>index.jsp</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;%
      String action="/SimpleServlet/DataInsertTable";
      String method="get";
  
      if(request.getAttribute("EDIT")!=null)
      {
          action="/SimpleServlet/DataUpdateTable";
          method="post";
      }
  
      %&gt;
  
  
      &lt;form method= "&lt;%=method%&gt;" action="&lt;%=action%&gt;"&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;%if(request.getAttribute("NAME")!=null){ %&gt;
              &lt;td&gt;&lt;input type="text" name="name" value="&lt;%=request.getAttribute("NAME")%&gt;"&gt;&lt;/input&gt;
  
              &lt;%}else{ %&gt;
              &lt;td&gt;&lt;input type="text" name="name"&gt;&lt;/input&gt;
              &lt;%} %&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;%if(request.getAttribute("ROLL")!=null){ %&gt;
              &lt;td&gt;&lt;input type="text" name="roll" value="&lt;%=request.getAttribute("ROLL")%&gt;"&gt;&lt;/input&gt;
  
              &lt;%}else{ %&gt;
              &lt;td&gt;&lt;input type="text" name="roll"&gt;&lt;/input&gt;
              &lt;%} %&gt;
              &lt;/td&gt;       
          &lt;/tr&gt;
  
          &lt;tr&gt;
              &lt;td&gt;Class &lt;/td&gt;
              &lt;td&gt; :- &lt;/td&gt;
               &lt;%if(request.getAttribute("CLAS")!=null){ %&gt;
              &lt;td&gt;&lt;input type="text" name="clas" value="&lt;%=request.getAttribute("CLAS")%&gt;"&gt;&lt;/input&gt;
  
              &lt;%}else{ %&gt;
              &lt;td&gt;&lt;input type="text" name="clas"&gt;&lt;/input&gt;
              &lt;%} %&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;%if(request.getAttribute("MONO")!=null){ %&gt;
              &lt;td&gt;&lt;input type="text" name="mono" value="&lt;%=request.getAttribute("MONO")%&gt;"&gt;&lt;/input&gt;
  
              &lt;%}else{ %&gt;
              &lt;td&gt;&lt;input type="text" name="mono"&gt;&lt;/input&gt;
              &lt;%} %&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"&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 DataInsert extends HttpServlet {
      public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws IOException, ServletException    {
  
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
          PrintWriter pwinsert = res.getWriter();
  //      PrintWriter pwdelete = 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;/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;"); 
  
              int a = 2; 
              out.println("&lt;table border="+a+ "&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;td&gt; Edit &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";
  
              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("Your data has been stored in the database"); 
                      } 
                      else    { 
                          pwinsert.println("Your data could not be stored in the database"); 
                      } 
                  }
              } 
              catch(Exception e)  {
                  pwinsert.println(e.getMessage());
              } 
  
              String idr=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 deleted..."); 
              }
  
  
  //          res.sendRedirect("DataInsertTable.java"); 
  
  
  
              idr = "SELECT * FROM student"; 
  
              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;td&gt;" + "&lt;a href='"+req.getContextPath()+"/DataUpdate?mode=EDIT&amp;name="+rs.getString(1)+"&amp;roll="+rs.getInt(2)+"&amp;clas="+rs.getString(3) +"&amp;mono="+rs.getString(4)+"'&gt;Edit&lt;/a&gt; &lt;/br&gt;" + "&lt;/td&gt;"); 
                      out.println("&lt;/tr&gt;"); 
                  } 
  
                  out.println("&lt;tr&gt;"); 
                      out.println("&lt;td&gt; &lt;/td&gt;"); 
                      out.println("&lt;td&gt; &lt;/td&gt;"); 
                      out.println("&lt;td&gt;" + "&lt;input type=\"submit\" value=\"Delete\" /&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>
  
  <p>DataUpdateTable.java</p>
  
  <pre><code>import java.io.*; 
  import java.sql.*; 
  import javax.servlet.*; 
  import javax.servlet.http.*; 
  
  public class DataUpdateTable extends HttpServlet    {
      public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws IOException, ServletException    { 
  
          res.setContentType("text/html"); 
          PrintWriter out = res.getWriter(); 
          PrintWriter pwinsert = res.getWriter(); 
  //      PrintWriter pwdelete = 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;/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;"); 
  
                  int a = 2; 
                  out.println("&lt;table border="+a+ "&gt; "); 
                  out.println("&lt;tr&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"); 
  
                  req.setAttribute("NAME", nm);
                  req.setAttribute("ROLL", roll);
                  req.setAttribute("CLAS", clas);
                  req.setAttribute("MONO", mono);
                  req.setAttribute("EDIT", "Y");
  
                  req.getRequestDispatcher("/index.jsp").forward(req, res);
  
      }
  
  
      public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws IOException, ServletException    {
  
          res.setContentType("text/html"); 
          PrintWriter out = res.getWriter(); 
          PrintWriter pwinsert = res.getWriter(); 
  //      PrintWriter pwdelete = res.getWriter(); 
  
          Connection con = null; 
          PreparedStatement ps = null; 
          ResultSet rs = null; 
          Statement st = null; 
  
  
          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";
  
          try { 
              con = DriverManager.getConnection(URL,Username,Password); 
  
              String nm = req.getParameter("name"); 
              String roll = req.getParameter("roll"); 
              String clas = req.getParameter("clas"); 
              String mono = req.getParameter("mono"); 
  
  
              if(req.getParameter("choise")==null)    {
                  ps = con.prepareStatement("update student set name=?, rollno=?, class=?, mobileno=? where rollno=?"); 
                  ps.setString(1,nm); 
                  ps.setString(2,roll); 
                  ps.setString(3,clas); 
                  ps.setString(4,mono); 
                  ps.setString(5,roll); 
  
                  int i = ps.executeUpdate(); 
                  pwinsert.println(i); 
  
  
                  if(i!=0)    {
                      pwinsert.println("Your data has been stored in the database"); 
                  } 
                  else    { 
                      pwinsert.println("Your data could not be stored in the database"); 
                  } 
              }
          } 
          catch(Exception e)  { 
              pwinsert.println(e.getMessage()); 
          } 
      }
  }
  </code></pre>