1.
import java.util.*;
public class ArrayListTest{
public static void main(String dd[]){
//new了一个存储list
List l=new ArrayList();
//因为Collection framework只能存储对象所以new封装类
l.add(new Integer(1));
l.add(new Integer(2));
l.add(new Integer(3));
l.add(new Integer(4));
Iterator it=l.iterator();
//hasNext是取值取的是当前值.他的运算过程是判断下个是否有值如果有继续.
while(it.hasNext()){
//设it.next封装类,调用Integer的intValue方法返回值为int赋给i;
int i=((Integer)it.next()).intValue();
System.out.println("Element in list is : "+i);
}
}
}
2.
import java.util.*;
public class ArrayListTest1{
public static void main(String dd[]){
//new了一个存储list
List l=new ArrayList();
//因为Collection framework只能存储对象这个例子就是说明String是对象
l.add("lalala");
l.add("afdsfa");
Iterator it=l.iterator();
//hasNext是取值取的是当前值.他的运算过程是判断下个是否有值如果有继续.
while(it.hasNext()){
//设it.next封装类,调用强制转换String类型赋值给i;
String i=(String)it.next();
System.out.println("Element in list is : "+i);
}
}
}
3.
public class QueryUtil extends BasicDAO {
private static Logger log = Log.getLog(QueryUtil.class);
private Connection con = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
/**
* 完成ResultSet对象向List对象为集合的对象的转化
* @param sql,指定的查询Sql
* @param className,Sql相对应的JavaBean/FormBean类的名字:具体的Vo值对象或ActionForm
* @Return:以类className为一条记录的结果集,
* 完成ResultSet对象向List对象为集合的className对象的转化
*/
public List Select(String sql, String className) {
List arr = null; //数组来处理
try {
con = DBTool.getDAOConnection(); //取得数据库连接
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery(); //执行查询
String recordValue = ""; //构造返回的结果集
Object c1 = null;
arr = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData(); //取得数据表中的字段数目,类型等返回结果
//是以ResultSetMetaData对象保存
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});
}
log.info("取得ActionForm或VO中的set方法" +
getSetMethodName(rsmd.getColumnName(1)));
arr.add(c1);
}
} catch (SQLException ex) {
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (InvocationTargetException e) {
} catch (IllegalAccessException e) {
} catch (InstantiationException e) {
} finally {
release(rs, pstmt, con); //释放资源
}
return arr;
}
//在JavaBean封装的商业逻辑中调用Select 方法,然后在JSP页面上显示出来:
//Function:取得用户列表
//Para:
//Return:返回用户列表
public List getUsers(){
List ret=null;
DatabaseManage db=new DatabaseManage();
String sql=" select usr_id,usr_name "
+" from users " ;
ret=db.Select(sql," com.mcsky. webis.system.UsersActionForm");
return ret;
}
public static void main(String[] args) {
//方法测试通过
String sql =
"select chkr_code,chkr_name,chkr_value chkr_regular from checkrule "; //查询告警用户过滤设置明细
QueryUtil util = new QueryUtil();
List arr = null;
arr = util.Select(sql, "com.scitel.crms.web.form.TestActionForm");
System.out.println("集合大小------>>>>>>>" + arr.size());
//System.out.println("集合大小------>>>>>>>"+arr.toArray().toString());
}
}