ArrayList集合存储VO封装对象后调用的问题

时间:2020-01-12
本文章向大家介绍ArrayList集合存储VO封装对象后调用的问题,主要包括ArrayList集合存储VO封装对象后调用的问题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

VO代码:

public class VO4Blog {
	private int b_id;
	private int b_typeid;
	private String b_title;
	private String b_intro;
	private String b_content;
	private String b_posttime;
	private String b_viewnums;
	

	public int getB_id() {
		return b_id;
	}

	public void setB_id(int b_id) {
		this.b_id = b_id;
	}

	public int getB_typeid() {
		return b_typeid;
	}

	public void setB_typeid(int b_typeid) {
		this.b_typeid = b_typeid;
	}

	public String getB_title() {
		return b_title;
	}

	public void setB_title(String b_title) {
		this.b_title = b_title;
	}

	public String getB_intro() {
		return b_intro;
	}

	public void setB_intro(String b_intro) {
		this.b_intro = b_intro;
	}

	public String getB_content() {
		return b_content;
	}

	public void setB_content(String b_content) {
		this.b_content = b_content;
	}

	public String getB_posttime() {
		return b_posttime;
	}

	public void setB_posttime(String b_posttime) {
		this.b_posttime = b_posttime;
	}

	public String getB_viewnums() {
		return b_viewnums;
	}

	public void setB_viewnums(String b_viewnums) {
		this.b_viewnums = b_viewnums;
	}
}

  将VO对象放进arraylst集合的代码:

public static List<VO4Blog> doCheckConByOrder() {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		VO4Blog vo = null;
		List<VO4Blog> vols = new ArrayList<VO4Blog>();
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/myblog","root","root");
			st = con.createStatement();
			String sql = "select * from mb_blog order by b_posttime desc";
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int b_id = rs.getInt("b_id");
				int b_typeid = rs.getInt("b_typeid");
				String b_title = rs.getString("b_title");
				String b_intro = rs.getString("b_intro");
				String b_content = rs.getString("b_content");
				String b_posttime = rs.getString("b_posttime");
				String b_viewnums = rs.getString("b_viewnums");
				//封装对象
				vo = new VO4Blog();
				vo.setB_id(b_id);
				vo.setB_typeid(b_typeid);
				vo.setB_title(b_title);
				vo.setB_intro(b_intro);
				vo.setB_content(b_content);
				vo.setB_posttime(b_posttime);
				vo.setB_viewnums(b_viewnums);
				vols.add(vo);
			}
		}catch(Exception ex){
			System.out.println(ex);
		}finally{
			try{
				if(rs!=null){
					rs.close();
				}
				if(st!=null){
					st.close();
				}
				if(con!=null){
					con.close();
				}
			}catch(Exception ex){
				System.out.println(ex);
			}
		}
		return vols;
	}

  定义一个List对象并调用里面的VO对象的属性值:

List<VO4Blog> vols = DBUtil.doCheckConByOrder();
		

 由于add()添加的是Object类型对象,可以使用迭代器取出的对象也是Object,所以要强制转换为VO4Blog对象:

Iterator it = vols.iterator();
while(it.hasNext()){
    VO4Blog vo = (VO4Blog)it.next();
    System.out.println(vo.getB_id());
}

  此时才能正常打印出vo.getB_id()

如果您路过看到,有更好的方法请多多指教,提出更好方法(谢谢!)。

原文地址:https://www.cnblogs.com/AlexFung/p/12183316.html