Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Insert Data into MySQL database from HTML SubForms

    • 1
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 111
    Answer it

    Hello EveryOne,

    I have 2 HTML Forms: 1. Register/Login 2. After User gets Login, He used to update details of him.

    I used MySql database in order to save Login details of User. And also i have another table for updating details of him after his login. I got succeed in register/Login process., But problem arises here.,., I dont know how to proceed further., After his Login., the user should be able to update his details in his respective table Used S/w: Eclipse, JDBC, HTML Servlets Any Help would be appreciable. Thanks In Advance

 2 Answer(s)

  • Here is the Login Servlet:

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*; 
    import java.sql.*;
    public class Login extends HttpServlet
    {
     public void doPost(HttpServletRequest request,
      HttpServletResponse response)throws ServletException,IOException
     {
    
     HttpSession session = request.getSession(true);
     try
     {
      String name=request.getParameter("name");
      String password=request.getParameter("pass");
      String member=request.getParameter("mem");
      session.setAttribute(name, "name");
      response.setContentType("text/html");
      PrintWriter out=response.getWriter();
      Class.forName("com.mysql.jdbc.Driver");
      Connection  con=DriverManager.getConnection
              ("jdbc:mysql://localhost:3306/EATWO","root","");
      PreparedStatement stmt=con.prepareStatement
                ("select * from singer where name=? and password=? and member=?");
      stmt.setString(1,name);
      stmt.setString(2,password);
      stmt.setString(3,member);
      ResultSet rset=stmt.executeQuery();
      if(rset.next()){
        if(member.equals("Singers"))
        {
            System.out.println(rset.getString(1));
    
                //out.println("Welcome," +name);
                //System.out.println(rset.getString(1));
                //out.println("<!DOCTYPE html><head>"+"<title>Singers Page</title>"+"</head>"+"<body>");
                //out.println("<script>alert('Create Ur Page Now')</script>");
                //out.print("</body></html>");
                //ServletContext context = getServletContext();
                RequestDispatcher rd = request.getRequestDispatcher("Singers.html");
                rd.include(request, response);
    
        }
        else{
            RequestDispatcher rd = request.getRequestDispatcher("Producers.html");
            rd.include(request, response);
        }
    }
      else
      {
        RequestDispatcher rd=request.getRequestDispatcher("login&#95;err.html");
        rd.include(request,response);
        con.close();
       }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    }
    }
    

    Here is the code which is used to store the details of that user:

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class SingerDetails extends HttpServlet{
      /**
     * 
     */
        private static final long serialVersionUID = 1L;
    
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            HttpSession session = request.getSession(true);
            String name = (String)request.getAttribute("name");
    
            String Sdetails = request.getParameter("Sdetails");
            String SName = request.getParameter("SName");
            String Language = request.getParameter("Language");
            String Website = request.getParameter("Email");
            String Mobile = request.getParameter("Mobile");
            try{
    
            //loading drivers for mysql
            Class.forName("com.mysql.jdbc.Driver");
    
        //creating connection with the database 
              Connection  con=DriverManager.getConnection
                         ("jdbc:mysql://localhost:3306/EATWO","root","");
    
            Statement statement = (Statement) con.createStatement();
            ResultSet rs = statement.executeQuery("SELECT userID from singer where name ='" + name + "'");
            String duplicate = null;
    
    
            while(rs.next()){
                duplicate = rs.getString(1);
                }
            if(duplicate == null){
                PreparedStatement ps=con.prepareStatement
                        ("insert into singerdetails(singername,stagename,Language,Email,Mobile) values(?,?,?,?,?)");
    
                 ps.setString(1, Sdetails);
                 ps.setString(2, SName);
                 ps.setString(3, Language);
                 ps.setString(4, Website);
                 ps.setString(5, Mobile);
                 int i=ps.executeUpdate();
    
                   if(i>0)
                   {
                     out.println("You are sucessfully registered");
                     //response.sendRedirect("./singerdetails1");
                   }
                }
                else{
                    //request.getRequestDispatcher("/register.html").forward(request, response);
                    //request.setAttribute("error","Invalid Username/password");
                    request.getRequestDispatcher("/Singers.html").forward(request, response);
                }
            }
            catch(Exception se)
            {
                se.printStackTrace();
            }
    
          }
      }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: