How we view student record in JSP:-
ViewStudent.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a href="StudentInfo.jsp">BACK</a>
<br>
<hr>
<h6>VIEW STUDENT RECORD HERE</h6>
<hr>
<table border="1" width="800" >
<tr><th>RNO</th><th>NAME</th><th>BRANCH</th><th>FEES</th><th>OPERATION</th></tr>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stuinfo","root","");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("select * from student");
while(res.next())
{ %>
<tr><td><%= res.getInt(1) %> </td><td><%= res.getString(2) %></td><td><%= res.getString(3) %></td><td><%= res.getString(4) %></td>
<td><a href="EditStudent.jsp?q=<%= res.getInt(1) %>"><img src="delete.png" width="30" height="30" /></a> <a href="Deletestudent.jsp?q=<%= res.getInt(1) %>"><img src="edit.png" width="30" height="30"></a></td>
</tr>
<%}
%>
</table>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a href="StudentInfo.jsp">BACK</a>
<br>
<hr>
<h6>VIEW STUDENT RECORD HERE</h6>
<hr>
<table border="1" width="800" >
<tr><th>RNO</th><th>NAME</th><th>BRANCH</th><th>FEES</th><th>OPERATION</th></tr>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stuinfo","root","");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("select * from student");
while(res.next())
{ %>
<tr><td><%= res.getInt(1) %> </td><td><%= res.getString(2) %></td><td><%= res.getString(3) %></td><td><%= res.getString(4) %></td>
<td><a href="EditStudent.jsp?q=<%= res.getInt(1) %>"><img src="delete.png" width="30" height="30" /></a> <a href="Deletestudent.jsp?q=<%= res.getInt(1) %>"><img src="edit.png" width="30" height="30"></a></td>
</tr>
<%}
%>
</table>
</body>
</html>
EditStudent.jsp
<%--
Document : EditStudent
Created on : Mar 11, 2020, 6:02:44 PM
Author : Hp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Are You Sure to Update Record</h1>
<hr>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stuinfo","root","");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("select * from student where rno='"+request.getParameter("q") +"'");
if(res.next())
{
%>
<form action="UpdateSer" method="post">
<table>
<tr><td>RNO</td><td><input type="text" name="txtrno" value="<%= res.getInt(1) %>"</td></tr>
<tr><td>Name</td><td><input type="text" name="txtname" value="<%= res.getString(2) %>"</td></tr>
<tr><td>Branch</td><td><input type="text" name="txtbranch" value="<%= res.getString(3) %>"</td></tr>
<tr><td>Fees</td><td><input type="text" name="txtfees" value="<%= res.getString(4) %>"</td></tr>
<tr><td colspan="2"><input type="submit" name="btnsubmit" value="Update" %></td></tr>
</table>
<%
}
%>
</form>
</body>
</html>
UpdateSer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Hp
*/
public class UpdateSer extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
PrintWriter out = response.getWriter();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stuinfo","root","");
Statement st = con.createStatement();
int x = st.executeUpdate("update student set name='"+request.getParameter("txtname")+"',branch='"+request.getParameter("txtbranch")+"',fees='"+request.getParameter("txtfees")+"' where rno='"+request.getParameter("txtrno")+"'");
if(x!=0)
{
response.sendRedirect("viewstudent.jsp");
}
} catch (Exception ex) {
}
}
}
DeleteStudent
<%--
Document : EditStudent
Created on : Mar 11, 2020, 6:02:44 PM
Author : Hp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stuinfo","root","");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("select * from student where rno='"+request.getParameter("q") +"'");
if(res.next())
{
%>
<form action="DeleteSer" method="post">
<h1> Are You Sure to delete record</h1>
<hr>
<table>
<tr><td>RNO</td><td><input type="hidden" name="txtrno" value="<%= res.getInt(1) %>" /><%= res.getInt(1) %></td></tr>
<tr><td>Name</td><td><%= res.getString(2) %></td></tr>
<tr><td>Branch</td><td><%= res.getString(3) %></td></tr>
<tr><td>Fees</td><td><%= res.getString(4) %></td></tr>
<tr><td colspan="2"><input type="submit" name="btnsubmit" value="Delete" %></td></tr>
</table>
<%
}
%>
</form>
</body>
</html>
DeleteSer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Hp
*/
public class DeleteSer extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
PrintWriter out = response.getWriter();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stuinfo","root","");
Statement st = con.createStatement();
int x = st.executeUpdate("delete from student where rno='"+request.getParameter("txtrno")+"'");
if(x!=0)
{
response.sendRedirect("viewstudent.jsp");
}
} catch (Exception ex) {
}
}
}
POST Answer of Questions and ASK to Doubt