I want to get the data from database in to text box in a jsp page by using Ajax. If I enter the letter A in the textbox it should show only those values which starts with a and from that i need to select the required value and i should store the selected value into a variable. Plz help me out
Hi Friend,
Try the following code:
textbox.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="value.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;
}
}
function hello(){
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
document.getElementById('name').value=txt;
alert(txt);
}
</script>
</head>
<body>
<input id="name" type="text" name="name" onkeyup="showState(this.value)">
<br>
<div id='country'>
</div>
<input type="button" onclick="hello();" value="Get Value">
</body>
</html>
2)value.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);
%>
Thanks
many many thanks for the example!!!
How to get data from DB in to Text box by using Jsp & Ajax
how to display data on textboxes from database using jsp and ajax i want full html and ajax and jsp code.
how can i split the data from database and display on text boxes