java+sqlserver做员工管理系统代码
时间: 2023-12-14 13:03:29 浏览: 186
很抱歉,我不能提供完整的员工管理系统代码,因为这需要花费大量的时间和精力来完成。但是,我可以给出一些代码示例来帮助您开始,这些示例可以帮助您实现员工管理系统的基本功能。
首先,您需要创建一个Employee类,该类应包含员工的基本信息,如ID,姓名,职位,薪水等。
```java
public class Employee {
private int id;
private String name;
private String position;
private double salary;
public Employee(int id, String name, String position, double salary) {
this.id = id;
this.name = name;
this.position = position;
this.salary = salary;
}
// getters and setters
}
```
然后,您需要创建一个EmployeeDAO类,该类应包含与数据库交互的方法,例如添加员工,删除员工和更新员工信息等。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class EmployeeDAO {
private Connection connection;
public EmployeeDAO(Connection connection) {
this.connection = connection;
}
public void addEmployee(Employee employee) throws SQLException {
String query = "INSERT INTO employees (id, name, position, salary) VALUES (?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(query);
statement.setInt(1, employee.getId());
statement.setString(2, employee.getName());
statement.setString(3, employee.getPosition());
statement.setDouble(4, employee.getSalary());
statement.executeUpdate();
}
public void deleteEmployee(int id) throws SQLException {
String query = "DELETE FROM employees WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setInt(1, id);
statement.executeUpdate();
}
public void updateEmployee(Employee employee) throws SQLException {
String query = "UPDATE employees SET name = ?, position = ?, salary = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, employee.getName());
statement.setString(2, employee.getPosition());
statement.setDouble(3, employee.getSalary());
statement.setInt(4, employee.getId());
statement.executeUpdate();
}
public List<Employee> getAllEmployees() throws SQLException {
List<Employee> employees = new ArrayList<>();
String query = "SELECT * FROM employees";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String position = resultSet.getString("position");
double salary = resultSet.getDouble("salary");
Employee employee = new Employee(id, name, position, salary);
employees.add(employee);
}
return employees;
}
}
```
接下来,您需要创建一个Main类,该类应包含用户界面和对EmployeeDAO方法的调用。
```java
import java.sql.*;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=employees", "username", "password");
EmployeeDAO employeeDAO = new EmployeeDAO(connection);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add employee");
System.out.println("2. Delete employee");
System.out.println("3. Update employee");
System.out.println("4. Get all employees");
System.out.println("5. Exit");
int choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1:
System.out.print("Enter employee ID: ");
int id = Integer.parseInt(scanner.nextLine());
System.out.print("Enter employee name: ");
String name = scanner.nextLine();
System.out.print("Enter employee position: ");
String position = scanner.nextLine();
System.out.print("Enter employee salary: ");
double salary = Double.parseDouble(scanner.nextLine());
Employee employee = new Employee(id, name, position, salary);
employeeDAO.addEmployee(employee);
System.out.println("Employee added successfully!");
break;
case 2:
System.out.print("Enter employee ID: ");
int idToDelete = Integer.parseInt(scanner.nextLine());
employeeDAO.deleteEmployee(idToDelete);
System.out.println("Employee deleted successfully!");
break;
case 3:
System.out.print("Enter employee ID: ");
int idToUpdate = Integer.parseInt(scanner.nextLine());
System.out.print("Enter new employee name: ");
String newName = scanner.nextLine();
System.out.print("Enter new employee position: ");
String newPosition = scanner.nextLine();
System.out.print("Enter new employee salary: ");
double newSalary = Double.parseDouble(scanner.nextLine());
Employee employeeToUpdate = new Employee(idToUpdate, newName, newPosition, newSalary);
employeeDAO.updateEmployee(employeeToUpdate);
System.out.println("Employee updated successfully!");
break;
case 4:
List<Employee> employees = employeeDAO.getAllEmployees();
for (Employee e : employees) {
System.out.println(e.getId() + " " + e.getName() + " " + e.getPosition() + " " + e.getSalary());
}
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
这是一个简单的示例,可以帮助您开始使用Java和SQL Server实现员工管理系统。请注意,此示例可能需要进行一些修改,以适合您的具体需求和环境。
阅读全文