STUDENT REGISTRATION WEB APPLICATION USING JAVA(NETBEANS) + MYSQL(PHPMYADMIN) - PART 02
UPDATE AND DELETE STUDENT
update.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
if (request.getParameter("submit") != null) {
String id = request.getParameter("id");
String name = request.getParameter("sname");
String course = request.getParameter("course");
String fee = request.getParameter("fee");
Connection con;
PreparedStatement pst;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "");
pst = con.prepareStatement("update student_db set stname = ?,course =?,fee= ? where id = ?");
pst.setString(1, name);
pst.setString(2, course);
pst.setString(3, fee);
pst.setString(4, id);
pst.executeUpdate();
%>
<script>
alert("Record Updated");
</script>
<% }
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<link href="../bootstrap/bootstrap-4.5.3-dist/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="../bootstrap/bootstrap-4.5.3-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h3>Student Update</h3>
<div class="row">
<div class="col-sm-4">
<div class="shadow-lg p-3 mb-5 bg-white rounded">
<form method="POST" action="#" >
<% Connection con;
PreparedStatement pst;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "");
String id = request.getParameter("id");
pst = con.prepareStatement("select * from student_db where id = ?");
pst.setString(1, id);
rs = pst.executeQuery();
while (rs.next()) {
%>
<div alight="left">
<label class="form-label">Student Name</label>
<input type="text" class="form-control" placeholder="Student Name" value="<%= rs.getString("stname")%>" name="sname" id="sname" required >
</div>
<div alight="left">
<label class="form-label">Course</label>
<input type="text" class="form-control" placeholder="Course" name="course" value="<%= rs.getString("course")%>" id="course" required >
</div>
<div alight="left">
<label class="form-label">Fee</label>
<input type="text" class="form-control" placeholder="Fee" name="fee" id="fee" value="<%= rs.getString("fee")%>" required >
</div>
<% }%>
</br>
<div alight="right">
<input type="submit" id="submit" value="submit" name="submit" class="btn btn-info">
<input type="reset" id="reset" value="reset" name="reset" class="btn btn-warning">
</div>
<div align="right">
<p><a href="index.jsp">Click Back</a></p>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
delete.jsp
<%@page import="java.sql.*" %>
<%
String id = request.getParameter("id");
Connection con;
PreparedStatement pst;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/student","root","");
pst = con.prepareStatement("delete from student_db where id = ?");
pst.setString(1, id);
pst.executeUpdate();
%>
<script>
alert("Record Delete");
</script>
YouTube Link : https://youtu.be/9En1qt62q1Y
Comments
Post a Comment