帮我写一份java连接数据库的员工管理系统
时间: 2023-06-11 20:09:55 浏览: 71
好的,以下是一个简单的Java连接MySQL数据库的员工管理系统例子:
1. 首先,需要下载并安装MySQL数据库,创建一个名为"employee"的数据库,并在该数据库中创建一个名为"employees"的表,该表包含以下字段:
- id (int)
- name (varchar)
- age (int)
- salary (double)
- department (varchar)
2. 接下来,使用Java JDBC连接数据库。在这个例子中,我们需要下载并导入MySQL JDBC驱动程序。
3. 编写代码创建一个Employee类,它包含id、name、age、salary和department属性,以及getter和setter方法。
```
public class Employee {
private int id;
private String name;
private int age;
private double salary;
private String department;
// getter and setter methods
}
```
4. 编写代码创建一个EmployeeDAO类,它包含以下方法:
- `getConnection()`:创建一个MySQL数据库连接。
- `getAllEmployees()`:获取所有员工信息。
- `getEmployeeById(int id)`:获取指定id的员工信息。
- `addEmployee(Employee employee)`:添加一个新员工。
- `updateEmployee(Employee employee)`:更新一个员工的信息。
- `deleteEmployee(int id)`:删除一个指定id的员工。
```
public class EmployeeDAO {
private Connection connection;
public EmployeeDAO() {
connection = getConnection();
}
public Connection getConnection() {
// 创建一个MySQL数据库连接
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/employee","root","password");
} catch (Exception e) {
System.out.println(e);
}
return conn;
}
public List<Employee> getAllEmployees() {
List<Employee> employees = new ArrayList<Employee>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM employees");
while (rs.next()) {
Employee employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setAge(rs.getInt("age"));
employee.setSalary(rs.getDouble("salary"));
employee.setDepartment(rs.getString("department"));
employees.add(employee);
}
} catch (SQLException e) {
e.printStackTrace();
}
return employees;
}
public Employee getEmployeeById(int id) {
Employee employee = new Employee();
try {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM employees WHERE id=?");
preparedStatement.setInt(1, id);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setAge(rs.getInt("age"));
employee.setSalary(rs.getDouble("salary"));
employee.setDepartment(rs.getString("department"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return employee;
}
public void addEmployee(Employee employee) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO employees(name, age, salary, department) VALUES (?, ?, ?, ?)");
preparedStatement.setString(1, employee.getName());
preparedStatement.setInt(2, employee.getAge());
preparedStatement.setDouble(3, employee.getSalary());
preparedStatement.setString(4, employee.getDepartment());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateEmployee(Employee employee) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE employees SET name=?, age=?, salary=?, department=? WHERE id=?");
preparedStatement.setString(1, employee.getName());
preparedStatement.setInt(2, employee.getAge());
preparedStatement.setDouble(3, employee.getSalary());
preparedStatement.setString(4, employee.getDepartment());
preparedStatement.setInt(5, employee.getId());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteEmployee(int id) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM employees WHERE id=?");
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
5. 编写代码创建一个Main类,用于测试EmployeeDAO类的方法。
```
public class Main {
public static void main(String[] args) {
EmployeeDAO employeeDAO = new EmployeeDAO();
// 获取所有员工信息
List<Employee> employees = employeeDAO.getAllEmployees();
for (Employee employee : employees) {
System.out.println(employee.getId() + " " + employee.getName() + " " + employee.getAge() + " " + employee.getSalary() + " " + employee.getDepartment());
}
// 获取指定id的员工信息
Employee employee = employeeDAO.getEmployeeById(1);
System.out.println(employee.getId() + " " + employee.getName() + " " + employee.getAge() + " " + employee.getSalary() + " " + employee.getDepartment());
// 添加一个新员工
Employee newEmployee = new Employee();
newEmployee.setName("Tom");
newEmployee.setAge(25);
newEmployee.setSalary(5000);
newEmployee.setDepartment("IT");
employeeDAO.addEmployee(newEmployee);
// 更新一个员工的信息
Employee updateEmployee = employeeDAO.getEmployeeById(1);
updateEmployee.setSalary(6000);
employeeDAO.updateEmployee(updateEmployee);
// 删除一个指定id的员工
employeeDAO.deleteEmployee(1);
}
}
```
这就是一个简单的Java连接MySQL数据库的员工管理系统例子。你可以根据自己的需求对代码进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)