0

I am using JDBC to connect to a database and using a select query. I am fetching the data into an Arraylist and showing the data into a JSP page in table format.

All things going good but I am unable to retrieve the data from the Arraylist to show it on my JSP page.

(In the code i have removed the import's)

Any help will be appreciated.

public class Search extends HttpServlet {

    ArrayList<Object> list = new ArrayList<Object>();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        try{
            Integer n =Integer.parseInt(request.getParameter("empno"));

            Class.forName("com.mysql.jdbc.Driver");
            Connection con =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root", "root");
            PreparedStatement ps = con.prepareStatement("select * from astintech where EmpNo = ?");
            ps.setInt(1, n);
            ResultSet rs = ps.executeQuery();

             if(rs.next())
             {
                {
                    String empno = rs.getString(1);
                    String ename = rs.getString(2);
                    String job = rs.getString(3);
                    String loc = rs.getString(4);

                    list.add(empno);
                    list.add(ename);
                    list.add(job);
                    list.add(loc);

                    System.out.println("list::" +list); 
                }

                request.setAttribute("nList", list);

                System.out.println("valid User no.");
                RequestDispatcher rd = request.getRequestDispatcher("Welcome.jsp");
                rd.include(request, response);
            }
            else
            {   
                System.out.println("invalid User no.");
                RequestDispatcher rd = request.getRequestDispatcher("Error.jsp");
                rd.include(request, response);
            }
        }catch(NumberFormatException nfe)
        {
            System.out.println("ONLY INTEGER VALUE ALLOWED");
            System.out.println("PLEASE ENTER ONLY INTEGER");
            RequestDispatcher rd = request.getRequestDispatcher("NumberFormatException.jsp");
            rd.include(request, response);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            out.close();
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rohit Raj
  • 1
  • 3

0 Answers0