I am creating simple search using JSP and MySQL. The search as two parts. When the page is loaded have to show all records. when the user type the relevant employee id should display information of employee to table. when load the page data not loaded from data base only display search the record on the search box data displayed
<div class="container">
<div class="form-group col-12 p-0">
<%
Connection con;
PreparedStatement pst;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/mcompany","root","");
String empid = request.getParameter("empid");
if(request.getMethod().compareToIgnoreCase("post")==0)
{
if(empid.toUpperCase().equals("ALL"))
{
pst = con.prepareStatement("select * from employee");
}
else
{
pst = con.prepareStatement("select * from employee where id =?");
}
pst.setString(1, empid);
rs = pst.executeQuery();
while(rs.next())
{
out.print("<TR>");
out.print("<TD>" + rs.getString("name") + "<TD>");
out.print("<TD>" + rs.getString("phone") + "<TD>");
out.print("<TD>" + rs.getString("salary") + "<TD>");
out.print("</TR>");
}}
%>
<body>
<div class="container">
<div class="form-group col-6 p-0">
<form id="form" method="post" action="index.jsp" class="form-horizontal">
<div class="form-group col-md-6">
<label>Employee ID</label>
<input type="text" name="empid" class="form-control" id="empid" placeholder="Employee ID">
</div>
<div class="form-group col-md-6" align="center">
<Button class="btn btn-success" style="width: 80px;">Submit</Button>
</div>
</form>
</div>
</div>
</body>
</html>