
how to read value from text box and alert whether it is available in database or not?

1)availability.jsp:
<html>
<head>
<script type="text/javascript">
function check(value){
xmlHttp=GetXmlHttpObject()
var url="check.jsp";
url=url+"?name="+value;
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged(){
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
var showdata = xmlHttp.responseText;
document.getElementById("mydiv").innerHTML= showdata;
}
}
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="form">
UserName: <input type="text" name="name" id="name" onkeyup="check(this.value);"><font color="red"><div id="mydiv"></div></font>
</form>
</body>
</html>
2)check.jsp:
<%@ page import="java.sql.*" %>
<%
String name = request.getParameter("name").toString();
System.out.println(name);
String data ="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+name+"'");
int count=0;
while(rs.next())
{
count++;
}
if(count>0)
{
data="Not Available";
}
else
{
data="Available";
}
out.println(data);
System.out.println(data);
}
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.