-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateServlet.java
More file actions
97 lines (78 loc) · 3.59 KB
/
UpdateServlet.java
File metadata and controls
97 lines (78 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.dao.DBConnection;
import com.model.Student;
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
action = "deleteAll";
}
List<Student> students = new ArrayList<>();
try (Connection conn = DBConnection.getConnection()) {
if (conn == null) {
return;
}
String sql;
PreparedStatement stmt;
if (action.equals("deleteAll")) {
sql = "DELETE FROM students;";
stmt = conn.prepareStatement(sql);
} else if (action.equals("CSE") || action.equals("ECE") || action.equals("EEE") || action.equals("MECH") || action.equals("CIVIL")) {
sql = "DELETE s FROM students s INNER JOIN branches b ON s.branch_id = b.branch_id WHERE b.branch_name = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, action);
System.out.println("Executing SQL: " + sql);
} else {
request.setAttribute("errorMessage", "Invalid action.");
request.getRequestDispatcher("error.jsp").forward(request, response);
return;
}
try (ResultSet rs = stmt.executeQuery()) {
if (rs != null) {
while (rs.next()) {
students.add(new Student(
rs.getString("roll_number"), rs.getString("first_name"), rs.getString("last_name"),
rs.getString("phone_number"), rs.getString("email"), rs.getString("branch_name")
));
}
} else {
return;
}
} catch (SQLException e) {
return;
}
System.out.println("Number of students found: " + students.size());
request.setAttribute("students", students);
request.getRequestDispatcher("login.jsp").forward(request, response);
} catch (SQLException e) {
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = request.getParameter("firstName");
String password = request.getParameter("password");
try (Connection conn = DBConnection.getConnection()) {
String sql = "Update students set password = (?) where first_name = (?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, firstName);
stmt.setString(2, password);
stmt.executeUpdate();
response.sendRedirect("login.jsp?message=Updated successful");
} catch (Exception e) {
e.printStackTrace();
}
}
}