Sunday, March 7, 2021

JSP program to display, add and modify employee details

 //JSP file to display employee details

<%-- 

    Document   : employeeDetails

    Created on : 21 Mar, 2020, 6:02:56 AM

    Author     : ashu

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@ page import="java.io.*,java.util.*,java.sql.*"%>

<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

<%@ page import="java.math.*"%>

<%@ page import="java.sql.Connection"%>

<%@ page import="java.sql.DriverManager"%>

<%@ page import="java.sql.ResultSet"%>

<%@ page import="java.sql.SQLException"%>

<%@ page import="java.sql.Statement"%>

<%@ page language="java"%>

<!DOCTYPE html>

<html>

    <head>

        <title>Employees</title>

    </head>

    <body>

    <center>

        <h1>Welcome to JavaTech!</h1>

        <h3>Following is the details of our employees</h3>

        <a href="index.html">Home</a>&nbsp;&nbsp;

        <a href="addEmployee.html">Add Employee</a>&nbsp;&nbsp;

        <a href="modifyDetails.html">Modify Details</a>

        <br><br>

        <%

            try {

            String connectionURL = "jdbc:mysql://localhost/employees";

            Class.forName("com.mysql.jdbc.Driver").newInstance(); 

            Connection con = DriverManager.getConnection(connectionURL, "root", "ashutosh");

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery("select * from employee;");

        %>

        <br>   

        <table border=1>

                <tr><th>EmpID</th><th>PhoneNo</th><th>Name</th><th>Address</th><th>Department</th><th>Salary(Rs.)</th></tr>

        <%

            while(rs.next()){

        %>

            <tr>

            <td><%=rs.getInt(1)%></td>

            <td><%=rs.getInt(2)%></td>

            <td><%=rs.getString(3)%></td>

            <td><%=rs.getString(4)%></td>

            <td><%=rs.getString(5)%></td>

            <td><%=rs.getFloat(6)%></td>

            </tr>

        <%   

        }

        rs.close();

        stmt.close();

        con.close();

        }catch(Exception ex){

            out.println("Unable to connect to database"+ex);

        }   

        %>

        

    </center>

    </body>

</html>

---------------------------------------------------------------------------------------------------------------
//Corresponding HTML file

<!DOCTYPE html>
<!--
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.
-->
<html>
    <head>
        <title>JavaTech</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <center><h1>Welcome to JavaTech</h1>
        <hr>
        <h2>Manage all employee activities from here</h2>
        <hr>
        <div>
            <a href="employeeDetails.jsp">Get details of each employees</a>&nbsp;&nbsp;
            <a href="addEmployee.html">Add a new employee</a>&nbsp;&nbsp;
            <a href="modifyDetails.html">Modify existing employee details</a>
        </div>
    </center>
    </body>
</html>
----------------------------------------------------------------------------------------------------------------

//JSP file to add employee details

<%-- 
    Document   : addEmployee
    Created on : 21 Mar, 2020, 6:47:29 AM
    Author     : ashu
--%>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="java.math.*"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page language="java"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Employees</title>
    </head>
    <body>
    <center>
        <h1>Welcome to JavaTech!</h1>
        <a href="index.html">Home</a>&nbsp;&nbsp;
        <a href="modifyDetails.html">Modify Details</a>
        <h3>You can add a new employee</h3>
        
        <%
            try {
            String connectionURL = "jdbc:mysql://localhost/employees";
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            Connection con = DriverManager.getConnection(connectionURL, "root", "ashutosh");
            Statement stmt = con.createStatement();
            String ph=request.getParameter("phNum");
            String name=request.getParameter("name");
            String address=request.getParameter("address");
            String dept=request.getParameter("dept");
            String salary=request.getParameter("salary");
            long phNumInt = Long.parseLong(ph);
            float salaryFt = Float.parseFloat(salary);
            stmt.executeUpdate("INSERT INTO employee(phNum,empName,empAddress,empDept,salary) values("+phNumInt+",'"+name+"','"+address+"','"+dept+"',"+salaryFt+");");
            out.println("New employee added sucessfully!");  
            out.println("<br><a href=\"employeeDetails.jsp\">Verify the change</a>");
        stmt.close();
        con.close();
        }catch(Exception ex){
            out.println("Unable to connect to database"+ex);
        }   
       %>
    </center>
    </body>
</html>
-------------------------------------------------------------------------------------------------------------

//Corresponding HTML file

<!DOCTYPE html>
<!--
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.
-->
<html>
    <head>
        <title>Add Employee</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
        function validateForm()
        {
            var name = document.myform.name.value;
            var phNum = document.myform.phNum.value;
            var address = document.myform.address.value;
            var dept = document.myform.dept.value;
            var salary = document.myform.salary.value;
            if(name===""||name===null)
            {
                alert("User Name should not be left blank");
                document.myform.name.focus();
                return false;
            }
            if(phNum===""||phNum===null)
            {
                alert("Phone number should not be left blank");
                document.myform.phNum.focus();
                return false;
            }
            if(address===""||address===null)
            {
                alert("address should not be left blank");
                document.myform.address.focus();
                return false;
            }
            if(dept===""||dept===null)
            {
                alert("Department should be not left blank");
                document.myform.dept.focus();
                return false;
            }
            if(salary===""||salary===null)
            {
                alert("Salary should be not left blank");
                document.myfrm.salary.focus();
                return false;
            }
            //phone number must be a number
            if(isNaN(phNum)){  
                alert("Phone number can have Numeric value only");  
                return false;  
            }
            //salary must be a number
            if(isNaN(salary)){  
                alert("Salary can have Numeric value only");  
                return false;  
            }
            if(phNum.length<9){  
                alert("Phone number must be at least 9 digits long.");  
                return false;  
            }  
        }
        </script>
    </head>
    <body>
    <center><h1>Add New Employee</h1>
        <a href="index.html">Home</a>&nbsp;&nbsp;<a href="employeeDetails.jsp">Employee Details</a>&nbsp;&nbsp;<a href="modifyDetails.html">Modify existing employee details</a><br><br>
        <div>
        <form name="myform" method="post" action="addEmployee.jsp" onsubmit="return validateForm()" >  
            <table>
                <tr><td>Name:</td><td><input type="text" name="name"></td></tr> 
                <tr><td>Phone Number:</td><td><input type="text" name="phNum"></td><tr/> 
                <tr><td>Address:</td><td><input type="text" name="address"></td><tr/>
                <tr><td>Department:</td><td><input type="text" name="dept"></td><tr/>
                <tr><td>Salary:</td><td><input type="text" name="salary"></td><tr/>
                <tr><td><input type="submit" value="add"></td></tr>  
            </table>
</form>  
        </div>
    </center>
    </body>
</html>
-----------------------------------------------------------------------------------------------------------

//JSP file to modify employee details

<%-- 
    Document   : modifyDetails
    Created on : 21 Mar, 2020, 8:25:41 AM
    Author     : ashu
--%>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="java.math.*"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page language="java"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Mofify Employee Details</title>
    </head>
    <body>
    <center>
        <h1>Welcome to JavaTech!</h1>
        <a href="index.html">Home</a>&nbsp;&nbsp;
        <a href="addEmployee.html">Add Employee</a>
        <h3>You can modify employee details here</h3>
        
        <%
            try {
            String connectionURL = "jdbc:mysql://localhost/employees";
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            Connection con = DriverManager.getConnection(connectionURL, "root", "ashutosh");
            Statement stmt = con.createStatement();
            String empID="";
            String ph="";
            String name="";
            String address="";
            String dept="";
            String salary="";
            long phNumInt=0;
            float salaryFt=0;
            
            empID=request.getParameter("empid");
            ph=request.getParameter("phNum");
            name=request.getParameter("name");
            address=request.getParameter("address");
            dept=request.getParameter("dept");
            salary=request.getParameter("salary");
            int empIDInt = Integer.parseInt(empID);
            
            if(ph!=""){
                phNumInt = Long.parseLong(ph);}
            if(salary!=""){
                salaryFt = Float.parseFloat(salary);}
            if(ph!=""){
            stmt.executeUpdate("UPDATE employee SET phNum = '"+phNumInt+"'WHERE empID = "+empIDInt+";");
            }
            if(name!=""){
            stmt.executeUpdate("UPDATE employee SET empName = '"+name+"'WHERE empID = "+empIDInt+";");
            }
            if(address!=""){
            stmt.executeUpdate("UPDATE employee SET empAddress = '"+address+"'WHERE empID = "+empIDInt+";");
            }
            if(dept!=""){
            stmt.executeUpdate("UPDATE employee SET empDept = '"+dept+"'WHERE empID = "+empIDInt+";");
            }
            if(salary!=""){
            stmt.executeUpdate("UPDATE employee SET salary = '"+salaryFt+"'WHERE empID = "+empIDInt+";");
            }
            out.println("Employee details modified sucessfully!");
            out.println("<br><a href=\"employeeDetails.jsp\">Verify the change</a>");
           
            stmt.close();
            con.close();
        }catch(Exception ex){
            out.println("Unable to connect to database"+ex);
        }   
       %>
       
    </center>
    </body>
</html>
-----------------------------------------------------------------------------------------------------------

// Coresponding HTML file

<!DOCTYPE html>
<!--
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.
-->
<html>
    <head>
        <title>Modify Employee Details</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            
        function validateForm()
        {   
            var empID = "";
            var name = "";
            var phNum = "";
            var address = "";
            var dept = "";
            var salary = "";
            
            empID = document.myform.empid.value;
            name = document.myform.name.value;
            phNum = document.myform.phNum.value;
            address = document.myform.address.value;
            dept = document.myform.dept.value;
            salary = document.myform.salary.value;
            
            if(empID===""||empID===null)
            {
                alert("Employee ID should not be left blank");
//The following two lines are must for every validation alert, else it will not wait for 
//the blank text box getting filled/corrected.
                document.myform.empid.focus();
                return false;
            }
            
            var checkboxes = document.querySelectorAll('input[type="checkbox"]');
            var checkedOne = Array.prototype.slice.call(checkboxes).some(x => x.checked);
            
                       if(!checkedOne){
                 alert("'At least one checkbox must be selected.'");
                 return false;
             }
            
             
            if((document.getElementById("cbname").checked) && name==="")
            {
                alert("Employee Name should not be left blank");
                document.myform.name.focus();
                return false;
            }
            if((document.getElementById('cbphNum').checked) && phNum==="" )
            {
                alert("Employee phone number should not be left blank");
                document.myform.phNum.focus();
                return false;
            }
            if((document.getElementById('cbaddress').checked) && address==="")
            {
                alert("Employee address should not be left blank");
                document.myform.address.focus();
                return false;
            }
            if((document.getElementById('cbdept').checked) && dept==="")
            {
                alert("Employee Department should be not left blank");
                document.myform.dept.focus();
                return false;
            }
            if((document.getElementById('cbsalary').checked) && salary==="")
            {
                alert("Employee Salary should be not left blank");
                document.myform.salary.focus();
                return false;
            }
            //phone number must be a number
            if(isNaN(phNum)){  
                alert("Phone number can have Numeric value only");  
                return false;  
            }
            //salary must be a number
            if(isNaN(salary)){  
                alert("Salary can have Numeric value only");  
                return false;  
            }
            if((document.getElementById('cbphNum').checked) && phNum.length<9){  
                alert("Phone number must be at least 9 digits long.");  
                return false;  
            }
            
        }
        
        </script>
    </head>
    <body>
    <center><h1>Modify Employee Details</h1>
        <a href="index.html">Home</a>&nbsp;&nbsp;
        <a href="addEmployee.html">Add Employee</a>&nbsp;&nbsp;
        <a href="employeeDetails.jsp">Employee Details</a>
        <h3>Enter the employee ID of the employee whose details has to be modified and</h3>
        <h3>Select at least one field that you want to modify:</h3>
        <div>
        <form id="cform" name="myform" method="post" action="modifyDetails.jsp" onsubmit="return validateForm()" >  
            <table>
                <tr><td>Employee ID:</td><td><input type="text" name="empid"></td></tr>
                <tr><td><input type="checkbox" id="cbname">Name:</<td><td><input type="text" name="name"></td></tr> 
                <tr><td><input type="checkbox" id="cbphNum">Phone Number:</td><td><input type="text" name="phNum"></td><tr/> 
                <tr><td><input type="checkbox" id="cbaddress">Address:</td><td><input type="text" name="address"></td><tr/>
                <tr><td><input type="checkbox" id="cbdept">Department:</td><td><input type="text" name="dept"></td><tr/>
                <tr><td><input type="checkbox" id="cbsalary">Salary:</td><td><input type="text" name="salary"></td><tr/>
                <tr><td><input type="submit" value="modify"></td></tr>  
            </table>
</form>  
           
        </div>
    </center>
    </body>
</html>

No comments:

Post a Comment

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...