Home Answers Viewqa Java-Beginners jsp- database dependent dropdown list

 
 


aayushi saxena
jsp- database dependent dropdown list
3 Answer(s)      a year and 7 months ago
Posted in : Java Beginners

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

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 Pages:
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
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
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.*"%>
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
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
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
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 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
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
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
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... data from the database and set it into the dropdown list in JSP using MySQL
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 -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
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
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
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
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
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 in struts - Struts
dropdown in struts  how to populate a drop down from database entries in struts application when i have the workflow as jsp->acton->Business... write the query (in which file)to get the list from database and how and where
Java Swing dependent JList
Java Swing dependent JList In this section, you will learn how to create a dependent list using java swing In the given code, we have created three lists... the first list, the tables present under that database should be displayed
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
Dropdown menus onchange atribute
from database and store it into dropdown list. On selecting the value from... the data from database and stored it into dropdown list. On selecting the value from... down list from database? I want to select a type of vehicle from a drop down
Passing Multi select list box values using ajax to get values to dependent list box
Passing Multi select list box values using ajax to get values to dependent list box  Hi, I have a jsp page which has 3 dependent list boxes... the dependent values to the immediate next list box. Please help me
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
DropDown Department Login
DropDown Department Login  Hi all, I am doing a project using JSP. My Issue is..... I have two departments name Customs and Accounts in the database... a drop down list consisting of Customs and Accounts. I have user accounts of both
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
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
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
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
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 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
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
DropDown Menu
DropDown Menu  Hello, i have a program that can view,add,delete... me draw to your my mind my program.. in my homepage which i have a dropdown menu that has the list of Page 1- Page 5. if i select page 1 it will display only
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
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
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
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
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
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
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
populate dropdown box
populate dropdown box  hi, Is there any ways in which i can populate my dropdown menu with values from 1 to n (n = the value stored in database)using java script or jsp, either is fine? Ex: If value corresponding to selected
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
fetch values in dropdown
fetch values in dropdown  in my application i want fetch dropdown values in jsp page through servlet. means i have to fetch the database fields values in array variable of servlet and then i have to print those values in dropdown
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
How to display content in same page while selecting fron dropdown list
dropdownlist in jsp page.If i select any value in that list(for eg-rollno...How to display content in same page while selecting fron dropdown list ...... Thanks in advance...   hi friend, JSP/Servlet works
dynamic drop down list
dynamic drop down list  I want to create 2 drop down list, where it takes value from database and the two list are dependent..means if I select a particular value from one the other list is developed accordingly..depending
Select DropDown Default
Select DropDown Default  Hi there, i have a program in JSP where i... of the record, in my jsp page i have a one dropdown menu where the user can choose, i.e if the user select PAGE 1 in the dropdown menu, it will display all
Multiple select dropdown with Ajax
Multiple select dropdown with Ajax This tutorial explains how to create multiple select  dropdown with Ajax  in JSP and Servlet . This example... database if not exists `multipleselect_dropdown`; USE `multipleselect
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.