.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Display corresponding records of employee table after choosing the job from combobox using servlet

Posted By:satyapriyanayak       Posted Date: June 30, 2014    Points: 40    Category: ADO.NET    URL: http://www.dotnetspark.com  

This servlet will show job to user through a combobox and display the corresponding records of employee table in tabular structure after choosing the job.
 

First create a employee table in oracle and insert some data as below.

create table employee(empid varchar(10),empname varchar(10),sal int,job varchar(10))
insert into employee values('e001','raj',10000,'clerk')
insert into employee values('e002','harry',20000,'player')
insert into employee values('e003','sunil',30000,'actor')
insert into employee values('e004','pollock',40000,'artist')
insert into employee values('e005','jonty',50000,'manager')
insert into employee values('e006','kallis',60000,'producer')
insert into employee values('e007','richard',70000,'director')


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 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

import  javax.servlet.*;
import  javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class dynatable extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
 PrintWriter out=res.getWriter();
out.println("<html><body>");
out.println("<form name='f1'>");
try
  {
  String st=req.getParameter("s1");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydsn","system","pintu");
Statement stmt=con.createStatement();
Statement stmt1=con.createStatement();
ResultSet rs=stmt.executeQuery("select distinct job from employee");
out.println("<h1>Job<select name='s1' onChange='document.f1.submit()'>");
out.println("<option>SELECT </option>");
while(rs.next())
   {
 String x=rs.getString("job");
 out.println("<option ");
if(st !=null && st.equals(x))
out.println(" selected");
out.println(">"+x+"</option>");
}
out.println("</select>");
 if(st !=null){
out.println("<h1 align='center'>Employee Info</h1>");
out.println("<table border='1' height='50%' width='50%'>");
ResultSet rs1=stmt1.executeQuery("Select * from  employee where job='"+st+"' ");
out.println("<tr><th>Empid</th><th>Empname</th><th>Salary</th></tr>");
while(rs1.next())
  {
  out.println("<tr>");
  String id=rs1.getString("empid");
  String name=rs1.getString("empname");
  int sal=rs1.getInt("sal");
 out.println("<td>"+id+"</td>");
 out.println("<td>"+name+"</td>");
 out.println("<td>"+sal+"</td>");
   out.println("</tr>");
  }
out.println("</table>");
  }//end of null checking if
 }//end of try
  catch(Exception e){System.out.println(e);}
    out.println("</body></html>");
  }
}

web.xml setting:-

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  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

      http://www.apache.org/licenses/LICENSE-2.0

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

 
    <servlet>
                    <servlet-name>dynatable</servlet-name>
                    <servlet-class>dynatable</servlet-class>
    </servlet
    
    <servlet-mapping>
                    <servlet-name>dynatable</servlet-name>
                    <url-pattern>/dynatable</url-pattern>
    </servlet-mapping>

</web-app>


Compile

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

 

Running the servlet in web browser

First run the tomcat 6.0

http://localhost:8081/javaservlet/ dynatable



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend