Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Retrieval of Dropdown list

Have Programming Question? Ask it here!
 
 
 


Prakash
Retrieval of Dropdown list
2 Answer(s)      2 years ago
Posted in : Java Beginners


Hi frnds... Am having problem with the below code... I have retrieved data from the mysql database into dropdownlist ... for eg, let A1,A2,A3,A4 be the values in dropdown.. if i click A1, the corresponding value of A1, list of name corresponding to A1 wil be displayed in another dropdown.. Am not able to retrive the name for further process... Pls check the below code and help me frnds.. If this code is wrong, pls help me with the proper coding... its urgent...

    Update.jsp:

             <script language="javascript" type="text/javascript">  
                            var xmlHttp  
                            var xmlHttp
                            function showEmp(str){
                                if (typeof XMLHttpRequest != "undefined"){
                                    xmlHttp= new XMLHttpRequest();
                                }
                                else if (window.ActiveXObject){
                                    xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
                                }
                                if (xmlHttp==null){
                                    alert("Browser does not support XMLHTTP Request")
                                    return;
                                } 
                                var url="SetUpdate.jsp";
                                url +="?groupname=" +str;
                                xmlHttp.onreadystatechange = stateChange;
                                xmlHttp.open("GET", url, true);
                                xmlHttp.send(null);
                            }

                            function stateChange(){   
                                if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
                                    document.getElementById("name").innerHTML=xmlHttp.responseText   
                                }   
                            }
                        </script>  


                                <table align="center" cellspacing="0">    

                                    <tr><td>Select Group:
                                    <select name='groupname' onchange="showEmp(this.value)">  
                                        <option value="none">Select</option>  
                                        <%
                                            Class.forName("com.mysql.jdbc.Driver").newInstance();
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/chitfund", "root", "root");
        Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery("Select * from groupdetail");
             while (rs.next()) {
                      %>
 <option value="<%=rs.getString("groupname")%>"><%=rs.getString("groupname")%></option>  
                                        <%
                                            }
                                        %>
                                    </select> 
                                    <br>  
                                    <div id='name'>  
                                            <select name='name' >  
                                            <option value='-1'></option>  
                                        </select>  
                                    </div>  </td> </tr>


    SetUpdate.jsp:

    <%@page import="java.sql.*"%>
    <%
       String groupname = request.getParameter("groupname");
       String buffer = "<select name='name' ><option value='-1'>Select</option>";
        try {
         Class.forName("com.mysql.jdbc.Driver").newInstance();
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/chitfund", "root", "root");
        Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("Select * from memberdetail where groupname='" + groupname + "' ");
                    while (rs.next()) {
                        buffer = buffer + "<option value='" + rs.getString(1) + "'>" + rs.getString("name") + "</option>";
                    }
                    buffer = buffer + "</select>";
                    response.getWriter().println(buffer);
                } catch (Exception e) {
                    System.out.println(e);
                }
    %>

Advertisement
View Answers

August 28, 2012 at 4:14 PM


Here is a jsp application that retrieves data from the database and stored into first dropdown. If user click any option from the dropdown, the corresponding values will get displayed into another dropdown.

1)country.jsp:

<%@page import="java.sql.*"%>
 <html>
      <head>  
      <script language="javascript" type="text/javascript">  
      var xmlHttp  
      var xmlHttp
      function showState(str){
      xmlHttp=GetXmlHttpObject();
      if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
      var url="state.jsp";
      url +="?count=" +str;
      xmlHttp.onreadystatechange = stateChange;
      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);
      }

      function stateChange(){   
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
      document.getElementById("state").innerHTML=xmlHttp.responseText   
      }   
      }

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
      </script>  
      </head>  
      <body>  
      <select name='country' onchange="showState(this.value)">  
      <option value="none">Select</option>  
    <%
 Class.forName("com.mysql.jdbc.Driver").newInstance();  
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from country");
 while(rs.next()){
     %>
      <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>  
      <%
 }
     %>
      </select>  
      <br>  
      <div id='state'>  
      <select name='state' >  
      <option value='-1'></option>  
      </select>  
      </div>  
      </body> 
      </html>

2)state.jsp:

<%@page import="java.sql.*"%>
<%
String country=request.getParameter("count");  
 String buffer="<select name='state' ><option value='-1'>Select</option>";  
 try{
 Class.forName("com.mysql.jdbc.Driver").newInstance();  
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from state where countryid='"+country+"' ");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>";  
   }  
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer); 
 }
 catch(Exception e){
     System.out.println(e);
 }

 %>

For the above code, we have created two database tables:

CREATE TABLE `country` (                                 
           `countryid` bigint(255) NOT NULL auto_increment,       
           `countryname` varchar(255) default NULL,               
           PRIMARY KEY  (`countryid`)); 



CREATE TABLE `state` (                                   
          `stateid` bigint(255) NOT NULL auto_increment,         
          `countryid` int(255) default NULL,                     
          `state` varchar(255) default NULL,                     
          PRIMARY KEY  (`stateid`));


August 28, 2012 at 5:03 PM


Thank u.. its working...



Related Tutorials/Questions & Answers:
 

 

 

DMCA.com