/
//Function: 完成ResultSet对象向ArrayList对象为集合的对象的转化 //Para:sql,指定的查询Sql //Para:className,Sql相对应得JavaBean/FormBean类的名字 //Return:以类className为一条记录的结果集,完成ResultSet对象向ArrayList对象为集 //合的className对象的转化 // public ArrayList Select(String sql,String className){ ArrayList paraList=new ArrayList(); try{ if (conn == null){ Connection();
} PreparedStatement stmt = conn.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); String recordValue=""; Object c1=null; paraList=new ArrayList(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); while (rs.next()){ c1=Class.forName(className).newInstance(); for (int i=1; i<=columnCount; i++) { if(rs.getString(rsmd.getColumnName(i))!=null){ recordValue=rs.getString(rsmd.getColumnName(i)); }else{ recordValue=""; } Method m=c1.getClass().getMethod(getSetMethodName(rsmd.getColumnName(i)), new Class[]{recordValue.getClass()}); m.invoke (c1, new Object[]{recordValue}); } paraList.add(c1); } }catch(SQLException ex){ } catch(ClassNotFoundException e){ }catch(NoSuchMethodException e) { }catch(InvocationTargetException e){ }catch (IllegalAccessException e){ }catch(InstantiationException e){ } finaly{ closeConnection(); return paraList; } } |