Servlet to add the data into database

In this program we are going to insert the data in the database table from html form.

Servlet to add the data into database

Servlet to add the data into database


In this program we are going to insert the data in the database table from a html form. 

This servlet program works with HTML form in which there are two fields one is for name and the other one is for entering  password. 
In HTML form there is a submit button, clicking on which the values will be passed to the server.

The data entered from the HTML form will be retrieved by the server side program i.e. servlet. 

For servlet program we first need to make a class named as
DataInsertionIntoDatabase that extends the abstract HttpServlet class, By the name of the class other person can understand what the program is going to perform.

The servlet code written inside the doGet() method takes two arguments, first is HttpServletRequest and the second one is the HttpServletResponse, this method can throw ServletException. This method calls the getWriter() method of  PrintWriter class. 

The data can be inserted in the database that reacquired connection between  database and  java program.
Database connection between database and the java program need the method forName() that is static in nature, this takes one argument which informs about the database driver that  is to be used. Now work of static method getConnection() that is from DriverManager class. This method returns the connection object. 

The method preparedStatement() obtained from the Connection object, returns the PreparedStatement object. It procces the SQL query.  HTML form passes the input to be set in the database with help of setString() method.

In the case if  data is inserted successfully in the table then the output looks like this "Data has been inserted" otherwise "
failed to insert the data".

The code of the program is given below:

<title>New Page 1</title>
<body bgcolor="#999966">
<form method="POST" action="/userregister/DataInsertionIntoDatabase">
<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.txt"
S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<% int count= 0;%>
<input type="hidden" name="id" value="count">
<p>Enter Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" 
name="uname" size="20"></p>
<p>Enter Password: <input type="text" name="password" size="20"></p>
<input type="submit" value="Submit" name="B1"></p>

DataInsertionIntoDatabase .java

package myservlets;
import java.lang.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DataInsertionIntoDatabase extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://localhost/jsp";
Connection connection;
String id = request.getParameter("id");
String uname = request.getParameter("uname");
String password = request.getParameter("password");
connection = DriverManager.getConnection(connectionURL, "root", "root");
PreparedStatement pst = connection.prepareStatement("insert into login values(?,?,?)");
int i = pst.executeUpdate();
pw.println("<br>Date has been inserted in to Datebase");
pw.println("failed to insert the data");
catch (Exception e){

web.xml file for this program:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns=""
Servlet to add the data into database
<display-name>Servlet to add the data into database</display-name>

The output of the program is given below:

This is the output of the above input.

Download Source Code