7 Replies Latest reply: Feb 22, 2011 4:40 PM by EJP RSS

    Database not getting updated

    799603
       
      package com.sandy.servlets;
      import javax.servlet.*;
      import java.sql.*;
      import javax.servlet.http.*;
      import java.io.*;
      public class FirstServlet extends HttpServlet
      {
           Connection con;
           PreparedStatement ps;
           public void init(ServletConfig config) throws ServletException
           {
           String driver=config.getInitParameter("driver");
           String cs = config.getInitParameter("url");
           String user=config.getInitParameter("user");
           String password=config.getInitParameter("pwd");
           try
           {
                Class.forName(driver);
                con=DriverManager.getConnection(cs,user,password);
                ps=con.prepareStatement("INSERT INTO USER2 VALUES(?,?)");
           }
           catch (ClassNotFoundException e)
           {
                e.printStackTrace();
           }
      catch(SQLException e)
                {
      e.printStackTrace();
      }
      }//init()
      public void destroy()
           {
      try
      {
           if(ps!=null)
                ps.close();
           if(con!=null)
                con.close();
      }
      catch (SQLException e)
      {
           e.printStackTrace();
      }
      }
      public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
           {
      String a = request.getParameter("FirstName");
      String b = request.getParameter("LastName");
      try
      {
         if(a!=null)
           {
              if(b!=null)
           ps.setString(1,a);
           ps.setString(2,b);
           ps.executeUpdate();
      }
      }
      catch (SQLException e)
      {
           e.printStackTrace();
      }
      response.sendRedirect("user.html");
      }
      }
      
       
      I ran the above program in tomcat,i didn't get any error but unfortunately database is not getting updated which is the sole purpose of this application

      Edited by: sandy on Feb 8, 2011 12:33 AM