
Hi, How to get data from MYSQL Database tables on giving a "text" in a text-box in a JSP file. Ex:dept table; if we give dept_no in a text-box in JSP file,we have to retrieve the other fields from the dept table and display it in the same JSP file.
Help me plz;

1)ajax.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script type="text/javascript">
function showData(id){
xmlHttp=GetXmlHttpObject()
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").style.visibility= "visible";
document.getElementById("address").style.visibility= "visible";
document.getElementById("contactNo").style.visibility= "visible";
document.getElementById("email").style.visibility= "visible";
document.getElementById("lab1").style.visibility= "visible";
document.getElementById("lab2").style.visibility= "visible";
document.getElementById("lab3").style.visibility= "visible";
document.getElementById("lab4").style.visibility= "visible";
document.getElementById("name").value= strar[1];
document.getElementById("address").value= strar[2];
document.getElementById("contactNo").value= strar[3];
document.getElementById("email").value= strar[4];
}
}
}
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 >
<div id="mydiv"></div>
<tr><td>Enter Id:</td><td><input type="text" id="id" onkeyup="showData(this.value);"></td></tr>
<tr><td><label id="lab1" style="visibility:hidden">Name:</label></td><td><input type="text" id="name" style="visibility:hidden"></td></tr>
<tr><td><label id="lab2" style="visibility:hidden">Address:</label></td><td><input type="text" id="address" style="visibility:hidden"></td></tr>
<tr><td><label id="lab3" style="visibility:hidden">Contact No:</label></td><td><input type="text" id="contactNo" style="visibility:hidden"></td></tr>
<tr><td><label id="lab4" style="visibility:hidden">Email:</label></td><td><input type="text" id="email" style="visibility:hidden"></td></tr>
</table>
</body>
</html>
2)getdata.jsp:
<%@ page import="java.sql.*" %>
<%
String emp_id = request.getParameter("id").toString();
String buffer="";
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 employee where id='"+emp_id+"'");
while(rs.next()){
buffer=":" +rs.getString("name") +":"+ rs.getString("address") +":"+rs.getString("contactNo")+":"+ rs.getString("email");
}
out.println(buffer);
System.out.println(buffer);
}
catch(Exception e){
System.out.println(e);
}
%>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.