Application using JSP with JDBC - Fetching all Records from a MySQL Table

Demo on Fetching All Records from a MySQL Table

JSP Code for Fetching all Records from an Employee and Department Table in a MySQL database using Joins


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>welcome to my display page</title>
</head>
<body>
<%
try
{
  
  Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vit","root","");
  String sql = "select employee_name,department_name from employee,department where dept_id=department_id;";
  PreparedStatement stmt = con.prepareStatement(sql);
  ResultSet rs = stmt.executeQuery();
  if(rs.next()==false)
  {
    out.println("No Records in the table");
  }
  else
  {%>
  
  <table border="1">
  <tr><th>Employee Name</th><th>Department Name</th></tr>
  <% 
    do
    {%>
    
    <tr><td><%= rs.getString(1)%></td><td><%= rs.getString(2)%></td></tr>
    
    <%}while(rs.next());
  }
  
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.getStackTrace();
}

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