Sei sulla pagina 1di 31

A Lab Project Report on

Fetch Records From Database Using Servlet


Submitted to the Dept. of Information Technology, SNIST

Ms. Shaik Reshma (12311A1263)


Mr. Thogiti Sai Teja (12311A1273)
Mr. Shahul Shaik(12311A1283)
Mr. Nalla Sravan Kumar (12311A1293)
Mr. Dhavili Vamshi Krishna (12311A12A3)
Ms. Anitha Silveru (12311A12B3)

Under the guidance of

Mr. K. SREE RAMA MURTHY


Associate Professor
SNIST

Department of Information Technology


School of Computer Science and Informatics

SreeNidhi Institute of Science and Technology


Yamnampet, Ghatkesar Mandal, R.R Dist., Hyderabad 501 301

1

Introduction
This article explains how to fetch data from a database using a Servlet in Java.
The NetBeans IDE is used for this application.
Fetch Data from Database
This article explains how to fetch data from a database using a servlet in Java.
For this application we need the following tools:

Oracle10g Database
Tomcat Server
NetBeans IDE
We need to create the following files:
1. userlogin table
2. index.html file
3. FetchData.java file
4. web.xml file
1. userlogin table
For fetching data you need to have a table with multiple records. In this
application I use a "userlogin" table syntax for creating this table as in the
following:
create table userlogin(name varchar2(4000), password varchar2(4000),
emailid varchar2(4000), country varchar2(4000));
To insert data the syntax is:
insert into userlogin values ('', '', '', );
For example I inserted two rows using:
2

insert into userlogin values ('Shahul', 'ahmed', 'shahul@gmail.com', 'India');


insert into userlogin values ('Reshma', 'shaik', 'reshma@gmail.com', 'India');
For creating manually
We need a table to store user information in the database. In this "registration
form" we create a table named "userlogin".
Syntax for table:
create table userlogin(name varchar2(4000), password varchar2(4000),
emailid varchar2(4000), country varchar2(4000));
Or use the following procedure.
Step 1
Open the database home page and login to your account as in the following:

3

Step 2
Now click on "Object Browser" and choose "Create" -> "Table" as in the
following:

4

Step 3
Now type the following details of your table (as table name and their column
names) as in the following:

5

Step 4
Now click on "Next" until you reach the create page, then your table is created
as in the following:

Now to create the other files:

index.html
SignUpServlet.java
web.xml

6

We need to use the following procedure for this.


Step 5
Open the NetBeans IDE.

7

Step 6
Choose "Java web" -> "Web project" as in the following:

8

Step 7
Now type your project name as "RegistrationForm" as in the following:

9

Step 8
Click on "Next" and choose your server wizard; I selected Tomcat7 as in the
following:.

10

Step 9
Now delete your default "index.jsp" file and create a new "index.html" file
and write the following code there.
index.html
<!DOCTYPE html>
<html>
<head>
<title>User Registration Form</title>
</head>
<body bgcolor="pink">
<center><h1>Registration Form</h1></center><br>
<form method="post" action="SignUpServlet">
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="uname">
</td>
</tr>
<td>
Create Password:
</td>
<td>
11

<input type="password" name="upass">


</td>
<tr>
<td>
Confirm Password:
</td>
<td>
<input type="password" name="upass">
</td>
</tr>
<tr>
<td>
Email-Id:
</td>
<td>
<input type="text" name="uemailId">
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<select name="ucountry">
<option>India </option>
<option>Pakistan</option>
<option>Bangladesh</option>
<option>japan</option>
12

<option>Canada</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Login">
</td>
<td>
<input type="reset" value="reset">
</td>
</tr>
</table>
</form>
</body>
</html>

Step 10
Now create a servlet file with the name "SignUpServlet" and provide the
following code for it.
SignUpServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
13

import java.sql.*;
public class SignUpServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("uname");
String p = request.getParameter("upass");
String e = request.getParameter("uemailId");
String c = request.getParameter("ucountry");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"sandeep", "welcome");
PreparedStatement stmt = con.prepareStatement("insert into userlogin
values(?,?,?,?)");
stmt.setString(1, n);
stmt.setString(2, p);
stmt.setString(3, e);
stmt.setString(4, c);
int i = stmt.executeUpdate();
if (i > 0) {
out.println("You are successfully registered.....");
}
} catch (Exception ey) {
System.out.println(ey);
}
14

out.close();
}
}
Step 11
Now ensure that the code of your web.xml file is the same as:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/
xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>SignUpServlet</servlet-name>
<servlet-class>SignUpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SignUpServlet</servlet-name>
<url-pattern>/SignUpServlet</url-pattern>
</servlet-mapping>
</web-app>
Step 12
Now your project is ready.
15

Right-click on the project menu then select "Run" as in the following:

16

The following output is generated.

17

Now fill in the detail as shown.

Click on "Reset" if you think you would pass some incorrect data. The demo
of the reset is shown below. Since here I typed the wrong emailid, now I click
on the Reset button.

18

Fill in the correct detail. I filled in the following details.

19

Now click on "Finish"; the "SignUpServlet" is showing the following


message.

20

Note
Now your data is saved in the database you created at the beginning of this
article.
To see the data in the database,
open the database home page and
go to "Object Browser" -> "View" -> "Table". Select your table as "userlogin".
Now click on "data" that is just shown up over the table.

21

Creating other files


We need to use the following procedure to create the other files.
Step 1
Open the NetBeans IDE.

22

Step 2
Choose "Java web" -> "Web application" as in the following:

23

Step 3
Type your project name as "Welcome" and click on "Finish" as in the
following:

Step 4
Now delete your default "index.jsp" file and create a new "index.html" file
and write the following code there.

24

index.html
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="Search">
Enter your Name: <input type="text" name="uname"/><br/>
<input type="submit" value="search"/>
</form>
</body>
</html>
Step 5
Now create a servlet file with the name "Search" and write the following code
there.
Search.java
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
25

import javax.servlet.http.*;
public class Search extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name=request.getParameter("uname");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:
xe","sandeep","welcome");
PreparedStatement ps=con.prepareStatement("select * from
userlogin where name=?");
ps.setString(1,name);
out.print("<table width=25% border=1>");
out.print("<center><h1>Result:</h1></center>");
ResultSet rs=ps.executeQuery();
/* Printing column names */
ResultSetMetaData rsmd=rs.getMetaData();
while(rs.next())
{
out.print("<tr>");
out.print("<td>"+rsmd.getColumnName(1)+"</td>");
out.print("<td>"+rs.getString(1)+"</td></tr>");
out.print("<tr><td>"+rsmd.getColumnName(2)+"</td>");
out.print("<td>"+rs.getString(2)+"</td></tr>");
out.print("<tr><td>"+rsmd.getColumnName(3)+"</td>");
26

out.print("<td>"+rs.getString(3)+"</td></tr>");
out.print("<tr><td>"+rsmd.getColumnName(4)+"</td>");
out.print("<td>"+rs.getString(4)+"</td></tr>");
}
out.print("</table>");
}catch (Exception e2)
{
e2.printStackTrace();
}
finally{out.close();
}
}
}
Step 6
Check your default web.xml file that it has the same code as in the following:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/
xml/ns/javaee/web-app_3_0.xsd">
<servlet>
27

<servlet-name>Search</servlet-name>
<servlet-class>Search</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Search</servlet-name>
<url-pattern>/Search</url-pattern>
</servlet-mapping>
</web-app>
Step 7
Now our project is ready to run. Right-click on the "Project" menu then select
"Run" as in the following:

28

Step 8
The following output is generated.

Step 9
Now type the name as you pass in your database file. For example I passed the
name "Sandeep" since it exists in my database.

29

Step 10
Now click on the "Search" button. The data from the database is fetched with
the username Sandeep as in the following:

Step 11
Now click on the back button of the browser and provide another name. Since
I have two records in the database I passed the other one as the name "Rahul".

30

Step 12
Now click on "Search".

31

Potrebbero piacerti anche