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:
- userlogin table
- index.html file
- FetchData.java file
- 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:
insert into userlogin values ('Sandeep', 'sandeep', '[email protected]', 'India');
insert into userlogin values ('Rahul', 'rahul', '[email protected]', 'India');
For creating manually
To manually create a table use the following link in which I show a demo.
http://www.c-sharpcorner.com/UploadFile/fd0172/registration-form-using-servlet-in-java/
Creating other files
We need to use the following procedure to create the other files.
Step 1
Open the NetBeans IDE.

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

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.
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;
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");







Join the conversation! Your thoughts help the community grow.