Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Servlets Inserting data from the HTML page to the database

Related Tutorials


 
 

Share on Google+Share on Google+

Inserting data from the HTML page to the database

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

Inserting data from the HTML page to the database

     

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

To make our program working we need to make one html form in which we will have two fields, one is for the name and the other one  is for entering the password. At last we will have the submit form, clicking on which the values will be passed to the server.

The values which we have entered in the Html form will be retrieved by the server side program which we are going to write. To accomplish our goal we first have to make a class named as ServletInsertingDataUsingHtml which must extends the abstract HttpServlet class, the name of the class should be such that the other person can understand what this program is going to perform. The logic of the program will be written inside the doGet() method which takes two arguments, first is HttpServletRequest interface and the second one is the HttpServletResponse interface and this method can throw ServletException.

Inside this method call the getWriter() method of the PrintWriter class. We can insert the data in the database only and only if there is a connectivity between our database and the java program. To establish the connection between our database and the java program we firstly need to call the method forName() which is static in nature of the class Class. It takes one argument which tells about the database driver  we are going to use. Now use the static method getConnection() of the DriverManager class. This method takes three arguments and returns the Connection object. SQL statements are executed and  results are returned within the context of a connection. Now your connection has been established. Now use the method prepareStatement() of the Connection object which will return the PreparedStatement object and takes one a query which we want to fire as its input. The values which we have got from the html will be set in the database by using the setString() method of the PreparedStatement object.

If the record will get inserted in the table then output will show "record has been inserted"  otherwise "sorry! Failure".

The code of the program is given below:

<html>
<head>
<title>New Page 1</title>
</head>
<body>
<form method="POST" action="/InDataByHtml/ServletInsertingDataUsingHtml">
<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.txt"
  S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
  <p>Enter Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" 
   
name="username" 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;

  <input type="submit" value="Submit" name="B1"></p>
</form>

</body>

</html>

 ServletInsertingDataUsingHtml.java

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

public class ServletInsertingDataUsingHtml 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/zulfiqar";
  Connection connection;
  try{
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  pw.println(username);
  pw.println(password);
  Class.forName("org.gjt.mm.mysql.Driver");
  connection = DriverManager.getConnection
  (connectionURL, 
"root""admin");
  PreparedStatement pst = connection.prepareStatement
  (
"insert into emp_info values(?,?)");
  pst.setString(1,username);
  pst.setString(2,password);
  int i = pst.executeUpdate();
  if(i!=0){
  pw.println("<br>Record has been inserted");
  }
  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"?>
<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
 <servlet>
 <servlet-name>Zulfiqar</servlet-name>
 <servlet-class>ServletInsertingDataUsingHtml</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Zulfiqar</servlet-name>
 <url-pattern>/ServletInsertingDataUsingHtml</url-pattern>
 </servlet-mapping>
</web-app>

The output of the program is given below:

This is the output of the above input.

Download this example:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: July 17, 2007

Related Tutorials

Discuss: Inserting data from the HTML page to the database   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
lax
June 22, 2011
please..! help me

what if u have no idea about javaServlet? what ineed here is how to insert data from the HTML page to the database...WITHOUT USING JAVASERVLET.
suresh
May 25, 2012
i have aproblem

how to store data in MSACCESS DATABASE TABLE using HTML WITH JAVA script....?
DrGreen
March 23, 2013
HTTP 404

I put the servlet in my project accordingly I modified the index.jsp by pasting the html code replacing the default. Also I modified the web.xml accordingly. It builds and deploys well, with no errors. When I run index.jsp after completing the form and clicking submit it gives me an HTTP 404 error like this : HTTP Status 404 - type Status report message descriptionThe requested resource () is not available. GlassFish Server Open Source Edition 3.1.2.2
DMCA.com