0% found this document useful (0 votes)
12 views7 pages

Practical 7 rishi wt (2)

The document outlines a Java web application that manages student information using a MySQL database. It includes SQL commands to create a 'students_info' table and insert student records, an XML configuration for a servlet, and Java code for a servlet that retrieves student data and forwards it to a JSP page for display. The JSP page presents the student information in a table format and handles error messages if database connections fail.

Uploaded by

rishibahi1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Practical 7 rishi wt (2)

The document outlines a Java web application that manages student information using a MySQL database. It includes SQL commands to create a 'students_info' table and insert student records, an XML configuration for a servlet, and Java code for a servlet that retrieves student data and forwards it to a JSP page for display. The JSP page presents the student information in a table format and handles error messages if database connections fail.

Uploaded by

rishibahi1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MySQL:

CREATE TABLE students_info (

stud_id INT PRIMARY KEY,


stud_name VARCHAR(255),

class VARCHAR(50),
division VARCHAR(10),
city VARCHAR(100)
);

INSERT INTO students_info (stud_id, stud_name, class, division, city) VALUES

(1, 'Nishant Vishwakarma', 'T.E', 'B', 'New York'),

(2, 'Shafi Uddin', 'T.E', 'B', 'Los Angeles'),


(3, 'Richita Srivastava', 'T.E', 'D', 'Chicago');

XML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

<servlet>
<servlet-name>StudentInfoServlet</servlet-name>
<servlet-class>StudentInfoServlet</servlet-class>

</servlet>

<servlet-mapping>
<servlet-name>StudentInfoServlet</servlet-name>
<url-pattern>/studentInfo</url-pattern>
</servlet-mapping>
</web-app>
Java:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentInfoServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

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

List<Student> studentList = new ArrayList<>();


Connection connection = null;

Statement statement = null;


ResultSet resultSet = null;

String jdbcUrl = "jdbc:mysql://localhost:3306/Practical 6";


String dbUser = "root";

String dbPass = "Ni$h@nt#47";


try {

Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass);
statement = connection.createStatement();

String sql = "SELECT stud_id, stud_name, class, division, city FROM


students_info";

resultSet = statement.executeQuery(sql);

while (resultSet.next()) {
Student student = new Student();
student.setStud_id(resultSet.getInt("stud_id"));
student.setStud_name(resultSet.getString("stud_name"));
student.setClass_(resultSet.getString("class"));
student.setDivision(resultSet.getString("division"));
student.setCity(resultSet.getString("city"));

studentList.add(student);

request.setAttribute("studentList", studentList);

request.getRequestDispatcher("index.jsp").forward(request, response);

} catch (Exception e) {
e.printStackTrace();
request.setAttribute("errorMessage", "Error connecting to database: " +
e.getMessage());

request.getRequestDispatcher("index.jsp").forward(request, response);
} finally {
try { if (resultSet != null) resultSet.close(); } catch (Exception e) {
e.printStackTrace(); }
try { if (statement != null) statement.close(); } catch (Exception e) {
e.printStackTrace(); }
try { if (connection != null) connection.close(); } catch (Exception e) {
e.printStackTrace(); }

}
}

public class Student {


private int stud_id;

private String stud_name;

private String class_;

private String division;


private String city;

public int getStud_id() {


return stud_id;

public void setStud_id(int stud_id) {


this.stud_id = stud_id;

public String getStud_name() {


return stud_name;

public void setStud_name(String stud_name) {

this.stud_name = stud_name;

public String getClass_() {


return class_;

}
public void setClass_(String class_) {
this.class_ = class_;

public String getDivision() {


return division;

public void setDivision(String division) {


this.division = division;

public String getCity() {


return city;

public void setCity(String city) {


this.city = city;

}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">

<title>Student Information</title>
</head>
<body>

<h2>Student Information</h2>
<c:if test="${not empty errorMessage}">

<p style="color:red;">Error: ${errorMessage}</p>

</c:if>

<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Class</th>

<th>Division</th>
<th>City</th>
</tr>

</thead>
<tbody>
<c:forEach var="student" items="${studentList}">
<tr>
<td>${student.stud_id}</td>
<td>${student.stud_name}</td>
<td>${student.class_}</td>

<td>${student.division}</td>
<td>${student.city}</td>
</tr>

</c:forEach>
</tbody>
</table>

</body>
</html>
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy