01 数据库数据封装到模型中

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载,博客地址:http://blog.csdn.net/xpala https://blog.csdn.net/xpala/article/details/89154380

新建模型Account.java

package com.xpw.model;

public class Account {
	private int id;
	private String name;
	private double money;
	public Account() {
		super();
		
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", name=" + name + ", money=" + money + "]";
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	
}

在before_dbutils.java中使用该模型

public class before_dbutils {
	
	@Test
	public void test1() throws SQLException{
		//1.获取连接对象
		Connection con=C3P0Utils.getConnection();
		
		//2.获取statement对象
		Statement stmt=con.createStatement();
		
		//3.执行查询
		ResultSet rs=stmt.executeQuery("select * from account");
		
		//4.遍历
		List <Account> list = new ArrayList<Account>();
		
		while(rs.next()){
			Account account=new Account();
			account.setId(rs.getInt("id"));
			account.setName(rs.getString("name"));
			account.setMoney(rs.getDouble("money"));
			list.add(account);
		}
		for (Account a:list){
			System.out.println(a);
		}
		//5.关闭
		C3P0Utils.closeAll(con, stmt, rs);		
	}
}

猜你喜欢

转载自blog.csdn.net/xpala/article/details/89154380