Servlet program which retrieves student data from database and print in a tabular format

 In this post, we will write a Servlet program that retrieves student data from the Oracle database and prints it in a tabular format.

Create Student Class

public class Student {
    private int id;
    private String name, password, email, country;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
}

JDBC Code to Retrieve Students

import java.util.*;
import java.sql.*;

public class StudentDao {

    public static Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
        } catch (Exception e) {
            System.out.println(e);
        }
        return con;
    }

    public static List < Student > getAllStudents() {
        List < Student > list = new ArrayList < Student > ();

        try {
            Connection con = StudentDao.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from students");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Student e = new Student();
                e.setId(rs.getInt(1));
                e.setName(rs.getString(2));
                e.setPassword(rs.getString(3));
                e.setEmail(rs.getString(4));
                e.setCountry(rs.getString(5));
                list.add(e);
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
}

Servlet Program to Show Students in a Table

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ShowServlet")
public class ViewServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<a href='index.html'>Add New Student</a>");
        out.println("<h1>Students List</h1>");

        List < Student > list = StudentDao.getAllStudents();

        out.print("<table border='1' width='100%'");
        out.print("<tr><th>Id</th><th>Name</th><th>Password</th><th>Email</th><th>Country</th>   <
            th > Edit < /th><th>Delete</th > < /tr>");  
            for (Student e: list) {
                out.print("<tr><td>" + e.getId() + "</td><td>" + e.getName() + "</td><td>" + e.getPassword() + "</td>   <
                    td > "+e.getEmail()+" < /td><td>"+e.getCountry()+"</td > < td > < a href = 'EditServlet?id="+e.getId()+"' > edit < /a></td >
                    <
                    td > < a href = 'DeleteServlet?id="+e.getId()+"' > delete < /a></td > < /tr>");  
                }
                out.print("</table>");

                out.close();
            }
        }

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course

Comments