详细写出employee的dao
时间: 2023-08-31 14:06:05 浏览: 108
Employee DAO(Data Access Object)是一种用于访问数据库的设计模式,用于处理相关实体的持久化数据。下面是一个简单的Employee DAO示例,它使用JDBC连接到数据库。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class EmployeeDAO {
private Connection connection;
public EmployeeDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public List<Employee> getAllEmployees() {
List<Employee> employees = new ArrayList<>();
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM employees");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String department = resultSet.getString("department");
double salary = resultSet.getDouble("salary");
employees.add(new Employee(id, name, department, salary));
}
} catch (SQLException e) {
e.printStackTrace();
}
return employees;
}
public Employee getEmployeeById(int id) {
Employee employee = null;
try {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM employees WHERE id = ?");
preparedStatement.setInt(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
String name = resultSet.getString("name");
String department = resultSet.getString("department");
double salary = resultSet.getDouble("salary");
employee = new Employee(id, name, department, salary);
}
} catch (SQLException e) {
e.printStackTrace();
}
return employee;
}
public void addEmployee(Employee employee) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO employees (name, department, salary) VALUES (?, ?, ?)");
preparedStatement.setString(1, employee.getName());
preparedStatement.setString(2, employee.getDepartment());
preparedStatement.setDouble(3, employee.getSalary());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateEmployee(Employee employee) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE employees SET name=?, department=?, salary=? WHERE id=?");
preparedStatement.setString(1, employee.getName());
preparedStatement.setString(2, employee.getDepartment());
preparedStatement.setDouble(3, employee.getSalary());
preparedStatement.setInt(4, 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();
}
}
public void closeConnection() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
上面的代码演示了如何使用JDBC API连接到数据库,执行SQL查询和更新语句,并从结果集中检索数据。Employee DAO中的方法用于从数据库中检索,添加,更新和删除Employee对象。每个方法都使用PreparedStatement对象执行SQL语句,该对象支持参数化查询,可以防止SQL注入攻击。此外,Employee DAO在构造函数中初始化数据库连接,并提供了一个关闭连接的方法,以确保资源在不再需要时被正确释放。
阅读全文