首页>代码>jsp+servlet+mysql员工管理系统源代码下载>/Emp/src/dao/jdbc/EmployeeDAOJdbcImpl.java
package dao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.DBConnection;
import dao.EmployeeDAO;
import entity.Employee;

public class EmployeeDAOJdbcImpl implements EmployeeDAO {

	public List<Employee> findALL() throws Exception {
		ArrayList<Employee> list = new ArrayList<Employee>();
		Connection conn = DBConnection.getConnection();
		PreparedStatement ps = conn.prepareStatement("select * from t_emp");
		ResultSet rs = ps.executeQuery();
		
		while(rs.next()) {
			Employee e = new Employee();
			e.setId(rs.getInt("id"));
			e.setName(rs.getString("name"));
			e.setAge(rs.getInt("age"));
			e.setSalary(rs.getDouble("salary"));
			list.add(e);
		}
		DBConnection.close(rs, ps, conn);
		return list;
	}
	
	public void insertEmp(Employee e) throws Exception {
		Connection conn = DBConnection.getConnection();
		PreparedStatement ps = conn.prepareStatement("insert into t_emp(name,age,salary) values(?,?,?)");
		ps.setString(1, e.getName());
		ps.setInt(2,e.getAge());
		ps.setDouble(3, e.getSalary());
		ps.executeUpdate();
		System.out.println("插入成功");
		DBConnection.close(ps, conn);
		
	}
	
	public void deleteEmpById(int id) throws Exception {
		Connection conn = DBConnection.getConnection();
		PreparedStatement ps = conn.prepareStatement("delete from t_emp where id=?");
		ps.setInt(1,id);
		ps.executeUpdate();
		
		DBConnection.close(ps, conn);
	}
	
	public void updateEmp(Employee e) throws Exception {
		Connection conn = DBConnection.getConnection();
		PreparedStatement ps = conn.prepareStatement("update t_emp set name=?,age=?,salary=? where id=?");
		ps.setString(1, e.getName());
		ps.setInt(2,e.getAge());
		ps.setDouble(3, e.getSalary());
		ps.setInt(4, e.getId());
		ps.executeUpdate();
		
		DBConnection.close(ps, conn);
	}
	
	public Employee findEmpById(int id) throws Exception {
		Employee e = null;
		Connection conn = DBConnection.getConnection();
		PreparedStatement ps = conn.prepareStatement("select * from t_emp where id=?");
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		
		while(rs.next()) {
			e = new Employee();
			e.setId(rs.getInt("id"));
			e.setName(rs.getString("name"));
			e.setAge(rs.getInt("age"));
			e.setSalary(rs.getDouble("salary"));
		}
		DBConnection.close(rs, ps, conn);
		return e;
		
	}


	
}
最近下载更多
山晴多  LV1 6月6日
2022102154  LV1 4月3日
小橘子是你啊  LV1 3月31日
新哥新奇士橙  LV4 1月26日
cxdddd  LV1 2024年12月31日
sinuodeng123  LV5 2024年12月13日
jiafei  LV1 2024年12月11日
b1uccc  LV2 2024年12月8日
blank1  LV1 2024年11月28日
最近浏览更多
GuanYang551 6月21日
暂无贡献等级
gigfkk 6月11日
暂无贡献等级
落花清阳 6月7日
暂无贡献等级
山晴多  LV1 6月6日
luhong  LV4 5月6日
222ada  LV1 4月23日
小橘子是你啊  LV1 3月31日
2022102154  LV1 3月27日
叉烧君  LV3 3月26日
feng0519 3月25日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友