Step1:create view page using html (jsp)

 

<form action="MyServlet">
   Enter Name:<input type="text" name="name"><br>
   Enter Email:<input type="text" name="email"><br>
   Enter Mno:<input type="text" name="mobile"><br>
   <input type="submit" value="ok">
    </form>


Step2:servlet code with database connection



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
        String a=request.getParameter("name");
        String b=request.getParameter("email");
        String c=request.getParameter("mobile");

         try {
             Class.forName("com.mysql.jdbc.Driver");
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/x","root","root");
             out.println(".............con.....: "+con);
            PreparedStatement ps=con.prepareStatement("insert into student values(?,?,?)");
            
            ps.setString(1,a);
            ps.setString(2,b);
            ps.setString(3,c);
             int i=ps.executeUpdate();
               if(i>0){
                
            RequestDispatcher rd=request.getRequestDispatcher("success.jsp");
             rd.include(request, response);
        }

           
        } catch (Exception ex) {
        }
   
       
       
       
    }

 
}


Step3:success page(success.jsp)

after enter details

 

 

 

 

 

 

your details are inserted successfully..........

click here <a href="index.jsp">Login</a>



after that check your database like follows:

 




0 comments:

Post a Comment