Display Records of Combobox in Java



This servlet will show a job to the user through a ComboBox and display the corresponding records of an 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)
 
 
 
insert into employee values('e001','raj',10000)
 
insert into employee values('e002','harry',20000)
 
insert into employee values('e003','sunil',30000)
 
insert into employee values('e004','pollock',40000)
 
insert into employee values('e005','jonty',50000)
 
insert into employee values('e006','kallis',60000)
 
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 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)

combobox in Java

Running the servlet in web browser

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

combobox in Java


Similar Articles