java文档和源代码提供了一个解决方案:)是的,我刚刚做了,并且获得了ArrayIndexOutofBoundsException。请参阅上面我更新的代码。stackoverflow不是这样工作的。你不能接受答案,添加到你的问题中,然后继续创建新的问题。如

java文档和源代码提供了一个解决方案:)是的,我刚刚做了,并且获得了ArrayIndexOutofBoundsException。请参阅上面我更新的代码。stackoverflow不是这样工作的。你不能接受答案,添加到你的问题中,然后继续创建新的问题。如,java,jdbc,apache-commons-dbutils,Java,Jdbc,Apache Commons Dbutils,java文档和源代码提供了一个解决方案:)是的,我刚刚做了,并且获得了ArrayIndexOutofBoundsException。请参阅上面我更新的代码。stackoverflow不是这样工作的。你不能接受答案,添加到你的问题中,然后继续创建新的问题。如果您对我的答案有疑问,您可以在我的答案上发表评论。对此表示歉意,因为我不知道如何向您显示我对代码所做的更改,因此我编辑了原始问题。“添加评论”部分只允许我添加很少的信息。没问题。但stackoverflow不是这样工作的。你不能一直增加你的问题


java文档和源代码提供了一个解决方案:)是的,我刚刚做了,并且获得了
ArrayIndexOutofBoundsException
。请参阅上面我更新的代码。stackoverflow不是这样工作的。你不能接受答案,添加到你的问题中,然后继续创建新的问题。如果您对我的答案有疑问,您可以在我的答案上发表评论。对此表示歉意,因为我不知道如何向您显示我对代码所做的更改,因此我编辑了原始问题。“添加评论”部分只允许我添加很少的信息。没问题。但stackoverflow不是这样工作的。你不能一直增加你的问题。您可以发布新问题,而不是更改此问题。我相信我已经回答了您最初的问题
如何使用BeanProcessor将列名映射到属性
非常感谢bot在这方面做了这么多研究。但是,我尝试了您的代码,在线程“main”java.lang.ArrayIndexOutOfBoundsException:9中出现了
异常。我已经更新了我的原始代码库供您参考。我与映射有关,但不确定如何映射,因为我仍然无法将整个图片形象化。你不应该将其他人的答案复制到你的问题中,并从中提出新的问题。我已编辑了答案以解决您的问题。在尝试您的最新答案后,现在出现以下异常:
java.sql.SQLException:无法设置createdOn:不兼容的类型,无法将java.lang.String转换为java.util.Date查询:从状态a中选择*其中a.active='Y'参数:[]在org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:392)
你应该解释你的答案。对不起,我是中国人,我的英语很差,我想你只需要得到关键词。只需使用hashMap将列名转换为bean属性。你可以点击这里
CREATE TABLE public.states (
  state_id INTEGER DEFAULT nextval('states_seq'::regclass) NOT NULL,
  state_cd VARCHAR(2) NOT NULL,
  name VARCHAR(100) NOT NULL,
  tax_pct NUMERIC(10,2) DEFAULT 0.00 NOT NULL,
  active CHAR(1) DEFAULT 'Y'::bpchar NOT NULL,
) 
  public class State implements Serializable {

    private int stateId;
    private String stateCode;
    private String name;
    private BigDecimal taxPct = new BigDecimal(0);
    private Date expiryDate;
    private String createdBy;
    private Date createdOn;
    private String active;

    //getters and setters here
}
    public class Main {

    public static void main(String[] args) {
        String url = "jdbc:postgresql://gsi-547576.gsiccorp.net:5432/istore-db";
        String driver = "org.postgresql.Driver";
        String user = "postgres";
        String pwd = "postgres";
        Connection conn = null;
        List<State> states = null;

        try {
            DbUtils.loadDriver(driver);
            conn = DriverManager.getConnection(url, user, pwd);

            states = (List<State>) new QueryRunner().query(conn, "select * from states a where a.active='Y'", new BeanListHandler(State.class);

            System.out.println("states::  " + states);

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
    }

}
public class StateBeanProcessor extends BeanProcessor {

    @Override
    protected  int[] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props) throws SQLException {
          int[] mapping = super.mapColumnsToProperties(rsmd, props);
          /*Map database columns to fields in the order in which they appear
            1st column in the DB will be mapped to 1st field in the Java
            class and so on.. */
          for(int i=0;i<mapping.length;++i) {
             mapping[i]=i;
          }
      }
  }
states = (List<State>) new QueryRunner().query(conn, "select * from states", new BeanListHandler(State.class,new BasicRowProcessor(new StateBeanProcessor())));
public  String changeColumnName(String columnName){
        if(columnName == null){
            return null;
        }
        if(columnName.contains("_")){
            char[] cs = columnName.toCharArray();
            int flag = -1;
            for(int index=0;index<columnName.toCharArray().length;index++){
                if(cs[index] == '_'){
                    flag = index;
                    break;
                }
            }
            columnName = columnName.substring(0, flag) + columnName.substring(flag+1,flag+2).toUpperCase() + columnName.substring(flag+2);
            return changeColumnName(columnName);
        }else{
            return columnName;
        }
    }
protected int[] mapColumnsToProperties(ResultSetMetaData rsmd,
        PropertyDescriptor[] props) throws SQLException {

    int cols = rsmd.getColumnCount();
    int[] columnToProperty = new int[cols + 1];
    Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);

    for (int col = 1; col <= cols; col++) {
        String columnName = rsmd.getColumnLabel(col);
        if (null == columnName || 0 == columnName.length()) {
          columnName = rsmd.getColumnName(col);
        }
        String propertyName = columnToPropertyOverrides.get(columnName);
        if (propertyName == null) {
            propertyName = changeColumnName(columnName);//add here
        }
        for (int i = 0; i < props.length; i++) {

            if (propertyName.equalsIgnoreCase(props[i].getName())) {
                columnToProperty[col] = i;
                break;
            }
        }
    }

    return columnToProperty;
}
Map<String,String> mapColumnsToProperties = new HashMap<>();
//mapping you database to entity here;
mapColumnsToProperties.put("database_column","entity_property");
BeanProcessor beanProcessor = new BeanProcessor(mapColumnsToProperties);
RowProcessor rowProcessor = new BasicRowProcessor( beanProcessor); 
ResultSetHandler<List<Entity>> resultSetHandler = new BeanListHandler<Entity>(Entity.class,rowProcessor);
List<Entity> entityLists = queryRunner.query(findListSQL, resultSetHandler);    
// TODO initialize
QueryRunner queryRunner = null;

ResultSetHandler<List<State>> resultSetHandler =
                new BeanListHandler<State>(State.class, new BasicRowProcessor(new GenerousBeanProcessor()));

// best practice is mentioning only required columns in the query
final List<State> states = queryRunner.query("select * from states a where a.active='Y'", resultSetHandler);

for (State state : states) {
    System.out.println(state.getStateId());
}
 public class State implements Serializable {

 @JsonProperty("state_id")//db column name
 private int stateId;

 @JsonProperty("state_cd")
 private String stateCode;

 //and so on ....