nestedexceptionisjava.SQL.sqlexception : columnindexoutofrange . caused : columnindexoutofrange . Java.sql.ResultSet
resultset元数据对象
如果JDBC返回消息,则metadata.getcolumnname(I )获取column字段名称并报告下标异常
因为for循环的问题
如果追溯getColumnName的源代码,则可以看到在检索column之前调用了getField method。 在getField方法中,columnIndex从1开始,在范围外抛出异常,因此for循环从0开始。 当然下标的例外是
最后粘贴映射赋值的代码
//clazz是实体对象的publicstaticttmappergetvalue (classt clazz,ResultSet resultSet ) throwssqlexception ) tinstance=null; try { instance=clazz.newInstance (; }安装执行(illegalaccessexceptione ) { e.print堆栈跟踪); } resultsetmetadatametadata=resultset.get metadata (; for(intI=1; i=metaData.getColumnCount (; I ) { propertydescriptorpropertydescriptor=null; try { string column=metadata.getcolumn name (I; stringattr=Linetohump(column ); 属性描述符=newpropertydescriptor (attr,clazz ); } catch (防毒墙群件版) { e.printStackTrace ); }资产属性描述符!=null; methodwritemethod=property descriptor.getwritemethod (; try { write method.invoke (instance,resultset.getobject )元数据. getcolumn name ) I ) }; } catch (illegalaccessexception|invocationtargetexceptione ) { e.printStackTrace ); } }返回实例; } privatestaticpatternlinepattern=pattern.com pile (' _ ((w ) ); /**下划线表示转动驼峰*/publicstaticstringlinetohump (stringstr ) ) { str=str.toLowerCase; matcher matcher=line pattern.matcher (str ); StringBuffer sb=new StringBuffer (; while(matcher.find () matcher.appendreplacement ) sb,matcher.group(1).toUpperCase ) ); }matcher.appendtail(sb; return sb.toString (; }