Java

Insert and Display Records Using HTTP Servlet

The following article describes how to Insert and Display Records Using HTTP Servlet.

Basically, here we have the following problem. Insert and Display Records Using HTTP Servlet in a database with the name WBPResult with table SecondSemResult. Further, a row with the student name “Anmol”, Enrolment No. “1212347822”, Internal Marks=16, and external marks = 47. Also, a total marks field will be there.

The following code demonstrates how to create an HTTP servlet to insert and display records in a database with specific requirements.

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResultServlet extends HttpServlet {
  private static final String DB_URL = "jdbc:mysql://localhost:3306/WBPResult";
  private static final String DB_USER = "root";
  private static final String DB_PASSWORD = "password";

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
      // Load the JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      // Connect to the database
      conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

      // Insert a new record into the SecondSemResult table
      stmt = conn.prepareStatement("INSERT INTO SecondSemResult (name, enrolment_no, internal_marks, external_marks, total_marks) VALUES (?, ?, ?, ?, ?)");
      stmt.setString(1, "Anmol");
      stmt.setString(2, "1212347822");
      stmt.setInt(3, 16);
      stmt.setInt(4, 47);
      stmt.setInt(5, 63);
      int rowsInserted = stmt.executeUpdate();

      // Display all the records from the SecondSemResult table
      stmt = conn.prepareStatement("SELECT * FROM SecondSemResult");
      rs = stmt.executeQuery();

      // Set the response content type
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      // Generate the HTML table to display the results
      out.println("<html>");
      out.println("<head><title>Second Semester Result</title></head>");
      out.println("<body>");
      out.println("<h1>Second Semester Result</h1>");
      out.println("<table border='1'>");
      out.println("<tr><th>Name</th><th>Enrolment No.</th><th>Internal Marks</th><th>External Marks</th><th>Total Marks</th></tr>");
      while (rs.next()) {
        out.println("<tr>");
        out.println("<td>" + rs.getString("name") + "</td>");
        out.println("<td>" + rs.getString("enrolment_no") + "</td>");
        out.println("<td>" + rs.getInt("internal_marks") + "</td>");
        out.println("<td>" + rs.getInt("external_marks") + "</td>");
        out.println("<td>" + rs.getInt("total_marks") + "</td>");
        out.println("</tr>");
      }
      out.println("</table>");
      out.println("</body></html>");

    } catch (ClassNotFoundException | SQLException e) {
      e.printStackTrace();
    } finally {
      // Close the database resources
      try {
        if (rs != null) {
          rs.close();
        }
        if (stmt != null) {
          stmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

In this example, we have created a servlet class named ResultServlet that extends HttpServlet. In the doGet method, we load the JDBC driver, connect to the database using the DriverManager.getConnection method, and execute an INSERT statement to add a new record to the SecondSemResult table. We then execute a SELECT statement to get all the records from the SecondSemResult table and display them.


Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise

programmingempire

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *