diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..c6bb036 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,35 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Java CI with Maven + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn -B package --file pom.xml + + # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive + - name: Update dependency graph + uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..539c473 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..947ef88 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index a95b725..e1e0745 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,39 @@ -# EmployeeconfigrationWithSQL -This project is a simple Java application that manages employee data using an SQL database. It demonstrates basic CRUD (Create, Read, Update, Delete) operations using JDBC for database interactions. +# Employee Configuration with SQL + +A Java application for managing employee data using an SQL database. Demonstrates basic CRUD (Create, Read, Update, Delete) operations via JDBC. + +## Features + +- **CRUD Operations:** Add, view, update, and delete employee records. +- **JDBC Integration:** Connects to SQL database for data manipulation. +- **Factory Pattern:** Creates employee objects with modular design. +- **Console Interface:** Basic user interaction through command line. + +## Project Structure + +- **`com.example.model`**: `Employee` class. +- **`com.example.dao`**: `EmployeeRepository` for DB operations. +- **`com.example.service`**: `EmployeeService` for business logic. +- **`com.example.factory`**: `EmployeeFactory` for creating employees. +- **`com.example`**: Main class (`Main`) for application execution. +- - **`com.example.util`**: Contains `DatabaseConnection` class for managing database connections. +## Database Connection and Setup + +### Database Setup + +1. **Create Database and Table:** + Set up your SQL Server database and table by executing the file in util + + ## Running the Application + +To run the application, follow these steps: + +### Build the Project + +Ensure you have Maven installed. Open a terminal or command prompt and navigate to the project root directory where the `pom.xml` file is located. + +Run the following Maven command to build the project: + +```bash +mvn clean install + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..78ee5bd --- /dev/null +++ b/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + + org.example + EmployeeconfigrationWithSQL + 1.0-SNAPSHOT + + + 17 + 17 + 20.0.1 + + + + + com.microsoft.sqlserver + mssql-jdbc + 9.4.1.jre8 + + + de.jensd + fontawesomefx + 8.9.1 + + + junit + junit + 4.13.2 + test + + + org.openjfx + javafx-controls + ${javafx.version} + + + com.jfoenix + jfoenix + 9.0.10 + + + + + + + + + + jitpack.io + https://jitpack.io + + + + + + + org.openjfx + javafx-maven-plugin + 0.0.13 + + com.example.EmployeeCRUDApp + + + + + run + + + + + + + diff --git a/src/main/java/com/example/EmployeeCRUDApp.java b/src/main/java/com/example/EmployeeCRUDApp.java new file mode 100644 index 0000000..3e5ac11 --- /dev/null +++ b/src/main/java/com/example/EmployeeCRUDApp.java @@ -0,0 +1,210 @@ +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.control.*; +import javafx.scene.control.cell.PropertyValueFactory; +import javafx.scene.layout.*; +import javafx.stage.Stage; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.geometry.Insets; + +import com.example.dao.EmployeeRepository; +import com.example.model.Employee; +import com.example.model.Developer; + +import java.sql.SQLException; +import java.util.List; + +public class EmployeeCRUDApp extends Application { + + private TableView table; + private TextField nameField, emailField, positionField, salaryField; + private Button addButton, cancelButton; + private EmployeeRepository repository; + + public static void main(String[] args) { + launch(args); + } + + @Override + public void start(Stage primaryStage) { + repository = new EmployeeRepository(); + + primaryStage.setTitle("Employee CRUD Operations"); + + table = new TableView<>(); + loadTableData(); + table.getColumns().addAll( + createColumn("ID", "id", 50), + createColumn("Name", "name", 150), + createColumn("Email", "email", 200), + createColumn("Position", "position", 100), + createColumn("Salary", "salary", 100), + createActionColumn() + ); + + nameField = new TextField(); + nameField.setPromptText("Enter name"); + nameField.setMinWidth(200); + + emailField = new TextField(); + emailField.setPromptText("Enter email"); + emailField.setMinWidth(200); + + positionField = new TextField(); + positionField.setPromptText("Enter position"); + positionField.setMinWidth(200); + + salaryField = new TextField(); + salaryField.setPromptText("Enter salary"); + salaryField.setMinWidth(200); + + addButton = new Button("Add"); + addButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white;"); + addButton.setMinWidth(120); + addButton.setOnAction(e -> addEmployee()); + + cancelButton = new Button("Cancel"); + cancelButton.setStyle("-fx-background-color: #f44336; -fx-text-fill: white;"); + cancelButton.setMinWidth(120); + cancelButton.setOnAction(e -> clearFields()); + + HBox buttonBox = new HBox(10, addButton, cancelButton); + + VBox formLayout = new VBox(15, + createFormRow("Name:", nameField), + createFormRow("Email:", emailField), + createFormRow("Position:", positionField), + createFormRow("Salary:", salaryField), + buttonBox); + formLayout.setPadding(new Insets(25)); + formLayout.setStyle("-fx-background-color: white; -fx-border-color: #dcdcdc; -fx-border-width: 1px;"); + + BorderPane mainLayout = new BorderPane(); + mainLayout.setCenter(table); + mainLayout.setRight(formLayout); + mainLayout.setPadding(new Insets(25)); + mainLayout.setStyle("-fx-background-color: white;"); + BorderPane.setMargin(table, new Insets(0, 25, 0, 0)); + + Scene scene = new Scene(mainLayout, 1200, 700); + primaryStage.setScene(scene); + primaryStage.setResizable(false); + primaryStage.show(); + } + + private HBox createFormRow(String labelText, TextField textField) { + Label label = new Label(labelText); + label.setMinWidth(100); + HBox row = new HBox(10, label, textField); + row.setStyle("-fx-alignment: center-left;"); + return row; + } + + private TableColumn createColumn(String title, String property, int width) { + TableColumn column = new TableColumn<>(title); + column.setCellValueFactory(new PropertyValueFactory<>(property)); + column.setMinWidth(width); + return column; + } + + private TableColumn createActionColumn() { + TableColumn col = new TableColumn<>("Actions"); + col.setCellFactory(param -> new TableCell<>() { + final Button editButton = new Button("Edit"); + final Button deleteButton = new Button("Delete"); + + { + editButton.setStyle("-fx-background-color: yellow;"); + deleteButton.setStyle("-fx-background-color: red;"); + editButton.setOnAction(e -> editEmployee(getIndex())); + deleteButton.setOnAction(e -> deleteEmployee(getIndex())); + } + + @Override + protected void updateItem(Void item, boolean empty) { + super.updateItem(item, empty); + if (empty) { + setGraphic(null); + } else { + HBox actions = new HBox(5, editButton, deleteButton); + setGraphic(actions); + } + } + }); + return col; + } + + private void loadTableData() { + try { + table.setItems(getEmployeeList()); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private ObservableList getEmployeeList() throws SQLException { + List employees = repository.getAllEmployees(); + return FXCollections.observableArrayList(employees); + } + + private void addEmployee() { + Employee emp = new Developer( + nameField.getText(), + emailField.getText(), + positionField.getText(), + Double.parseDouble(salaryField.getText()) + ); + try { + repository.addEmployee(emp); + table.getItems().add(emp); + clearFields(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void editEmployee(int index) { + Employee selectedEmployee = table.getItems().get(index); + nameField.setText(selectedEmployee.getName()); + emailField.setText(selectedEmployee.getEmail()); + positionField.setText(selectedEmployee.getPosition()); + salaryField.setText(String.valueOf(selectedEmployee.getSalary())); + + addButton.setText("Update"); + addButton.setOnAction(e -> { + selectedEmployee.setName(nameField.getText()); + selectedEmployee.setEmail(emailField.getText()); + selectedEmployee.setPosition(positionField.getText()); + selectedEmployee.setSalary(Double.parseDouble(salaryField.getText())); + try { + repository.updateEmployee(selectedEmployee); + table.refresh(); + clearFields(); + addButton.setText("Add"); + addButton.setOnAction(event -> addEmployee()); + } catch (SQLException ex) { + ex.printStackTrace(); + } + }); + } + + private void deleteEmployee(int index) { + Employee selectedEmployee = table.getItems().get(index); + try { + repository.deleteEmployee(selectedEmployee.getId()); + table.getItems().remove(index); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void clearFields() { + nameField.clear(); + emailField.clear(); + positionField.clear(); + salaryField.clear(); + addButton.setText("Add"); + addButton.setOnAction(event -> addEmployee()); + } +} diff --git a/src/main/java/com/example/dao/EmployeeRepository.java b/src/main/java/com/example/dao/EmployeeRepository.java new file mode 100644 index 0000000..be0d0e8 --- /dev/null +++ b/src/main/java/com/example/dao/EmployeeRepository.java @@ -0,0 +1,86 @@ +package com.example.dao; + +import com.example.model.Employee; +import com.example.model.Developer; +import com.example.util.DatabaseConnection; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class EmployeeRepository { + + public void addEmployee(Employee employee) throws SQLException { + String query = "INSERT INTO employees (name, email, position, salary) VALUES (?, ?, ?, ?)"; + try (Connection conn = DatabaseConnection.getConnection(); + PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setString(1, employee.getName()); + stmt.setString(2, employee.getEmail()); + stmt.setString(3, employee.getPosition()); + stmt.setDouble(4, employee.getSalary()); + stmt.executeUpdate(); + } + } + + public List getAllEmployees() throws SQLException { + List employees = new ArrayList<>(); + String query = "SELECT * FROM employees"; + try (Connection conn = DatabaseConnection.getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(query)) { + while (rs.next()) { + Employee employee = new Developer( + rs.getString("name"), + rs.getString("email"), + rs.getString("position"), + rs.getDouble("salary") + ); + employee.setId(rs.getInt("id")); + employees.add(employee); + } + } + return employees; + } + + public Employee getEmployeeById(int id) throws SQLException { + String query = "SELECT * FROM employees WHERE id = ?"; + Employee employee = null; + try (Connection conn = DatabaseConnection.getConnection(); + PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setInt(1, id); + ResultSet rs = stmt.executeQuery(); + if (rs.next()) { + employee = new Developer( + rs.getString("name"), + rs.getString("email"), + rs.getString("position"), + rs.getDouble("salary") + ); + employee.setId(rs.getInt("id")); + } + } + return employee; + } + + public void updateEmployee(Employee employee) throws SQLException { + String query = "UPDATE employees SET name = ?, email = ?, position = ?, salary = ? WHERE id = ?"; + try (Connection conn = DatabaseConnection.getConnection(); + PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setString(1, employee.getName()); + stmt.setString(2, employee.getEmail()); + stmt.setString(3, employee.getPosition()); + stmt.setDouble(4, employee.getSalary()); + stmt.setInt(5, employee.getId()); + stmt.executeUpdate(); + } + } + + public void deleteEmployee(int id) throws SQLException { + String query = "DELETE FROM employees WHERE id = ?"; + try (Connection conn = DatabaseConnection.getConnection(); + PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setInt(1, id); + stmt.executeUpdate(); + } + } +} diff --git a/src/main/java/com/example/factory/EmployeeFactory.java b/src/main/java/com/example/factory/EmployeeFactory.java new file mode 100644 index 0000000..9d16c63 --- /dev/null +++ b/src/main/java/com/example/factory/EmployeeFactory.java @@ -0,0 +1,16 @@ +package com.example.factory; + +import com.example.model.Developer; +import com.example.model.Employee; +import com.example.model.Manager; + +public class EmployeeFactory { + public static Employee createEmployee(String type, String name, String email, String position, double salary) { + if ("Developer".equalsIgnoreCase(type)) { + return new Developer(name, email, position, salary); + } else if ("Manager".equalsIgnoreCase(type)) { + return new Manager(name, email, position, salary); + } + throw new IllegalArgumentException("Unknown employee type: " + type); + } +} diff --git a/src/main/java/com/example/model/Developer.java b/src/main/java/com/example/model/Developer.java new file mode 100644 index 0000000..6ccbde7 --- /dev/null +++ b/src/main/java/com/example/model/Developer.java @@ -0,0 +1,12 @@ +package com.example.model; + +public class Developer extends Employee { + public Developer(String name, String email, String position, double salary) { + super(name, email, position, salary); + } + + //@Override + //public void performDuties() { + //System.out.println("Writing code, debugging, and reviewing pull requests."); + //} +} \ No newline at end of file diff --git a/src/main/java/com/example/model/Employee.java b/src/main/java/com/example/model/Employee.java new file mode 100644 index 0000000..74e28b9 --- /dev/null +++ b/src/main/java/com/example/model/Employee.java @@ -0,0 +1,66 @@ +package com.example.model; + +public class Employee { + private int id; + private String name; + private String email; + private String position; + private double salary; + + public Employee() {} + + public Employee(int id, String name, String email, String position, double salary) { + this.id = id; + this.name = name; + this.email = email; + this.position = position; + this.salary = salary; + } + + public Employee(String name, String email, String position, double salary) { + this.id = 0; + this.name = name; + this.email = email; + this.position = position; + 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 getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPosition() { + return position; + } + + public void setPosition(String position) { + this.position = position; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + this.salary = salary; + } +} diff --git a/src/main/java/com/example/model/Manager.java b/src/main/java/com/example/model/Manager.java new file mode 100644 index 0000000..7e970bf --- /dev/null +++ b/src/main/java/com/example/model/Manager.java @@ -0,0 +1,11 @@ +package com.example.model; + +public class Manager extends Employee { + public Manager(String name, String email, String position, double salary) { + super(name, email, position, salary); + } + + //@Override + //public void performDuties() { + //System.out.println("Managing teams, conducting meetings, and planning projects."); + } diff --git a/src/main/java/com/example/service/EmployeeService.java b/src/main/java/com/example/service/EmployeeService.java new file mode 100644 index 0000000..a5d7e4f --- /dev/null +++ b/src/main/java/com/example/service/EmployeeService.java @@ -0,0 +1,35 @@ +package com.example.service; + +import com.example.dao.EmployeeRepository; +import com.example.model.Employee; + +import java.sql.SQLException; +import java.util.List; + +public class EmployeeService { + private final EmployeeRepository employeeRepository; + + public EmployeeService() { + this.employeeRepository = new EmployeeRepository(); + } + + public void addEmployee(Employee employee) throws SQLException { + employeeRepository.addEmployee(employee); + } + + public List getAllEmployees() throws SQLException { + return employeeRepository.getAllEmployees(); + } + + public Employee getEmployeeById(int id) throws SQLException { + return employeeRepository.getEmployeeById(id); + } + + public void updateEmployee(Employee employee) throws SQLException { + employeeRepository.updateEmployee(employee); + } + + public void deleteEmployee(int id) throws SQLException { + employeeRepository.deleteEmployee(id); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/util/DatabaseConnection.java b/src/main/java/com/example/util/DatabaseConnection.java new file mode 100644 index 0000000..82b6878 --- /dev/null +++ b/src/main/java/com/example/util/DatabaseConnection.java @@ -0,0 +1,15 @@ +package com.example.util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DatabaseConnection { + private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=EmployeeDB"; + private static final String USER = "User"; + private static final String PASSWORD = "Newpassword12"; + + public static Connection getConnection() throws SQLException { + return DriverManager.getConnection(URL, USER, PASSWORD); + } +} diff --git a/src/main/java/com/example/util/employee.sql b/src/main/java/com/example/util/employee.sql new file mode 100644 index 0000000..fedaa82 --- /dev/null +++ b/src/main/java/com/example/util/employee.sql @@ -0,0 +1,29 @@ +CREATE DATABASE EmployeeDB; + +USE EmployeeDB; + +CREATE TABLE employees ( + id INT PRIMARY KEY IDENTITY(1,1), + name NVARCHAR(100) NOT NULL, + email NVARCHAR(100) NOT NULL UNIQUE, + position NVARCHAR(50) NOT NULL, + salary DECIMAL(10, 2) NOT NULL +); + + +INSERT INTO employees (name, email, position, salary) +VALUES +('mohamed Ali', 'mohamed.ali@gmail.com', 'Developer', 7000.00), +('Mona Hassan', 'mona.hassan@gmail.com', 'Manager', 12000.00), +('Youssef Ibrahim', 'youssef.ibrahim@gmail.com', 'Developer', 8000.00); + +SELECT * FROM employees; + +UPDATE employees +SET name = 'Ali Ahmed', email = 'ali.ahmed@gmail.com', position = 'Senior Developer', salary = 9000.00 +WHERE id = 1; + +DELETE FROM employees WHERE id = 2; + +SELECT * FROM employees WHERE id = 1; + diff --git a/target/classes/EmployeeCRUDApp$1.class b/target/classes/EmployeeCRUDApp$1.class new file mode 100644 index 0000000..510ed69 Binary files /dev/null and b/target/classes/EmployeeCRUDApp$1.class differ diff --git a/target/classes/EmployeeCRUDApp.class b/target/classes/EmployeeCRUDApp.class new file mode 100644 index 0000000..a2388db Binary files /dev/null and b/target/classes/EmployeeCRUDApp.class differ diff --git a/target/classes/com/example/dao/EmployeeRepository.class b/target/classes/com/example/dao/EmployeeRepository.class new file mode 100644 index 0000000..2129cfc Binary files /dev/null and b/target/classes/com/example/dao/EmployeeRepository.class differ diff --git a/target/classes/com/example/factory/EmployeeFactory.class b/target/classes/com/example/factory/EmployeeFactory.class new file mode 100644 index 0000000..3db42c9 Binary files /dev/null and b/target/classes/com/example/factory/EmployeeFactory.class differ diff --git a/target/classes/com/example/model/Developer.class b/target/classes/com/example/model/Developer.class new file mode 100644 index 0000000..e202f75 Binary files /dev/null and b/target/classes/com/example/model/Developer.class differ diff --git a/target/classes/com/example/model/Employee.class b/target/classes/com/example/model/Employee.class new file mode 100644 index 0000000..a370afc Binary files /dev/null and b/target/classes/com/example/model/Employee.class differ diff --git a/target/classes/com/example/model/Manager.class b/target/classes/com/example/model/Manager.class new file mode 100644 index 0000000..a970699 Binary files /dev/null and b/target/classes/com/example/model/Manager.class differ diff --git a/target/classes/com/example/service/EmployeeService.class b/target/classes/com/example/service/EmployeeService.class new file mode 100644 index 0000000..87192d5 Binary files /dev/null and b/target/classes/com/example/service/EmployeeService.class differ diff --git a/target/classes/com/example/util/DatabaseConnection.class b/target/classes/com/example/util/DatabaseConnection.class new file mode 100644 index 0000000..7b22a94 Binary files /dev/null and b/target/classes/com/example/util/DatabaseConnection.class differ