Insert record in database from servlet

Q. Develop a registration page in servlet with proper validation and store all records in the database.

Answer:

In the below example we take all detail from the user and store them on to the database. If user leaves any of the fields blank, then it will not be saved.

Create a "Registration" table

SQL> create table registration (Name varchar2(20), password varchar2(10), email varchar2(20), mobile number, dob varchar2(20), gender varchar2(6), country varchar2 (10));

There are three files in registration page

i. register.html
ii. Register.java
iii. web.xml

register.html

<!doctype html>  
   <body>  
      <form action="servlet/Register" method="post">  
         <fieldset style="width:20%; background-color:#ccffeb">
            <h2 align="center">Registration form</h2><hr>
            <table>
               <tr>
                  <td>Name</td>
                  <td><input type="text" name="userName" required /></td>
               </tr>  
               <tr>
                  <td>Password</td>
                  <td><input type="password" name="userPass" required /></td>
               </tr>  
               <tr>
                  <td>Email Id</td>
                  <td><input type="text" name="userEmail" required /></td>
               </tr>  
               <tr>
                  <td>Mobile</td>
                  <td><input type="text" name="userMobile" required/></td>
               </tr>  
               <tr>
                  <td>Date of Birth</td>
                  <td><input type="date" name="userDOB" required/></td>
               </tr>  
               <tr>
                  <td>Gender</td>
                  <td><input type="radio" name="gender" value="male" checked> Male
                  <input type="radio" name="gender" value="female"> Female </td></tr>
               <tr>
                  <td>Country</td>
                  <td><select name="userCountry" style="width:130px">  
                     <option>Select a country</option>  
                     <option>India</option>  
                     <option>America</option>  
                     <option>England</option>  
                     <option>other</option></select>
                  </td>
               </tr>
               <tr>
                  <td><input type="reset" value="Reset"/></td>
                  <td><input type="submit" value="Register"/></td>
               </tr>
            </table>
         </fieldset>  
      </form>  
   </body>  
</html>


Register.java

import java.io.*;  
import java.sql.*;  
import javax.servlet.ServletException;  
import javax.servlet.http.*;  
  
public class Register extends HttpServlet
{  
     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     {  
          response.setContentType("text/html");  
          PrintWriter out = response.getWriter();  
          
          String name = request.getParameter("userName");  
          String pwd = request.getParameter("userPass");  
          String email = request.getParameter("userEmail");
          int mobile = Integer.parseInt(request.getParameter("userMobile"));
          String dob = request.getParameter("userDOB");  
          String gender = request.getParameter("gender");  
          String country =request.getParameter("userCountry");  
          
          try
          {  
               //load the driver
               Class.forName("oracle.jdbc.driver.OracleDriver");  
               //create connection object
               Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","local","test");  
               // create the prepared statement object
               PreparedStatement ps=con.prepareStatement("insert into registration values(?,?,?,?,?,?,?)");  
  
               ps.setString(1,name);  
               ps.setString(2,pwd);  
               ps.setString(3,email);  
               ps.setInt(4, mobile);
               ps.setString(5,dob);  
               ps.setString(6,gender);  
               ps.setString(7,country);  
  
               int i = ps.executeUpdate();  
               if(i>0)  
               out.print("You are successfully registered...");  
  
          }
          catch (Exception ex)
          {
               ex.printStackTrace();
          }  
          out.close();  
     }  
}


web.xml

<web-app>    
     <servlet>  
          <servlet-name>Register</servlet-name>  
          <servlet-class>Register</servlet-class>  
     </servlet>  
  
     <servlet-mapping>  
          <servlet-name>Register</servlet-name>  
          <url-pattern>/servlet/Register</url-pattern>  
     </servlet-mapping>  
  
     <welcome-file-list>  
          <welcome-file>register.html</welcome-file>  
     </welcome-file-list>    
</web-app>


Output:

This page doesn’t allow to submit the record without filling the textbox.  

registration form

After filling all textbox, it allows to submit the record.

registration form

Click on Register button.

message

Check the record on the database, the textbox value will be inserted