Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.employeemanager;

public class Employee {
private int id;
private String name;
private String department;
private double salary;

public Employee(int id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", department='" + department + '\'' +
", salary=" + salary +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.example.employeemanager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDAO {
private final String url = "jdbc:mysql://localhost:3306/employeedb";
private final String user = "root";
private final String password = "password";

public Connection connect() throws SQLException {
return DriverManager.getConnection(url, user, password);
}

public void addEmployee(Employee employee) {
String sql = "INSERT INTO employees (id, name, department, salary) VALUES (?, ?, ?, ?)";
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, employee.getId());
pstmt.setString(2, employee.getName());
pstmt.setString(3, employee.getDepartment());
pstmt.setDouble(4, employee.getSalary());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

public List<Employee> getAllEmployees() {
List<Employee> employees = new ArrayList<>();
String sql = "SELECT * FROM employees";
try (Connection conn = connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Employee employee = new Employee(
rs.getInt("id"),
rs.getString("name"),
rs.getString("department"),
rs.getDouble("salary")
);
employees.add(employee);
}
} catch (SQLException e) {
e.printStackTrace();
}
return employees;
}

public void updateEmployee(Employee employee) {
String sql = "UPDATE employees SET name = ?, department = ?, salary = ? WHERE id = ?";
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, employee.getName());
pstmt.setString(2, employee.getDepartment());
pstmt.setDouble(3, employee.getSalary());
pstmt.setInt(4, employee.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void deleteEmployee(int id) {
String sql = "DELETE FROM employees WHERE id = ?";
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.employeemanager;

import java.util.List;

public class Main {
public static void main(String[] args) {
EmployeeDAO employeeDAO = new EmployeeDAO();

// Add a new employee
Employee emp1 = new Employee(1, "John Doe", "Engineering", 75000);
employeeDAO.addEmployee(emp1);

// Retrieve all employees
List<Employee> employees = employeeDAO.getAllEmployees();
System.out.println("All Employees:");
for (Employee emp : employees) {
System.out.println(emp);
}

// Update an employee
emp1.setSalary(80000);
employeeDAO.updateEmployee(emp1);

// Delete an employee
employeeDAO.deleteEmployee(1);
}
}