Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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;&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:

                         

» View all related tutorials
Related Tags: c ide orm table data form io make type column field name columns change this id nsis war tab row

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

"/InDataByHtml/ServletInsertingDataUsingHtml"
why this path will be given in this page

Posted by jyothi on Wednesday, 05.21.08 @ 14:34pm | #60582

i am a java developer...i am doing one project on servelts....to ..database...i had taken one html form ...the data will be inserted into the database...if any record is modified or deleted the details will also be displayed....please send the code...

Posted by phani on Tuesday, 12.18.07 @ 16:45pm | #42831

The above code does not consider to close the database connection, it would be good practise to write in finally block to close the connection.

Posted by srikanth on Tuesday, 08.14.07 @ 17:23pm | #23367

I tried this with access database I given proper driver name :

But it saying no default driver found

please help me as soon as

Posted by kiruba on Tuesday, 07.31.07 @ 12:10pm | #22285

Hi
Dears
I am a java Developer i have stucked during my project how to write a progarm for password checking in core java
the conditions are:
1.password must contain combination of lowercase letters & numerical digits
EX:abcd123,krishna1(right)... krishna, snake(wrong).
2.Password length b/w 5&12.
3.password must not be contain a seuence of same letters.
Ex:ramuu122,rraamu123,raraga12,sadsad1,(wrong), ramu123,catalyst1,sequence23(right)

we have to write a program for this code in java only if anyone knows please help me and send the code to my mail address:
krishnamurphybtech@gmail.com

Thank u friends eagarly waiting for ur replies..

regards,
krishnamurthy.

Posted by krishnamurthy on Friday, 06.22.07 @ 18:52pm | #19973

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.