Fetching Multiple Records from database Dynamically Using ArrayList

Fetch.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body bgcolor="lightgreen">
        <style>

            .button{
                padding: 5px;
                width: 6%;
                background: cornflowerblue;
                color: white;
            }
        </style>

        <form method="POST" action="fetch">
            <center>
                <br><br>
               
                <div style="text-align: center;margin-left: 70px;margin-top: 10px;">
                    <input type="submit" name="submit" class="button" value="FETCH"/>
                </div>
            </center> 
        </form>
    </body>
</html>

view.jsp

<%@page import="java.util.ArrayList"%>
<%@page import="model.student"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>

    <body bgcolor="lightgreen">
         <h1 style="text-align: center"> Single  Record From Database </h1>
        <style>
            table,th,tr{
                border:1px solid black;
            }
            th,td{
                border-right:1px solid black;
            }
        </style>
        <table   align="center"  width="800" style="border-collapse:collapse;cursor: default;"> 
            <tr>  
                <TH bgcolor="#6CBFE8" >Studentid</TH>
                <TH bgcolor="#6CBFE8" >NAME</TH>
                <TH bgcolor="#6CBFE8">Marks</TH>
            </tr>  

            <% ArrayList<student> mydata = (ArrayList) request.getAttribute("myArray");
                Iterator<student> itr = mydata.iterator();
                while (itr.hasNext()) {
                    student s = itr.next();%> 
            <tr>
                <td align="center" width="15%"><%=s.getId()%></td>  
                <td align="center" width="15%"><%=s.getName()%></td> 
                <td align="center" width="15%"><%=s.getMarks()%></td> 
            </tr>
            <%}%> 


        </table>   
    </body>
</html>

Fetch Servlet:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.student;


@WebServlet(name = "fetch", urlPatterns = {"/fetch"})
public class fetch extends HttpServlet 
{

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Try
 {
        
            student_impl s = new student_impl();
            ArrayList<student> ar = s.getstudent_details();
            request.setAttribute("myArray", ar);
            RequestDispatcher dispatcher = request.getRequestDispatcher("view.jsp");
            dispatcher.forward(request, response);
        } 
catch (Exception e) 
{
            e.printStackTrace();
        }

    }

}

Student_impl: class name

public class student_impl 
{

    Connection con;
    Statement st;
    ResultSet rs;

    public student_impl() 
{
        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");

        } 
catch (Exception e) 
{
            e.printStackTrace();
        }
    }



public ArrayList<student> getstudent_details() 
{
        ArrayList<student> al = new ArrayList<student>();
        try 
{
            st = con.createStatement();
         
            String qry = "select * from student ;";
            rs = st.executeQuery(qry);
            while (rs.next())
 {
                student p = new student();
                p.setId(rs.getString("studentid"));
                p.setName(rs.getString("sname"));
                p.setMarks(rs.getString("smarks"));
                al.add(p);
            }

        } catch (Exception e) 
{
            e.printStackTrace();
        }
        return al;
    }
}

Class name: Student

public class student 
{
  String id;
  String name;
  String marks;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getMarks() {
        return marks;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMarks(String marks) {
        this.marks = marks;
    }
  
    
}

SQL:

create table student(
 studentid int(10)  NOT NULL AUTO_INCREMENT,
  sname varchar(45) NOT NULL,
  smarks varchar(45) NOT NULL,

  PRIMARY KEY (studentid)
);

insert into student
 values(0,'karthik','87');

(in the table student , the column studentid is primary key and it is auto incremented.
so while writing the query just enter 0 (zero) for the column. it wil be auto incremented in the table)

select * from student

Posted on by