package bean;/**  
 * 根据传入的javabean对象和ResultSet产生一个具体javaBean的Collection  
 */  
import bean.*;
import java.util.*;   
import java.sql.*;   
import java.lang.reflect.*;   public class BeanCollectionBuilder{       private BeanCollectionBuilder(){}       /**  
     * 得到一个jvaBean的Colleciton  
     * @param bean 要生成的javaBean  
     * @param rs 记录集  
     * @return 一个具体javaBean的Collection  
     */  
    public static <T> Collection<T> getBeanCollection(T object, ResultSet rs) {      Collection<T> collection = null;      Class clazzT = object.getClass();  
    Method[] methods = clazzT.getMethods();//获得bean的方法      
    List<Method> setterMethodList = new ArrayList<Method>();//构造一个List用来存放bean中所有set开头的方法          //获得Bean中所有set方法      
    for (Method method : methods) {  
        if (method.getName().startsWith("set")) {  
            setterMethodList.add(method);  
        }  
    }      ResultSetMetaData meta = null;  
    try {  
        meta = rs.getMetaData();          //如果记录集中的字段不等于Bean中的属性值,抛出异常      
        if (setterMethodList.size() != meta.getColumnCount()) {  
            throw new IllegalArgumentException("传入的JavaBean与ResultSet不一致");  
        }          //将ResultSet中的每一条记录构建一个JavaBean实例,然后添加到Collection中      
        collection = new ArrayList<T>();          while (rs.next()) {  
            T o = (T)clazzT.newInstance();              for (Method m : setterMethodList) {  
                m.invoke(o, rs.getObject(m.getName().substring(3).toLowerCase()));  
            }  
            collection.add(o);  
        }  
    } catch (Exception e) {  
        e.printStackTrace();  
          // throw e;  
    }       return collection;//最后返回这个Collection      
}  
    public static <T> Collection<T> getBeanCollection(T object, String command) { 
        return getBeanCollection(object,dis(command));
    }
 
public static Connection getcon()
    {
        String url="jdbc:microsoft:sqlserver://";
        String IP_port="127.0.0.1:1433";
        String dbname="petDB";
        String username="sa";
        String password="";
        String SQLDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";        Connection con=null;
        try
        {
            Class.forName(SQLDriver);
            url+=IP_port+";user="+username+";Password="+password+";DatabaseName="+dbname;
            con=DriverManager.getConnection(url);
        }
        catch(Exception ee)
        {
            System.out.println(ee.toString());
        }
        return con;
    }    public static boolean exec(Connection con,String command)
    {
        int cou=0;
        try
        {
         Statement stm=con.createStatement();
         cou=stm.executeUpdate(command);
         con.close();
        }catch(Exception ee)
        {
            System.out.println(ee.toString());
            cou=0;
        }
        if(cou<1)
            return false;
        else return true;
    }       public static boolean exec(String command)
    {
        return exec(getcon(),command);
    }
        public static ResultSet dis(Connection con,String command)
        {
            try
      {
        Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        ResultSet rs=stm.executeQuery(command);
        return rs;
      }
    catch(Exception ee)
    {
        System.out.println(ee.toString());
        return null;
    }
}public static ResultSet dis(String command)
{
    return dis(getcon(),command);
}
 
    public static void main(String[] args) {
        Tbean cd=new Tbean();
        int cs=BeanCollectionBuilder.getBeanCollection(cd ,"select * from test").size();
        System.out.println(cs);       
    }
}Tbean是一个纯Bean,属性和表中的字段一致,
可以通过编译,但运行出现java.lang.IllegalArgumentException: argument type mismatch
另外如何来操作返回的集合呢?