package com.sample.dao;

import java.util.ArrayList;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.dbcp.BasicDataSource;

import com.sample.entity.Employee;


public class EmployeeDaoImpl implements EmployeeDao {
	
	// 数据源
	private BasicDataSource dataSource;

    public ArrayList<Employee> getList() {
    	ArrayList<Employee> tempList = new ArrayList<Employee>();
    	
    	try {
            Connection conn = dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM `employees`");
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                Employee employee = new Employee();
                employee.setName(rs.getString("name"));
                employee.setAge(rs.getInt("age"));
                employee.setEmail(rs.getString("email"));
                tempList.add(employee);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    	
    	return tempList;
    }
    
    public void setDataSource(BasicDataSource dataSource) {
    	this.dataSource = dataSource;
    }

    public BasicDataSource getDataSource() {
        return dataSource;
    }
}
