-
Notifications
You must be signed in to change notification settings - Fork 0
/
students.jsp
executable file
·235 lines (196 loc) · 8.54 KB
/
students.jsp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<html>
<body>
<table>
<tr>
<td valign="top">
<%-- -------- Include menu HTML code -------- --%>
<jsp:include page="../menu.html" />
</td>
<td>
<%-- Import the java.sql package --%>
<%@ page import="java.sql.*"%>
<%-- -------- Open Connection Code -------- --%>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// Registering Postgresql JDBC driver with the DriverManager
Class.forName("org.postgresql.Driver");
// Open a connection to the database using DriverManager
conn = DriverManager.getConnection(
"jdbc:postgresql://localhost/Project1DB?" +
"user=postgres&password=postgres");
%>
<%-- -------- INSERT Code -------- --%>
<%
String action = request.getParameter("action");
// Check if an insertion is requested
if (action != null && action.equals("insert")) {
// Begin transaction
conn.setAutoCommit(false);
// Create the prepared statement and use it to
// INSERT student values INTO the students table.
pstmt = conn
.prepareStatement("INSERT INTO students (pid, first_name, middle_name, last_name) VALUES (?, ?, ?, ?)");
pstmt.setInt(1, Integer.parseInt(request.getParameter("pid")));
pstmt.setString(2, request.getParameter("first"));
pstmt.setString(3, request.getParameter("middle"));
pstmt.setString(4, request.getParameter("last"));
int rowCount = pstmt.executeUpdate();
// Commit transaction
conn.commit();
conn.setAutoCommit(true);
}
%>
<%-- -------- UPDATE Code -------- --%>
<%
// Check if an update is requested
if (action != null && action.equals("update")) {
// Begin transaction
conn.setAutoCommit(false);
// Create the prepared statement and use it to
// UPDATE student values in the Students table.
pstmt = conn
.prepareStatement("UPDATE students SET pid = ?, first_name = ?, "
+ "middle_name = ?, last_name = ? WHERE id = ?");
pstmt.setInt(1, Integer.parseInt(request.getParameter("pid")));
pstmt.setString(2, request.getParameter("first"));
pstmt.setString(3, request.getParameter("middle"));
pstmt.setString(4, request.getParameter("last"));
pstmt.setInt(5, Integer.parseInt(request.getParameter("id")));
int rowCount = pstmt.executeUpdate();
// Commit transaction
conn.commit();
conn.setAutoCommit(true);
}
%>
<%-- -------- DELETE Code -------- --%>
<%
// Check if a delete is requested
if (action != null && action.equals("delete")) {
// Begin transaction
conn.setAutoCommit(false);
// Create the prepared statement and use it to
// DELETE students FROM the Students table.
pstmt = conn
.prepareStatement("DELETE FROM Students WHERE id = ?");
pstmt.setInt(1, Integer.parseInt(request.getParameter("id")));
int rowCount = pstmt.executeUpdate();
// Commit transaction
conn.commit();
conn.setAutoCommit(true);
}
%>
<%-- -------- SELECT Statement Code -------- --%>
<%
// Create the statement
Statement statement = conn.createStatement();
// Use the created statement to SELECT
// the student attributes FROM the Student table.
rs = statement.executeQuery("SELECT * FROM students");
%>
<!-- Add an HTML table header row to format the results -->
<table border="1">
<tr>
<th>ID</th>
<th>PID</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<tr>
<form action="students.jsp" method="POST">
<input type="hidden" name="action" value="insert"/>
<th> </th>
<th><input value="" name="pid" size="10"/></th>
<th><input value="" name="first" size="15"/></th>
<th><input value="" name="middle" size="15"/></th>
<th><input value="" name="last" size="15"/></th>
<th><input type="submit" value="Insert"/></th>
</form>
</tr>
<%-- -------- Iteration Code -------- --%>
<%
// Iterate over the ResultSet
while (rs.next()) {
%>
<tr>
<form action="students.jsp" method="POST">
<input type="hidden" name="action" value="update"/>
<input type="hidden" name="id" value="<%=rs.getInt("id")%>"/>
<%-- Get the id --%>
<td>
<%=rs.getInt("id")%>
</td>
<%-- Get the pid --%>
<td>
<input value="<%=rs.getInt("pid")%>" name="pid" size="15"/>
</td>
<%-- Get the first name --%>
<td>
<input value="<%=rs.getString("first_name")%>" name="first" size="15"/>
</td>
<%-- Get the middle name --%>
<td>
<input value="<%=rs.getString("middle_name")%>" name="middle" size="15"/>
</td>
<%-- Get the last name --%>
<td>
<input value="<%=rs.getString("last_name")%>" name="last" size="15"/>
</td>
<%-- Button --%>
<td><input type="submit" value="Update"></td>
</form>
<form action="students.jsp" method="POST">
<input type="hidden" name="action" value="delete"/>
<input type="hidden" value="<%=rs.getInt("id")%>" name="id"/>
<%-- Button --%>
<td><input type="submit" value="Delete"/></td>
</form>
</tr>
<%
}
%>
<%-- -------- Close Connection Code -------- --%>
<%
// Close the ResultSet
rs.close();
// Close the Statement
statement.close();
// Close the Connection
conn.close();
} catch (SQLException e) {
// Wrap the SQL exception in a runtime exception to propagate
// it upwards
throw new RuntimeException(e);
}
finally {
// Release resources in a finally block in reverse-order of
// their creation
if (rs != null) {
try {
rs.close();
} catch (SQLException e) { } // Ignore
rs = null;
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) { } // Ignore
pstmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) { } // Ignore
conn = null;
}
}
%>
</table>
</td>
</tr>
</table>
</body>
</html>