hi friends the below code is working correctly for loading the data dynamically from the data base but my problem is it is retrieving the whole department field from the data base (i.e in the drop down list the department names are repeated)actually it should not happen.when once a department name is given it has to come only once and when new department is entered it should be included in the drop down list.
1)selDept.jsp: <%@page import="java.sql.*"%> <html> <head> <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="selEmp.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("emp").innerHTML=xmlHttp.responseText } } </script> </head> <body> <select name='dept' onchange="showEmp(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 dept"); while(rs.next()){ %> <option value="<%=rs.getString("DEPT_NO")%>"><%=rs.getString("DEPT_NAME")%></option> <% } %> </select> <br> <div id='emp'> <select name='emp' > <option value='-1'></option> </select> </div> </body> </html>
it also has another jsp page
2)selEmp.jsp: <%@page import="java.sql.*"%> <% String no=request.getParameter("count"); String buffer="<select name='emp' ><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 emp where DEPT_NO='"+no+"' "); while(rs.next()){ buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString("EMP_NAME")+"</option>"; } buffer=buffer+"</select>"; response.getWriter().println(buffer); } catch(Exception e){ System.out.println(e); } %>
Ads