
How to Retrieve The Data From MYSQL database TO Use Select the Emp_id Option.And Also Search Option?

JSP search data from mysql database:
1)selEmployee.jsp:
<%@page import="java.sql.*"%>
<script type="text/javascript">
function showData(value){
xmlHttp=GetXmlHttpObject()
var url="empdata.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;
var strar = showdata.split(":");
document.getElementById("table").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>
<table border=1>
<tr><td>Employee Id</td><td>Search</td></tr>
<%
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 id from employee");
while(rs.next()){
%>
<tr><td><%=rs.getInt("id")%></td>
<td><input type="button" value="Search" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="showData(<%=rs.getInt("id")%>);" ></td></tr>
<%
}
%>
<table id="table" style="visibility:hidden;">
<div id="mydiv"></div>
<tr><td><b>Name:</b></td><td>
<input type="text" name="name" id="name" ></td></tr>
<tr><td ><b>Address:</b></td><td>
<input type="text" name="address" id="address" ></td></tr>
<tr><td><b>ContactNo:</b></td><td>
<input type="text" name="contactNo" id="contactNo" ></td></tr>
<tr><td><b>Email:</b></td><td>
<input type="text" name="email" id="email" ></td></tr>
</table>
2)empdata.jsp:
<%@ page import="java.sql.*" %>
<%
int id = Integer.parseInt(request.getParameter("id").toString());
System.out.println(id);
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())
{
data =":"+rs.getString("name") + ":" + rs.getString("address")+":"+Integer.parseInt(rs.getString("contactNo"))+":"+rs.getString("email");
}
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.