4. write a servlet program that accepts roll number from the student and obtains the result "pass class", "first-class" etc by checking the appropriate fields from the student's table.
Prerequisite :
- MySQL Server.
- MySQL-connector-java.jar
- An IDE (Netbeans, eclipse or your favorite one )
- A Database with at least one table in it.
All of them or anyone can be downloaded from the mentioned link or from the official websites.
You will have to create two files namely StudentResult.html, StudentResult.java (Servlet), and will be required one database table, let's name it students.
You will have to create two files namely StudentResult.html, StudentResult.java (Servlet), and will be required one database table, let's name it students.
StudentResult.html
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form method="post" action="StudResult"> Enter Your Roll Number : <input type="text" name="rollno"/> <br> <input type="submit" value="Get Result"/> </form> </body> </html>
StudentResult.java
import java.io.IOException;
import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Shubham Singh (https://shubhamsingh.dev/) */ @WebServlet(urlPatterns = {"/StudResult"}) public class StudResult extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { out.println("<html>"); out.println("<body>"); int rollno = Integer.parseInt(request.getParameter("rollno")); int per=0; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root",""); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("Select percentage from Student where rollno="+rollno+";"); while(rs.next()==true) { per = Integer.parseInt(rs.getString(1)); } if(per>75) out.println("<h1> First Class </h1>"); else if(per<=75 && per>55) out.println("<h1> Second Class </h1>"); else if(per<=55 && per>35) out.println("<h1> Pass Class </h1>"); else out.println("<h1> Fail </h1>"); out.println("</body>"); out.println("</html>"); } catch(ClassNotFoundException | NumberFormatException | SQLException e) { out.println("Error : "+e); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override public String getServletInfo() { return "Short description"; } }
And finally, Create a Student Table and Insert Some Values.
CREATE TABLE STUDENT ( ROLLNO INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, CLASS CHAR (25), PERCENTAGE CHAR (10), PRIMARY KEY (ROLLNO) ); INSERT INTO STUDENT VALUES (101, "Shubham", 23,"Science", 68); INSERT INTO STUDENT VALUES (102, "Salim", 23,"Maths", 70); INSERT INTO STUDENT VALUES (103, "Manisha", 25,"Geography", 75); INSERT INTO STUDENT VALUES (104, "Nagesh", 26,"Computer Science", 60); INSERT INTO STUDENT VALUES (105, "Sagar", 21,"Civics", 50); INSERT INTO STUDENT VALUES (106, "Aditya", 22,"Biology", 90); INSERT INTO STUDENT VALUES (107, "Tushar", 26,"History", 60); INSERT INTO STUDENT VALUES (108, "Mansi", 22,"Chemistry", 80); INSERT INTO STUDENT VALUES (109, "Pooja", 23,"General knowledge", 92); INSERT INTO STUDENT VALUES (110, "Sheela", 23,"Physics", 30);
Before Running the program please check if you have placed MySQL-connector-java.jar in Libraries folder.
No comments:
Post a Comment