---
title: "ResultSet ——>List"
author: "kkllmey"
date: "2008-09-26T15:58:59Z"
tags: []
slug: "resultset-list"
draft: false
lastmod: "2008-09-26T15:58:59Z"
id: 247126
url: "https://ibc.iteye.com/blog/247126"
---

```
package com.service;

import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import com.entity.Student;

public class Service {
	public List toList(ResultSet rs,Class cls)
	{
		try
		{
			
			List lst=new ArrayList();
			ResultSetMetaData meta=rs.getMetaData();
			Object obj=null;
			while(rs.next())
			{
				obj=Class.forName(cls.getName()).newInstance();
				for(int i=1;i<=meta.getColumnCount();i++)
				{
					String colName=meta.getColumnName(i);
					colName=colName.replace(colName.charAt(0)+"", new String(colName.charAt(0)+"").toUpperCase());
					String methodName="set"+colName;
					System.out.println(methodName);
					Object value=rs.getObject(i);
					Method method=obj.getClass().getMethod(methodName, value.getClass());
					method.invoke(obj, value);
				}
				lst.add(obj);
			}
			
			return lst;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
		
	}
}
```