
How to do javascript validations to check whether the entered primary key is present in the database or not .Iam using jsp language

Javascript validation
1)checkid.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script type="text/javascript">
function showData(){
var value=document.getElementById("id").value;
xmlHttp=GetXmlHttpObject()
var url="id.jsp";
url=url+"?id="+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;
alert(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>
<form name="employee">
<div id="mydiv"></div>
<input type="text" name="id" id="id"><input type="button" value="check" onclick="showData();">
</html>
2)id.jsp:
<%@ page import="java.sql.*" %>
<%
String id = request.getParameter("id").toString();
System.out.println(id);
int count=0;
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 employee where id='"+id+"'");
while(rs.next())
{
count++;
}
if(count>0){
data="It is a valid id";
}
else{
data="It is not a valid id";
}
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.