1

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>
Spectric
  • 30,714
  • 6
  • 20
  • 43
user3698489
  • 41
  • 1
  • 1
  • 7
  • And where is the stacktrace? – Reporter Oct 23 '20 at 11:31
  • if(empid.toUpperCase().equals("ALL")) { : pst = con.prepareStatement("select * from employee"); this line sir – user3698489 Oct 23 '20 at 11:39
  • That's not the stacktrace. It could be possible that empid is null. – Reporter Oct 23 '20 at 11:41
  • i updated the stacktrace – user3698489 Oct 23 '20 at 11:45
  • And as you can see there is a null point exception. See also https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Reporter Oct 23 '20 at 11:50
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) Checkvariable con for null – Reporter Oct 23 '20 at 11:52
  • Now working but. i have problem when the form is loaded data not loaded. – user3698489 Oct 23 '20 at 12:18
  • does your `All` condition ever get executed ? Why not put condition like this `if(empid == null || empid.isEmpty()){` and see if that works . – Swati Oct 23 '20 at 12:51
  • sir now working . when i load the form all data should loaded. when i enter id on the textbox relvent record only need to display. – user3698489 Oct 23 '20 at 12:53
  • @user3698489 Try above suggestion and let me know if you face any error – Swati Oct 23 '20 at 12:58
  • yes madam. when i load the form all data should loaded. when i enter id on the textbox relvent record only need to display.but when load the page data not loaded from data base only display search the record on the search box data displayed – user3698489 Oct 23 '20 at 12:59
  • are you there i update my code please check. – user3698489 Oct 23 '20 at 13:02
  • Just use like this - >`if (empid == null || empid.isEmpty()) { pst = con.prepareStatement("select * from employee");}else { pst = con.prepareStatement("select * from employee where id =?"); pst.setString(1, empid);}` no need to put that `if(request.getMethod().compareToIgn..` remove it . – Swati Oct 23 '20 at 13:08
  • 1
    thanks madam it is working now if(request.getMethod().compareToIgn not put show error – user3698489 Oct 23 '20 at 13:15

0 Answers0