Hi, my question is how to retrieve data from sql server 2008 using a jsp file and place the values in the text fields of a html file.
i created a database table named
addclients clientid 1000 name jhon address abbanna clolony
my html page is
<table> <form id="form7" name="form7" method="post" action="modify_clients.jsp"> <tr> <td height="46"><div align="right"> <h3>Client ID</h3> </div></td> <td> <label> <input name="Client_id" type="text" id="textfield17" value="" /> </label> </td> </tr> <tr> <td width="120"><div align="right">Name</div></td> <td width="288"> <label> <input type="text" name="Name" id="textfield9" /> </label> </td> </tr> <tr> <td><div align="right">Address</div></td> <td><input type="text" name="textfield9" id="textfield10" value="" /></td> </tr> <label> <input type="submit" name="button3" id="button3" value="Modify" /> </label> <label> <input type="reset" name="button3" id="button4" value="Delete" /> </label> </td> </tr> </form> </table>
now i want to place the values of name "jhon" and address "abbanna colony" in those text fields using a query "select * from addclients where clinetid=1000".
the main theme of this page is when anyone enter his clientid and click modify button the details of the person with clientid has to be displayed on those text fields and after changing his details and pressing the submit button the values have to be updated in the addclients table.
can anyone plese send me the jsp code to retrieve and set the data to the html file?
1)ajax.jsp:
<%@page import="java.sql.*"%> <html> <head> <script type="text/javascript"> function showData(){ xmlHttp=GetXmlHttpObject() var id=document.getElementById("id").value; var url="getdata.jsp"; url=url+"?id="+id; xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged(){ if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { var showdata = xmlHttp.responseText; var strar = showdata.split(":"); if(strar.length>1) { var strname = strar[1]; document.getElementById("name").value= strar[1]; document.getElementById("address").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 method="post" action="update.jsp" > <br><br> <table > <tr><td>Client ID:</td><td><input type="text" id="id" name="id" onkeyup="showData();"></td></tr> <tr><td>Name:</td><td><input type="text" id="name" name="name"></td></tr> <tr><td>Address:</td><td><input type="text" id="address" name="address"></td></tr> <tr><td><input type="submit" value="Update"></td><td><input type="reset" value="Reset"></td></tr> </table> </body>
2)getdata.jsp:
<%@ page import="java.sql.*" %> <% int id = Integer.parseInt(request.getParameter("id")); String data=" "; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("Select * from addclients where clientid ="+id+""); while(rs.next()) { data = ":" + rs.getString("name") + ": " + rs.getString("address"); } out.println(data); } catch(Exception e){ System.out.println(e); } %> </html>
3)update.jsp:
<%@page import="java.sql.*"%> <% String ide=request.getParameter("id"); int num=Integer.parseInt(ide); String name=request.getParameter("name"); String address=request.getParameter("address"); try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); Statement st=null; st=conn.createStatement(); st.executeUpdate("update addclients set name='"+name+"',address='"+address+"' where clientid ='"+num+"'"); out.println("Record is updated successfully!"); } catch(Exception e){ System.out.println(e); } %>
hi i want to retrieve country name from mysql db to textfield.. maeans if anyone type first letter of the name it should show the corresponding names in the list.. like autocomplete.. ihave tried coding in this forum but its has some difficulties i caant able to select particular name to textfield... it should be in jsp.. please help me out