0% found this document useful (0 votes)
8 views

day 10 emp servlet conn

The document contains Java code for an employee management system, including classes for Employee, database connection (DBUtil), and data access object (EmplDAO) to retrieve employee data from a database. It also includes servlet code to handle HTTP requests and JSP code to display employee information in a table format. The system allows users to view all employees by fetching data from the database and presenting it on a web page.

Uploaded by

rethinakumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

day 10 emp servlet conn

The document contains Java code for an employee management system, including classes for Employee, database connection (DBUtil), and data access object (EmplDAO) to retrieve employee data from a database. It also includes servlet code to handle HTTP requests and JSP code to display employee information in a table format. The system allows users to view all employees by fetching data from the database and presenting it on a web page.

Uploaded by

rethinakumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<a href="ViewAllEmployees">View all employees</a>

-----------------------------
Employee.java
-----------------------

package empbean; public class Employee { ... by Athma M


Athma M
12:07 PM
package empbean;

public class Employee {


private int emp_id;
private String emp_name;
private int emp_sal;
public Employee() {
super();
}
public Employee(int emp_id, String emp_name, int emp_sal) {
super();
this.emp_id = emp_id;
this.emp_name = emp_name;
this.emp_sal = emp_sal;
}
public int getEmp_id() {
return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public int getEmp_sal() {
return emp_sal;
}
public void setEmp_sal(int emp_sal) {
this.emp_sal = emp_sal;
}
public String toString() {
return "Employee Details[Emp_ID=" + emp_id + ", emp_name=" + emp_name +
", emp_sal=" + emp_sal + "]";
}
}

-----------------------------
DBUtil.java

------------------------

public static Connection getDBConnection() {


Connection con = null;
// code
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); // Oracle DB
con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "wcf20jan",
"wcf20jan");
} catch (Exception ex) {
}
return con;
}

--------------------------------
EmplDAO.java change according your table
--------------------------

public ArrayList<Employee> getAllEmployee(){


ArrayList<Employee> empList = new ArrayList<Employee>();
//code
try {
Connection con = DBUtil.getDBConnection();
String sql ="Select * from Emp ";
PreparedStatement ps =con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
int emp_id = rs.getInt("EMPLOYEE_ID");
String emp_name = rs.getString("FIRST_NAME");
int emp_sal = rs.getInt("SALARY");
Employee emp = new Employee(emp_id,emp_name,emp_sal);
empList.add(emp);
}
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
return empList;
}

--------------------------
ViewAllEmployees.java
------------------------------

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
EmplDAO eDAO = new EmplDAO();
ArrayList<Employee> empList = eDAO.getAllEmployee();
request.setAttribute("eList", empList);
request.getRequestDispatcher("ViewAllEmp.jsp").forward(request,
response);
}

---------------

NewFile.jsp
----------------------------
<body>

<br>
<a href="ViewEmployee" class="btn btn-primary">View All Employees</a>
<br>
<hr>

<%
ArrayList<Employee> empList = (ArrayList<Employee>)
request.getAttribute("eList");
if (empList != null) {
%>
<table class="table table-bordered table-striped">
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Salary</th>
</tr>
<%
for (Employee emp : empList) {
%>
<tr>
<td><%= emp.getEmp_id() %></td>
<td><%= emp.getEmp_name() %></td>
<td><%= emp.getEmp_sal() %></td>
</tr>
<%
}

}
%>
</table>
</body>

change according your code

<%@ page import="java.util.ArrayList"%> <%@... by Athma M


Athma M
12:27 PM
<%@ page import="java.util.ArrayList"%>
<%@ page import="empbean.Employee"%>

<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

-----------------
viewallemp.jsp
------------------------------

You might also like