public class ORMTest extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String sql = "select username as Username,password as Password from user where id<?";
        
        Object obj[] = new Object[]{1020};
        try {
            
            List<Object> list = getObject(sql, obj, User.class);
            
            request.setAttribute("list", list);
            
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        } catch (InstantiationException | IllegalAccessException
                | IllegalArgumentException | InvocationTargetException e) {            e.printStackTrace();
        }
    }
    static List<Object> getObject(String sql,Object obj[],Class clazz) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        JDBCUtils utils = JDBCUtils.getInstance();
        List<Object> list = new ArrayList<>();
        try {
            conn = utils.getConnection();
            ps = conn.prepareStatement(sql);
            
            ParameterMetaData pmd = ps.getParameterMetaData();
            
            int count = pmd.getParameterCount();
            
            for(int i=1;i<=count;i++){
                ps.setObject(i, obj[i-1]);
            }
            
            rs=ps.executeQuery();
            
            Method ms[] = clazz.getMethods();
            
            while(rs.next()){
                
                ResultSetMetaData rsmd = rs.getMetaData();
                
                int columnCount = rsmd.getColumnCount();
                
                Object object = clazz.newInstance();
                
                for(int i=1;i<=columnCount;i++){
                    String columnLabel =rsmd.getColumnLabel(i);
                    
                    String methodName = "set"+columnLabel;
                    
                    for (Method method : ms) {
                                                if(method.getName().equals(methodName)){
                            
                            method.invoke(object, rs.getObject(columnLabel));
                        }
                    }
                }
                                list.add(object);            }        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            utils.free(conn, ps, rs);
        }
        return list;
    }
}
public class User {
    public User() {
    }
    public int id;
    public String username;
    public String password;
    public String sex;
    public String age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "id="+this.id+"username="+this.username+"password="+this.password
        +"age="+this.age+"sex:"+this.sex;
    }
}