
Auto Complete Text Box Please help me in Aotocomplete text box code. I m a java programmer. I m working on jsp tech. please give me the code for Autocomplete text box in which the values will generate from database table

Hi Friend,
Try the following code:
1)auto.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp
var xmlHttp
function showState(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="get.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("country").innerHTML=xmlHttp.responseText;
}
}
</script>
</head>
<body>
<input id="name" type="text" name="name" onkeyup="showState(this.value)">
<br>
<div id='country'>
</div>
</body>
</html>
2)get.jsp:
<%@page language="java" import ="java.sql.*" %>
<%
String name=request.getParameter("count");
String buffer="<div>";
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 country where countryname LIKE '"+name+"%'");
while(rs.next()){
buffer=buffer+rs.getString(2)+"<br>";
}
buffer=buffer+"</div>";
response.getWriter().println(buffer);
%>
Hope that it works! Thanks

how to use another executeQuery in the same page get.jsp.So that i can to fill data in another textbox or listbox

Thanks Buddy its working Cool

yeah thats working fine but it is just taking data in the textbox from the database but we are not able to select the data from textfield so object is not completed.How can we select the data into the textfield.
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.