jsp- database dependent dropdown list

jsp- database dependent dropdown list

i want 2 dropdown list 1- CLASS 2-SECTION both are should come from database. and if i select a class suppose class a the corresponding section dropdown list should retrieve the section respective to that class from database. please help by providing the code in jsp as soon as possible.its urgetnt.

View Answers

May 31, 2013 at 4:49 PM

thank you sir, but its nt working in my pc and that to i am nt aware of the ajax so please send me the same project in jsp


October 27, 2011 at 11:28 AM

1)country.jsp:

<%@page import="java.sql.*"%>
 <html>
      <head>  
      <script language="javascript" type="text/javascript">  
      var xmlHttp  
      var xmlHttp
      function showState(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="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   
      }   
      }
      </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`));

November 1, 2011 at 11:45 AM

Hai friends I have a doubt in above code that How can we get those selected values into our SERVLET.

I mean i could display the values dynamaically but while submitting the form,the selected values getting NULL.

I replaced the code in above

";

as

";

still i could not...please provide solution


November 1, 2011 at 11:54 AM

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<html>

<script>

function change(){
var cid=document.getElementById("books").selectedIndex;
var val = document.getElementById("books").options[cid].text;
window.location.replace("http://localhost:8080/faculty/book1.jsp?id="+cid+"&&value="+val);
}
function extract(){
var ide=document.getElementById("info").selectedIndex;
var bookname = document.getElementById("info").options[ide].text;

window.location.replace("http://localhost:8080/faculty/book1.jsp?book="+bookname);
}
function extractsub(){
var sde=document.getElementById("subject").selectedIndex;
var subject = document.getElementById("subject").options[sde].text;

window.location.replace("http://localhost:8080/faculty/book1.jsp?subject="+subject);
}
function enableDisable() {
  if(document.book1.holiday.checked){
     document.book1.txt.disabled = false;
} else {
document.book1.txt.disabled = true;
}
  }
</script>
    <input type="text" name="txt">
    <form name="book1" action="try.jsp" method="post" >
<%!
Connection conn = null;
ResultSet rs =null;
Statement st=null;
String query="";

%>
<%
String value=request.getParameter("value");
Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/newdatabase","root","root"); 
st = con.createStatement();
rs = st.executeQuery("select * from book");
%>
<select id="books" name="books" onchange="change();">
<option value="0">--Please Select--</option>
<% 
while(rs.next())
{
%>
<option value="<%=rs.getString("books")%>"><%=rs.getString("books")%></option>
<% 
if(rs.getString("books").equals(value))
{

%>
<option value="<%=value%>" selected disabled><%=value%></option>
<%
}
}
%>
</select>
<select id="info" name ="info">
<option value="0">--Please Select--</option>
<%
String id=request.getParameter("id");
rs=st.executeQuery("select * from bookinformation where bookid='"+id+"'");
while(rs.next())
{
%>
<option value="<%=rs.getString("id")%>" ><%=rs.getString("booknames")%></option>
<%
}
%>
</select>
HOLIDAY<input type ="checkbox" name="holiday" value="HOLIDAY" onclick="radio.disable=!this.checked">
<br><h3>SUBJECTS</h3>
<table>
  <%
String sid=request.getParameter("id");

    rs=st.executeQuery("select * from subject where id='"+sid+"'");


 while(rs.next())
           {

%>

<tr><td><%=rs.getString("subject")%></td><td><input type="radio" value="<%=rs.getString("subject")%>" name="subject" onchange="extractsub();"></td>

    <%
       }
    %>


</tr>


</table>
        <select id="faculty_name" name="faculty_name">
<option value="0">--Please Select--</option>
<%
String faculty_name=request.getParameter("faculty_name");
rs=st.executeQuery("select * from faculty_name ");
while(rs.next())
{
        %>
      <option value="<%=rs.getString("faculty_name")%>"><%=rs.getString("faculty_name")%></option>
           <%
                     }
        %>
    </select>
    <input type="submit" value="SUBMIT">
    </form>

</body>
</html>

i have included this coding my project. this coding will create 2 dropdown and dependent on each other. and u'll get a list of subjects in radio button according to the selection made in first drop down list.









Related Tutorials/Questions & Answers:
jsp- database dependent dropdown list
jsp- database dependent dropdown list   i want 2 dropdown list 1... suppose class a the corresponding section dropdown list should retrieve the section respective to that class from database. please help by providing the code in jsp
3 dropdown list from the database using JSP
3 dropdown list from the database using JSP  Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other and get the options from the database using JSP
Advertisements
getting and storing dropdown list in database in jsp
getting and storing dropdown list in database in jsp  i have a drop down list to select book from database. i'm able to retrieve dropdown list from database. but unable to store the selected value in database table. please help
how to add data dynamically from database into a dropdown list in a jsp
how to add data dynamically from database into a dropdown list in a jsp  hi friends i am doing a project in jsp and oracle as my database.In my... in storing them into the database,this registration form contains name
dropdown list in jsp
dropdown list in jsp  hai, i have static dropdown list.. i want to get the selected value in string variable without jsp regards asha
JSP Get Data Into Dropdown list From Database
JSP Get Data Into Dropdown list From Database In this section we will discuss about how to fetch data dynamically into the dropdown list in JSP... for fetching data from the database and set it into the dropdown list in JSP
Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other and get the options from the database using JSP
Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other... to create 3 drop dropdown list each depend on the other and get the options from the database using JSP. like country,state,city..please guide me
jsp -sevlet connecting to database using dropdown
jsp -sevlet connecting to database using dropdown  How can I get my dropdown list from oracle database and then submit it to another table in JSP. I... to the database and fetches an array of strings from a database table and then sends
Inserting values into a database table of selected DropDown in jsp.
Inserting values into a database table of selected DropDown in jsp.  http://www.roseindia.net/answers/viewqa/Ajax/15250-DropDown-in-ajax+jsp.html Thank you for this program, it helped me alot to learn. I have done it. now i want
Populate dropdown menu from database using jsp and servlet
Populate dropdown menu from database using jsp and servlet  please i need code to populate dropdown menu from mysql database using jsp and servlet. thanks
how to use dropdown list in JSP and display value in same page
how to use dropdown list in JSP and display value in same page  I have a Dropdown list with some values say "A", "B" and "C" When the user selects... in the same page Value selected = C How can we do this in a JSP page
DropDown list
DropDown list  how to get mysql database values into dropdown usign java servlet and ajax?   Here is a jsp code that displays the database values into dropdown list. 1)country.jsp: <%@page import="java.sql.*"%>
How to access Contacts from gmail using LDAP in JSP dropdown list
How to access Contacts from gmail using LDAP in JSP dropdown list  HI, I am creating a JSP page in which i have to make a dropdown box, and access gmail contacts in that drop downlist using LDAP. Can any one plz help me out
inserting dropdown values into database table
inserting dropdown values into database table   hi i want to insert dropdown values into a database table by using jsp
Want code for 5 dependent dropdown in php
Want code for 5 dependent dropdown in php  I have 5 dds, if i select value from first one then related values are displayed in 2 dd and so on .plz help   Please go through the following link: Visit Here The above link
Dependent drop down list
Dependent drop down list  hi,i am trying to design a form on which 2 dependent drop down list is used my code is successful but when i select class... is possible to manage four dropdown dependnt list? Thanks a lot. regards Nico
struts dropdown list
struts dropdown list   In strtus how to set the dropdown list values from database ? I have a ArrayList object and set this object in dropdown jsp page using struts ? please send me jsp code... sample code: **Action
store dropdown box values in database server
store dropdown box values in database server  how to store dropdown box values in database server in jsp
jsp dropdown refresh - JSP-Servlet
jsp dropdown refresh  Sir Thank u for your jsp code for dependent dropdown list, i used it in different table for data extraction but the probleam during data extraction when i select first drop down its refresh then 2nd
jsp dropdown - JSP-Servlet
jsp dropdown  print dropdown selected value in same page without refreshing page in jsp....   Hi Friend, Try the following code: 1... to use this textbox value to fetch values from database
jsp dropdown - JSP-Servlet
jsp dropdown  in my table there are 3 fields named orderid ,itemname, itemqty. i want code that fetch orderid in dropdown... on select dropdown get all items list of that orderid... there are multiple items at one orderid
creating dropdown lists in jsp
creating dropdown lists in jsp  i want to create two dropdown list which are dependent that is the first box choice have to evaluate the second boxs options
how can i send a mail to a particular user from a many user dropdown list in jsp
how can i send a mail to a particular user from a many user dropdown list in jsp  how can i a sent a user question to a particular person from a drop down list in jsp
retrive the data from access database to drop down list box in jsp
retrive the data from access database to drop down list box in jsp  hai, im new to jsp now im using the jsp along with access database.in table i load all the data's i need to retrive the data from database to dropdown list box
dropdown list in jsf
dropdown list in jsf  I want to add a list box to display the country... then in the second list box it will display the states related to India only and the flow... with and without using database
JSP Dependent Drop Down Menu
JSP Dependent Drop Down Menu  Hey Guy/Gals! I need someone help...; The given code will help you to create a dependent dropdown box. 1... menu and it's selection. So in my database I have a category table that has a ID
Dropdown Checkbox list
Dropdown Checkbox list This tutorial explains how to create jquery Dropdown Checkbox list in JSP.  This example is created in eclipse IDE and run... jquery Dropdown Checkbox list. The application directory structure should look like
Retrieval of Dropdown list
Retrieval of Dropdown list  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
checkbox with dropdown listbox in jsp
checkbox with dropdown listbox in jsp  i loaded the the data into the drop down listbox in jsp from access database.now i have to add a check box,if it checked means dropdownlist box carryout the data's,else it should
dropdown list and text fields in php
dropdown list and text fields in php  How could I use php to populate text fields by selecting a name of a business from dropdown list? Those text... number, description, and opening times from a mysql database. If you can, post
getting values from dropdown list
getting values from dropdown list  I am having a dropdown list which has hardcoded values ( we need it hardcoded only and will not be populated from the database) My question is when i select a particular value it should be pass
dropdown - JSP-Servlet
dropdown  i want to use dropdown select value in query-string in same page.. Using selected 'name' want to fetch details of that person from database.. without using Ajax...  Hi Friend, Try the following code
DropDown in ajax+jsp
DropDown in ajax+jsp  I have four dropdown if i select first dd...(Exception e){ System.out.println(e); } %> We have created 3 dependent dropdown. You can create the fourth one similarly. For the above code, we have
dropdown - JSP-Servlet
dropdown  i want to use dropdown select value in query-string in same page.. Using selected 'name' want to fetch details of that person from database..  Hi Friend, Try the following code: 1)ajaxExample.jsp
Dropdown code to retrieve result from oracle database
that will retrieve values from the database into dropdown. As the user choose any option...Dropdown code to retrieve result from oracle database  Hi Friends, I... contains dropdowns in frontend(Javascript or JSP).If i choose an option,it should
dropdown - JSP-Servlet
dropdown  i have below code for dropdown . now i want to user that textbox value in query at same page... how to get it in a new variable??? function getValue(){ var combo = document.getElementById("sel"); var val
dependent drop down box - JSP-Servlet
dependent drop down box  haloo sir Please give me the solution how to extract data from dependent drop down box with page refresh...; window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?id="+cid+"&&value
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected
creating list in dropdown using struts - Struts
creating list in dropdown using struts   creating list in dropdown using struts : In action class list.add(new LabelValueBean("ID","Name")); In Jsp * Select Item Select In Form : getter
dropdown box
dropdown box  i need to have country,state and city in drop down box using ajax and use db2 database   Have a look at the following link: JSP dependent dropdown
displaying List of records from database in a jsp using ajax
displaying List of records from database in a jsp using ajax  Sir, I need to retrieve the records from the database for every 7 seconds and display...;%@page import="com.tbss.RtChannels"%><%@ taglib uri="http://java.sun.com/jsp
jsp database
jsp database   I want to retrive value in my jsp page but i dont want show my database query in my jsp page. is there any other method to show value in my jsp page using java class
how to load value in dropdown list after selecting value from first dropdown list using javascript
how to load value in dropdown list after selecting value from first dropdown list using javascript  how to load value in dropdown list after selecting value from first dropdown list using javascript
retrieve related data from database using jsp and mysql
retrieve related data from database using jsp and mysql  Hi sir, please give some example of jsp code for retrieving mysql database values in multiple dropdown list. if we change a value in a dropdown its related value must
dropdown
dropdown  I have a dropdown having 2 options-"Open"& "closed".When i select "open" option the related rows of data are retrieved from database and same with the other option.My database is Mysql and coding in PHP
help to select a value from the dropdown list
help to select a value from the dropdown list  I have a html file...;/html> I also have a jsp file called SearchIndex.jsp which is shown below <jsp:useBean id="sessionBean" scope="session" class="com.spl.hms.SessionBean
How to backup a selected file from the dropDown Menu in JSP?
How to backup a selected file from the dropDown Menu in JSP?  I am trying to create a dropdown menu list for some type of files contained ina... file into the backup directory. I need the jsp code that can generate
add text box and select list dynamically and get its value to store it into database using jsp request parameter
add text box and select list dynamically and get its value to store it into database using jsp request parameter  its very helpful when you have only... added select box also into next jsp page plz reply me as early as posssible

Ads