Display Data From Database Through Servlet And JDBC

Introduction 

 
 
First, create an employee table in Oracle and insert some data as below.
  1. create table employee(empid varchar(10),empname varchar(10),sal int)    
  2.       
  3. insert into employee values('e001','raj',10000)    
  4. insert into employee values('e002','harry',20000)    
  5. insert into employee values('e003','sunil',30000)    
  6. insert into employee values('e004','pollock',40000)    
  7. insert into employee values('e005','jonty',50000)    
  8. insert into employee values('e006','kallis',60000)    
  9. insert into employee values('e007','richard',70000)     

Creation of dsn(database source name)

 
Start-Control Panel- Administrative Tools- Data Sources (ODBC)-go to system DSN tab-click add button-select a driver for which you want to set up a data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.
 
Program to display data from database through servlet and JDBC
  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4. import java.sql.*;  
  5.     
  6. public class display extends HttpServlet  
  7. {    
  8.      public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException 
  9.       {  
  10.          PrintWriter out = res.getWriter();  
  11.          res.setContentType("text/html");  
  12.          out.println("<html><body>");  
  13.          try 
  14.          {  
  15.              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  16.              Connection con = DriverManager.getConnection("jdbc:odbc:mydsn""system""pintu");  
  17.              // Here dsnname- mydsn,user id- system(for oracle 10g),password is pintu.  
  18.              Statement stmt = con.createStatement();  
  19.              ResultSet rs = stmt.executeQuery("select * from employee");  
  20.              out.println("<table border=1 width=50% height=50%>");  
  21.              out.println("<tr><th>EmpId</th><th>EmpName</th><th>Salary</th><tr>");  
  22.              while (rs.next()) 
  23.              {  
  24.                  String n = rs.getString("empid");  
  25.                  String nm = rs.getString("empname");  
  26.                  int s = rs.getInt("sal");   
  27.                  out.println("<tr><td>" + n + "</td><td>" + nm + "</td><td>" + s + "</td></tr>");   
  28.              }  
  29.              out.println("</table>");  
  30.              out.println("</html></body>");  
  31.              con.close();  
  32.             }  
  33.              catch (Exception e) 
  34.             {  
  35.              out.println("error");  
  36.          }  
  37.      }  
  38.  }  
web.xml setting:-
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!--  
  3. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at  
  4.   
  5. http://www.apache.org/licenses/LICENSE-2.0  
  6.   
  7. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.  
  8. -->  
  9.   
  10. <web-app 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_2_5.xsd" version="2.5">  
  11.   
  12.     <servlet>  
  13.         <servlet-name>display</servlet-name>  
  14.         <servlet-class>display</servlet-class>  
  15.     </servlet>  
  16.   
  17.     <servlet-mapping>  
  18.         <servlet-name>display</servlet-name>  
  19.         <url-pattern>/display</url-pattern>  
  20.     </servlet-mapping>  
  21. </web-app>  

Compile

 
 javac -cp servlet-api.jar display.java (for tomcat 6.0)

 servlet api in java 
 

Running the servlet in a web browser

 
First run the tomcat 6.0
 
http://localhost:8081/javaservlet/display

 sevlet in bowser in java


Similar Articles