一个jdbc的封装类有问题,不知如何解决
public List getResult(ResultSet rs, Class cls) {

        List coll = new ArrayList();        try {            Method[] method = cls.getMethods();
            Field[] field = cls.getFields(); //这里可以是xml文件对应值            int count = rs.getMetaData().getColumnCount();
            int types[] = new int[count];
            String keys[] = new String[count];
            for (int i = 0; i < count; i++) {
                keys[i] = rs.getMetaData().getColumnName(i); //这里出现问题
                types[i] = rs.getMetaData().getColumnType(i);
            }            while (rs.next()) {
                Object obj = cls.newInstance();
                //通过反射得到o的所有字段!
                for (int j = 0; j < count; j++) {
                    for (int i = 0; i < method.length; i++) {
                        //if ("set".equals(method[i].getName().substring(0, 2))) {
                            //key.substring(0,1).toUpperCase();
                            if (method[i].getName().equals("set" + keys[j])) {
                                //Class[] types = method[i].getParameterTypes();
                                if (types[i] == Types.CHAR ||
                                    types[i] == Types.VARCHAR) {
                                    method[i].invoke(obj,
                                            new Object[] {rs.getString(i)});
                                }
                                if (types[i] == Types.DATE) {
                                    method[i].invoke(obj,
                                            new Object[] {rs.getDate(i)});
                                }
                                if (types[i] == Types.DOUBLE) {
                                    method[i].invoke(obj,
                                            new Object[] {Double.valueOf(i)});
                                }
                                if (types[i] == Types.FLOAT) {
                                    method[i].invoke(obj,
                                            new Object[] {Float.valueOf(i)});
                                }
                                if (types[i] == Types.BOOLEAN) {
                                    method[i].invoke(obj,
                                            new Object[] {Boolean.
                                            valueOf(rs.getBoolean(i))});
                                }
                                if (types[i] == Types.INTEGER) {
                                    method[i].invoke(obj,
                                            new Object[] {Integer.
                                            valueOf(rs.getInt(i))});
                                }
                            }
                        }
                   // }
                }
                //String value = rs.getString();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return coll;错误信息:
com.microsoft.sqlserver.jdbc.SQLServerException: 索引 0 超出范围。

解决方案 »

  1.   

    getColumnName(i); 
    从1开始。
      

  2.   

    这个方法,感觉不实用,现在都有Hibernate了
      

  3.   

    恩,对的
    example:
    # public List getResultDataList(String sql, Connection con) {  
    #         List<Map> dataList = new ArrayList<Map>();  
    #         Map<String, String> map = new HashMap<String, String>();//存放对应字段数据  
    #         PreparedStatement ps = null;  
    #         ResultSet ds = null;  
    #         ResultSetMetaData rsd;  
    #         try {  
    #             ps = con.prepareStatement(sql);  
    #             ds = ps.executeQuery();  
    #             while (ds.next()) {  
    #                 map = new HashMap<String, String>();//通过键值对存放一条记录的数据  
    #                 rsd = ds.getMetaData();  
    #                 for (int i = 1; i <= rsd.getColumnCount(); i++) {  
    #                     switch (rsd.getColumnType(i)) {//通过判断数据类型转换数据  
    #                     case 12:// varchar(12)  
    #                         map.put(rsd.getColumnName(i), ds.getString(i));  
    #                         break;  
    #                     case 1:// char(1)  
    #                         map.put(rsd.getColumnName(i), ds.getString(i));  
    #                         break;  
    #                     case -7:// bit(-7)  
    #                         map.put(rsd.getColumnName(i), Boolean.toString(ds  
    #                                 .getBoolean(i)));  
    #                         break;  
    #                     case 4:// int(4)  
    #                         map.put(rsd.getColumnName(i), Integer.toString(ds  
    #                                 .getInt(i)));  
    #                         break;  
    #                     case 5:// smallint(5)  
    #                         map.put(rsd.getColumnName(i), Integer.toString(ds  
    #                                 .getInt(i)));  
    #                         break;    
    #                     case 93:// datetime(93)  
    #                         if (ds.getString(i) == null)  
    #                             map.put(rsd.getColumnName(i), "");  
    #                         else {  
    #                             if (ds.getString(i).length() >= 19) {  
    #                                 map.put(rsd.getColumnName(i), ds.getString(i)  
    #                                         .substring(0, 19));  
    #                             } else {  
    #                                 map.put(rsd.getColumnName(i), ds.getString(i)  
    #                                         .substring(0, 10));  
    #                             }  
    #                         }  
    #                         break;  
    #                     case 3:// decimal(3)  
    #                         map.put(rsd.getColumnName(i), Double.toString(ds  
    #                                 .getDouble(i)));  
    #                         break;  
    #                     default:  
    #                         map.put(rsd.getColumnName(i), ds.getString(i));  
    #                         break;  
    #                     }  
    #                 }  
    #                 dataList.add( map);//存放所有行数据  
    #             }  
    #         } catch (SQLException e) {  
    #             e.printStackTrace();  
    #             throw new RuntimeException(e);  
    #         } finally {  
    #                         connection是通过HibernateDaoSupport获取得到的,似乎能够自动回收(反回到连接池),是否真的不用关闭连接呢?  
    #             /* 
    #              * try{ if(ds!=null){ds.close();ds=null;} 
    #              * if(ps!=null){ps.close();ps=null;} 
    #              * if(con!=null){con.close();con=null;} }catch(SQLException e){} 
    #              */  
    #         }  
    #         return dataList;  
    #     }