import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class EmployeeSource { Database datbase; public EmployeeSource(Database database) { this.datbase = database; } public ArrayList getEmployees() { try { return tryGetEmployees(); } catch (SQLException e) { System.err.println("Hiba! A lekérdezés sikertelen!"); System.err.println(e.getMessage()); return null; } } public ArrayList tryGetEmployees() throws SQLException { Connection conn = datbase.connect(); String sql = "select * from employees"; ArrayList employees = new ArrayList(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { Employee employee = new Employee(); employee.id = rs.getInt("id"); employee.name = rs.getString("name"); employee.city = rs.getString("city"); employee.salary = rs.getDouble("salary"); employees.add(employee); } return employees; } public Employee addEmployee(Employee employee) { try { return tryAddEmployee(employee); } catch (SQLException e) { System.err.println("Hiba! A hozzáadás sikertelen!"); System.err.println(e.getMessage()); return null; } } public Employee tryAddEmployee(Employee employee) throws SQLException { Connection conn = datbase.connect(); String sql = """ insert into employees (name, city, salary) values (?, ?, ?) """; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, employee.name); pstmt.setString(2, employee.city); pstmt.setDouble(3, employee.salary); pstmt.executeUpdate(); return employee; } }