I have a problem in my JSP Coding. How to retrive value from database in to text field when user select one value from dropdown list. For example, if user select on of name in dropdown list (the name list is also coming from database)every information about the person should show in texfield that already create in the web page. I'm really stuck on this area. Please help me.
By the way, I'm using access Database and JSP coding.
Thank You.
Here is an application that retrieves table name from database and display in dropdown box. When the user select particular name from dropdown corresponding values will get displayed into textboxes.
1)selectname.jsp:
<%@page import="java.sql.*"%> <html> <head> <script type="text/javascript"> function showEmp(emp_value){ if(document.getElementById("address").value!="-1"){ xmlHttp=GetXmlHttpObject() if (xmlHttp==null){ alert ("Browser does not support HTTP Request") return } var url="getvalue.jsp" url=url+"?name="+emp_value xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } else{ alert("Please Select Employee Id"); } } function stateChanged(){ document.getElementById("email").value =""; document.getElementById("address").value =""; if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { var showdata = xmlHttp.responseText; var strar = showdata.split(":"); if(strar.length==1){ document.getElementById("address").focus(); alert("Please Select Employee Id"); document.getElementById("email").value =" "; document.getElementById("address").value =" "; } else if(strar.length>1) { var strname = strar[1]; document.getElementById("address").value= strar[1]; document.getElementById("email").value= strar[2]; } } } function GetXmlHttpObject(){ var xmlHttp=null; try{ xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } </script> </head> <body> <form name="employee"> <br><br> <table border="0" width="400px" align="center" bgcolor="#CDFFFF"> <div id="mydiv"></div> <tr><td><b>Select Employee Name</b></td><td> <select name="name" onchange="showEmp(this.value);"> <option value="-1">Select</option> <% Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","root"); String query = "select * from employee"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(query); while(rs.next()) { %> <option value="<%=rs.getString("name")%>"><%=rs.getString("name")%></option> <% } %> </select> </td></tr> <tr><td ><b>Employee Address:</b></td><td> <input type="text" name="address" id="address" value=""></td></tr> <tr><td><b>Employee Email:</b></td><td> <input type="text" name="email" id="email" value=""></td></tr> </table> </form> <table border="0" width="100%" align="center"> <br> <br> </table> </body> </html>
2)getvalue.jsp:
<%@page import="java.sql.*"%> <% String emp_id = request.getParameter("name").toString(); String data=""; int sumcount=0; Statement st; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","root"); String query = "select * from employee where name='"+emp_id+"'"; st = conn.createStatement(); ResultSet rs = st.executeQuery(query); while(rs.next()) { data = ":" + rs.getString("address") +":"+ rs.getString("email"); } out.println(data); } catch (Exception e) { e.printStackTrace(); } %>
3)For the above code, we have created following table:
CREATE TABLE employee
(
id
bigint(255) default NULL,
name
varchar(255) default NULL,
address
varchar(255) default NULL,
contactNo
int(255) default NULL,
email
varchar(255) default NULL
)
Please visit the following link:
thank you very much
Ads