1

My project is StaffAllocation, and I want to retrieve information from the database. I'm very new and this is my very first project. I created a drop down list retrieving staffnames from one of my table. Now I want to perform a query action to view the details of the selected staffnames from the drop-down list. The following is the coding which i have, which is not correct:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
  <%@ page import="java.sql.*" %>
  <%@ page import="java.io.*" %>
  <%@ page import="java.lang.*" %>
  <%@ page import="javax.servlet.*" %>
  <%@ page import="javax.servlet.http.*" %>
  <%ResultSet resultset =null; %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Staff Details</title>
  </head>
  <BODY>
  <form method=post>
  <h3>Select Stafftype:</h3>
  <p><input type="radio" name="Stafftype" value="Male"> Male</input></p>
  <p><input type="radio" name="Stafftype" value="Female"> Female</input></p>
  <input type="submit" value="Submit">
  </form>
  <%
  try{

    Class.forName("com.mysql.jdbc.Driver");
    Connection connection =      DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation?  user=root&password=success");
    Statement statement = connection.createStatement() ;
    String Stafftype= request.getParameter("Stafftype");
    out.print(Stafftype);
    if(Stafftype.contentEquals("Male")){
    resultset=statement.executeQuery("select * from tblstaffdetails where Stafftype=  'Male'");
    }
    else if(Stafftype.contentEquals("Female")){
        resultset=statement.executeQuery("select * from tblstaffdetails where   Stafftype= 'Female'");
    }
    else
    {
        System.out.println("your coding is wrong");
    }
   %>

    <select> <% while(resultset.next()){ %>
    <option><%= resultset.getString(2)%></option>
   <%} %> 

   <%

   String StaffName= request.getParameter("StaffName"); 
   int staffId;
   String subcode;
   if(StaffName != null) {
   resultset=statement.executeQuery("SELECT a.staffId, a.StaffName, b.subcode FROM tblstaffdetails a LEFT JOIN tblsubhandled b ON a.staffId = b.staffId where StaffName='request.getParameter('StaffName')'");

     }       
     }
    catch(Exception e)
    {
         out.println("wrong entry"+e);
    }
    %>
   <form method = "get">
   <br><br>
   <input name="Submit" type="button" value="Submit">
   </form>
   </body>
   </html>`

Tables:

tblstaffdetails -(1).staffId(2).StaffName(3).Stafftype(male or female)

tblsubhandled - (1).staffId(2).subcode

Arman H
  • 5,488
  • 10
  • 51
  • 76
user2951465
  • 409
  • 3
  • 5
  • 15
  • You have to check this [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) before proceed. – Vinoth Krishnan Nov 04 '13 at 05:28
  • can you please help me how to perform query operation on a selected item from drop down menu – user2951465 Nov 04 '13 at 06:40
  • Yeah sure. What you need is to populate second dropdown based on value from first dropdown, Hence you need to use Ajax for send your value to servlet and populate the response into your second dropdown. Let me know if you need furthur help. – Vinoth Krishnan Nov 04 '13 at 07:00
  • this shows me that i have to learn a lot. can you please post a example coding for performing a query operation on a selected StaffName? – user2951465 Nov 04 '13 at 07:32
  • I already did that, You can find the answer below. – Vinoth Krishnan Nov 04 '13 at 07:35
  • sir, thank you for your kind response. the term ajax is something new to me. i do not have much time, because my project demo is tomorrow. atleast i must show one page, i did not understand what you said :( – user2951465 Nov 04 '13 at 07:42
  • the drop dowm box is generated from my database. what is the purpose of generating second drop down? is there any way to process the selected value in html itself? – user2951465 Nov 04 '13 at 08:32

2 Answers2

0

I have fiddled sample implementation for your reference. You can implement like that. All you need to do is include jQuery plugin. Sample code lke this,

 $.ajax({
    url : 'ur_servlet_url' + selValue,
    type : "POST",
    async : false,
    success : function(data) {
        //Sample data
        var data = "<select id='child'>
                    <option value='11'>Value11</option></select>"
        $("#fillValue").html(data);
    }
});

You need to compose your java response like select tag and return it to your ajax response. Finally you can fill the second dropdown like that. Let me know if this helps.

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • If i understood you correct, you are using the selected first dropdown value for query the database and using the result set you are composing the another dropdown. – Vinoth Krishnan Nov 04 '13 at 08:36
  • i do not want to generate another drop down. i already generated a dropdown from a existing table which consists of staffnames. if a select a staffname from the drop down list, details of that particular staff must be shown, like what subject she is handling, which year, course,.. stuffs lik that.. i just created a sample database and have to perform inner join operation to view that particular staff details i select from the drop down list. – user2951465 Nov 04 '13 at 08:44
  • You can use the same ajax but you should change the response, like what you need. If you need to show the table you can compose the table and put it in that div. Similarly you can do it whatever the output you are expecting. – Vinoth Krishnan Nov 04 '13 at 09:31
0
<form method="post" action="select.jsp">
    <select name="sell">
       <option value="Alto">Alto</option>
       <option value="Esteem">Esteem</option>
       <option value="Honda City">Honda City</option>
       <option value="Chevrolet">Chevrolet</option>
    </select>
    <br>
    <input type="Submit" value="Submit">
</form>
<%
  String st=request.getSelectedIndex("sell");
  if(st!=null){
    out.println("You have selected: "+st);
  }
%>
Govan
  • 7,751
  • 5
  • 26
  • 42