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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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

                         

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

                         

» View all related tutorials
Related Tags: c database security web com rest ant website data io connection user password word uri name using this tab check

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

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

i got error which i past here during the runing the program Servlet to add the data into database

ERROR is
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.client_jsp._jspService(client_jsp.java:112)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3074)
sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
java.sql.DriverManager.getConnection(DriverManager.java:525)
java.sql.DriverManager.getConnection(DriverManager.java:171)
org.apache.jsp.client_jsp._jspService(client_jsp.java:56)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.0.28

Posted by sunil on Friday, 12.12.08 @ 03:55am | #82677

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
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.