database-2

Ads

 
 

Share on Google+Share on Google+

Shruti Bansal
database-2
1 Answer(s)      6 years and 5 months ago
Posted in : JDBC

I have 2 columns in a table in database and have 2 drop down menus.

what will be the database code for

when i select one option from 1st drop down menu .. the corresponding values to this option stored in the the database should be retrived in the 2nd combo box.

i want the coding in Jsp and microsoft access.

thank u

Ads
View Answers

June 2, 2011 at 5:28 PM


you need to create two tables: country(countryid Number,countryname text) and state(stateid Number countryid Number, statename text);

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("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student");
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from country");
 while(rs.next()){
     %>
      <option value="<%=rs.getInt("countryid")%>"><%=rs.getString("countryname")%></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");  
int id=Integer.parseInt(country);
 String buffer="<select name='state'><option value='-1'>Select</option>";  
 try{
 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student");  
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from state where countryid="+id+" ");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getInt("stateid")+"'>"+rs.getString("statename")+"</option>";  
   }  
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer); 
 }
 catch(Exception e){
     System.out.println(e);
 }
 %>

Ads









Related Tutorials/Questions & Answers:
database-2
database-2  I have 2 columns in a table in database and have 2 drop down menus. what will be the database code for when i select one option from 1st drop down menu .. the corresponding values to this option stored

Ads

 
Advertisement null

Ads