Share on Google+Share on Google+

Servlet to add the data into database

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

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:

<html>
<head>
<title>New Page 1</title>
</head>
<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>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="Submit" name="B1"></p>
</form>
</body>
</html>

DataInsertionIntoDatabase .java

package myservlets;
import java.io.*;
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{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://localhost/jsp";
Connection connection;
try{
String id = request.getParameter("id");
String uname = request.getParameter("uname");
String password = request.getParameter("password");
pw.println(uname);
pw.println(password);
Class.forName("org.gjt.mm.mysql.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "root");
PreparedStatement pst = connection.prepareStatement("insert into login values(?,?,?)");
pst.setString(1,id);
pst.setString(2,uname);
pst.setString(3,password);
int i = pst.executeUpdate();
if(i!=0){
pw.println("<br>Date has been inserted in to Datebase");
}
else{
pw.println("failed to insert the data");
}
}
catch (Exception e){
pw.println(e);
}
}
}

web.xml file for this program:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">
<description>
Servlet to add the data into database
</description>
<display-name>Servlet to add the data into database</display-name>
<context-param>
<param-name>User</param-name>
<param-value>RoseIndia</param-value>
</context-param>
<servlet-name>DataInsertionIntoDatabase</servlet-name>
<servlet-class>myservlets.DataInsertionIntoDatabase</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataInsertionIntoDatabase</servlet-name>
<url-pattern>/DataInsertionIntoDatabase</url-pattern>
</servlet-mapping>
</web-app>

The output of the program is given below:

This is the output of the above input.

Download Source Code

Advertisements

Advertisement

Posted on: July 8, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Servlet to add the data into database   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0