GTU WTAD Practical 16 : Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student.

GTU WTAD Practical 16 :  Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student.

GTU WTAD Practical 16 : Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student.

Database is the most important features of all server side scripting language, Here I have explained how we can connect with MySQL Database using Servlet. Make sure to install WAMP before you execute this program or you can also install MySQL Standalone if you have. Once you install MySQL you need to create database named “mydatabase”. Create table named “student_marks” with few fields as shows below. Make sure to include JAR file before you execute. Kindly refer my previous article to get more details about database.

Jump to GTU WTAD Practical 17

Database Configuration

  • Database Used : MySQL
  • Database Name : mydatabase
  • Userid : adarsh
  • Password : patel

Database Table Required

  • Table Name: student_marks
  • Field 1 : enrollment
  • Field 2 : StudentName
  • Field 3 : Subject1Marks
  • Field 4 : Subject2Marks
  • Field 5 : Subject3Marks

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu16\

<html>
	<head>
		<title>GTU WTAD Practical 16</title>
	</head>
	<body>
	
	<h2 align="center"> GTU WTAD Practical 16 :  Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student. </h2>
	
	
	
		<table>
		<tr>
			<th>Student Result</th>
		</tr>
		<tr>
		<td>
		<form action="/gtu16/StudentMarksFromDb" method="get">
			Enrollment Number : 
			<input type="text" name="par1" value="123456789">
			
			<input type="submit" value="submit">
		</form>
		</td>
		</tr>
		</table>
	</body>
</html>

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu16\WEB-INF

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  
  <display-name>GTU WTAD Practical 16  </display-name>
  <description>Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student. </description>
  
  <servlet>
    <servlet-name>StudentMarksFromDb</servlet-name>
    <servlet-class>StudentMarksFromDb</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>StudentMarksFromDb</servlet-name>
      <url-pattern>/StudentMarksFromDb</url-pattern>
  </servlet-mapping> 
  
</web-app>

File Name : StudentMarksFromDb.java

File Location : Tomcat_Apache\webapps\gtu16\WEB-INF\classes

/*
		GTU WTAD Practical 16 :  Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student.
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class StudentMarksFromDb extends HttpServlet
{
    static int count;
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");      
		out.println("<html><head><title>GTU WTAD Practical 16 : Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student. </title></head><body></h1></center><br><br>");
 
		try
		{
 
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","adarsh","patel");
            Statement st=con.createStatement();
            
             ResultSet rs=st.executeQuery("Select * from student_marks where enrollment='"+request.getParameter("par1")+"'");
			if(rs.next())
			{
				 out.println("<center><table border=1>");
				 out.println("<tr><th colspan=\"2\">Student Name : "+rs.getString(2)+" </th></tr>");
				 out.println("<tr><th>Subject Name </th><th>Marks</th></tr>");
				 out.println("<tr><td>Subject 1 : </td><td>"+rs.getFloat(3)+ "</td></tr>");
				 out.println("<tr><td>Subject 2 : </td><td>"+rs.getFloat(4)+ "</td></tr>");
				 out.println("<tr><td>Subject 3 : </td><td>"+rs.getFloat(5)+ "</td></tr>");
				 out.println("</table></center>");
			}
			 else
			 {
				out.println("<br><br><h3><center>No Record Found</center></h3>");
			 }
			    out.close();
       }
       catch (Exception e)
       {
          out.println("The error is:" + e.getMessage());
       }
}
}

 

Output (index.jsp file)

GTU WTAD Practical 16 : Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student.

Output of Servlet (If Student Information Found)

GTU_16_2

 

Output (If No Records Found)

GTU WTAD Practical 16 : Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student.

Leave a Reply