Java servlet to add data to database

Using servlet you can create client server application that can run over the wide area network such as the online reservation, online registration, e-commerce and other web application. Java servlet can manipulate data that are stored in database like MySQL. You can create a program or servlet that can add or insert data to database. The sour e code below is an example on how to add data to MySQL, it will serve as reference to the one who wants to learn on how to create web application using servlet.

Today servlet are popular because program or application created in Java servlet are efficient in the sense that the initialization code for a servlet is executed only when the servlet is executed for the first time and the succeeding request coming from client is process by the service method. Program or application created in servlet are also robust because Java has exception handling and garbage collection features. Another reason why servlet are popular because program or application created in Java are portable, it can run on different operating system.

To illustrate the example servlet on how to add records or data into MySQL database we need to create first the entry form to be use when making input. Open text editor like notepad and type the source code below.

<html>
<head>
<title>Registration</title>
</head>
<body>
<h1>Registration</h1>
<form action="/servlet/registration.Registration" method="POST">
<table>
<tr>
<td>
First Name:
</td>
<td>
<input type="text" name="fname" size="20">
</td>
</tr>
<tr>
<td>
MI:
</td>
<td>
<input type="text" name="mi" size="20">
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" name="lname" size="20">
</td>
</tr>
<tr>
<td>
Birth Date:
</td>
<td>
<input type="text" name="dbirth" size="20">
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
<input type="text" name="address" size="20">
</td>
</tr>
<tr>
<td>
<strong>Sex:</strong>
</td>
</tr>
<tr>
<td>
<input type="radio" name="sex" value="Male">Male<br>
<input type="radio" name="sex" value="Female">Female<br>
</td>
</tr>
<tr>
<td>
<strong>Civil Status:</strong>
</td>
</tr>
<tr>
<td>
<input type="radio" name="civilstatus" value="Single">Single<br>
<input type="radio" name="civilstatus" value="Married">Married<br>
<input type="radio" name="civilstatus" value="Widow">Widow<br>
</td>
</tr>
<tr colspan="2">
<td>
<input type="submit" name="btnSubmit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>

Save the above code you need first to create a folder and name it as “registration” in the directory: C:\tomcat\webaps\root\registration. The save the code with the file name “registration.jsp”

Using text editor like notepad create a servlet that add the data encoded in the input form. The code is listed below.

package registration;
import java.io.*;
import java.lang.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Registration extends
 HttpServlet{
  public void doPost(HttpServletRequest request, 
HttpServletResponse response)
throws ServletException, IOException{
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  String connectionURL = "jdbc:mysql://localhost/registration";
  Connection connection;
  try{
  
  String strFname = request.getParameter("fname");
  String strMI = request.getParameter("mi");
  String strLname = request.getParameter("lname");
  String strDbirth = request.getParameter("dbirth");
  String strAddress = request.getParameter("address");
  String strSex = request.getParameter("sex");
  String strCivilStatus = request.getParameter("civilstatus");
    
  Class.forName("org.gjt.mm.mysql.Driver");
  connection = DriverManager.getConnection
  (connectionURL, "root", "");
  PreparedStatement pst = connection.prepareStatement
  ("insert into student(fname,mi,lname,birthday,address,sex,civilstatus) values(?,?,?,?,?,?,?)");

  pst.setString(1,strFname);
  pst.setString(2,strMI);
  pst.setString(3,strLname);
  pst.setString(4,strDbirth);
  pst.setString(5,strAddress);
  pst.setString(6,strSex);
  pst.setString(7,strCivilStatus);
  
  int i = pst.executeUpdate();
  if(i!=0){
  pw.println("<br>Record has been inserted");
  pw.println("<br>");
  pw.println("<a href='http://localhost:8081/registration/'>Home</a>");
  }
  else{
  pw.println("failed to insert the data");
  pw.println("<br>");
  pw.println("<a href='http://localhost:8081/registration/'>Home</a>");
  }
  }
  catch (Exception e){
  pw.println(e);
  pw.println("<br>");
  pw.println("<a href='http://localhost:8081/registration/'>Home</a>");
  }
  }
}

To save the source code you need to create a folder name it as “registration” the folder should be created in: C:\tomcat\webapps\root\web-inf\classes\registration. Save the file with a file name: “Registration.java”. You need to compile the java code using java compiler so that you can have a class file. 

Note: You need to create a database in MySQL and name it as “registration” then create a table name it as “student” with the following fields: id(primary_key auto_increment), fname,mi,lname,birthday,address,sex,civilstatus.