一个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.   

    但是数组下标还是从零开始,所以应该这么写
    for (int i = 0; i < count; i++) {
        keys[i] = rs.getMetaData().getColumnName(i + 1);
        types[i] = rs.getMetaData().getColumnType(i + 1);
    }
      

  2.   

    for (int i = 0; i < count; i++) {
        keys[i] = rs.getMetaData().getColumnName(i + 1);
        types[i] = rs.getMetaData().getColumnType(i + 1);
    }
    就这样了