hi friends i am doing a project in jsp and oracle as my database.In my project i have an registration form containing many text fields.there is no problem in storing them into the database,this registration form contains name and department fields also.I have a create an jsp page in which there should 2 drop down list.In that first list is for department and the 2nd list for the names of the persons who work in that department.The values into the drop down list should get dynamically from the database and whenever a new person has registered his name should also be included in the list dynamically and any updating on the department i.e if the person department has been changed he should come under his new department list. Please could any one send the exact code which can solve my problem. Please try to send so soon as possible Thank you in advance
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>
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); } %>
For the above code, we have used two database tables:
1)dept:
CREATE TABLE `dept` ( `DEPT_NO` int(100) default NULL, `DEPT_NAME` varchar(255) default NULL );
2)emp:
CREATE TABLE `emp` ( `EMP_NO` int(10) NOT NULL auto_increment, `EMP_NAME` varchar(100) default NULL, `DESIGNATION` varchar(100) default NULL, `JOINING_DATE` date default NULL, `SALARY` int(100) default NULL, `DEPT_NO` int(100) default NULL, `DEPT_NAME` varchar(100) default NULL, PRIMARY KEY (`EMP_NO`) );
thank u for the code.But i did not get the result.If you don't mind could display whats the output for this code. If possible please send another code which uses oracle as its database i don't know mysql.send an example that has the registration form and full working which is similar to my question. Thank you in advance
hi friends i am doing a project in jsp and mysql as my database.In my project i have an edit form containing many text fields.there is no problem in storing them into the database,this registration form contains date,flight number,origin and departure fields also.I have a create an jsp page in which there should multiple drop down list.In that first list is for flight number and the 2nd list dependent on 1st list and subsequently.The values into the drop down list should get dynamically from the database. Please could any one send the exact code which can solve my problem. Please try to send so soon as possible Thank you in advance