8.数据访问层

entity:

package cn.epet.entity;

/**
 * 主人类
 * 
 * @author chaoyi
 * @2013-11-7
 */
public class PetOwner {
	private int id; // 主人id
	private String name; // 姓名
	private String password; // 密码
	private double money; // 元宝

	/**
	 * 获取主人id
	 * 
	 * @return 主人id
	 */
	public int getId() {
		return id;
	}

	/**
	 * 指定主人id
	 * 
	 * @param id
	 *            主人id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * 获取姓名
	 * 
	 * @return 姓名
	 */
	public String getName() {
		return name;
	}

	/**
	 * 指定姓名
	 * 
	 * @param name
	 *            姓名
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获取密码
	 * 
	 * @return 密码
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * 指定密码
	 * 
	 * @param password
	 *            密码
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 获取元宝
	 * 
	 * @return 元宝
	 */
	public double getMoney() {
		return money;
	}

	/**
	 * 指定元宝
	 * 
	 * @param money
	 *            元宝
	 */
	public void setMoney(double money) {
		this.money = money;
	}

	/**
	 * 无参构造方法
	 */
	public PetOwner() {
	}

	/**
	 * 有参构造方法
	 * 
	 * @param id
	 *            主人id
	 * @param name
	 *            姓名
	 * @param password
	 *            密码
	 * @param money
	 *            元宝
	 */
	public PetOwner(int id, String name, String password, double money) {
		this.setId(id);
		this.setName(name);
		this.setPassword(password);
		this.setMoney(money);
	}

}

 

/**
 * 
 */
package cn.epet.entity;

import java.util.Date;

/**
 * @author 北大青鸟 宠物实体类
 * 
 */
public class Pet {
	/**
	 * 宠物标识符
	 */
	private long id;

	/**
	 * 宠物名称
	 */
	private String name;

	/**
	 * 宠物类别
	 */
	private String typeName;

	/**
	 * 宠物健康指数
	 */
	private int health;

	/**
	 * 宠物爱心
	 */
	private int love;

	/**
	 * 宠物生日
	 */
	private Date birthdate;

	/**
	 * 宠物所属宠物主人标识符
	 */
	private int ownerId;

	/**
	 * 宠物所属宠物商店标识符
	 */
	private long storeId;

	/**
	 * 获取宠物所属宠物主人标识符
	 * 
	 * @return the ownerId
	 */
	public int getOwnerId() {
		return ownerId;
	}

	/**
	 * 指定宠物所属宠物主人标识符
	 * 
	 * @param ownerId
	 *            the ownerId to set
	 */
	public void setOwnerId(int ownerId) {
		this.ownerId = ownerId;
	}

	/**
	 * 获取宠物生日
	 * 
	 * @return the birthdate
	 */
	public Date getBirthdate() {
		return birthdate;
	}

	/**
	 * 获取宠物所属宠物商店标识符
	 * 
	 * @return the storeId
	 */
	public long getStoreId() {
		return storeId;
	}

	/**
	 * 指定宠物所属宠物商店标识符
	 * 
	 * @param storeId
	 *            the storeId to set
	 */
	public void setStoreId(long storeId) {
		this.storeId = storeId;
	}

	/**
	 * 指定宠物生日
	 * 
	 * @param birthdate
	 *            the birthdate to set
	 */
	public void setBirthdate(Date birthdate) {
		this.birthdate = birthdate;
	}

	/**
	 * 获取健康值
	 * 
	 * @return the health
	 */
	public int getHealth() {
		return health;
	}

	/**
	 * 指定健康值
	 * 
	 * @param health
	 *            the health to set
	 */
	public void setHealth(int health) {
		this.health = health;
	}

	/**
	 * 获致宠物标识符
	 * 
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * 指定宠物标识符
	 * 
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * 获取亲密度
	 * 
	 * @return the love
	 */
	public int getLove() {
		return love;
	}

	/**
	 * 指定亲密度
	 * 
	 * @param love
	 *            the love to set
	 */
	public void setLove(int love) {
		this.love = love;
	}

	/**
	 * 获取昵称
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * 指定昵称
	 * 
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获取类型名
	 * 
	 * @return the typeName
	 */
	public String getTypeName() {
		return typeName;
	}

	/**
	 * 指定类型名
	 * 
	 * @param typeName
	 *            the typeName to set
	 */
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
}

 

dao:

package cn.epet.dao;

import java.util.List;
import cn.epet.entity.PetOwner;

/**
 * 主人数据访问层接口
 * 
 * @author chaoyi
 * @2013-11-7
 */
public interface PetOwnerDao {
	/**
	 * 对PetOwner进行查询操作
	 * 
	 * @param sql
	 * @param pars
	 * @return list<PetOwner>
	 */
	List<PetOwner> executeQuery(String sql, Object[] pars);

	/**
	 * 对PetOwner进行增、删、改操作
	 * 
	 * @param sql
	 * @param pars
	 * @return int
	 */
	int executeUpdate(String sql, Object[] pars);
}

 

package cn.epet.dao;

import java.util.List;

import cn.epet.entity.Pet;

/**
 * 主人数据访问层接口
 * 
 * @author chaoyi
 * @2013-11-7
 */
public interface PetDao {
	/**
	 * 对PetOwner进行查询操作
	 * 
	 * @param sql
	 * @param pars
	 * @return list<PetOwner>
	 */
	List<Pet> executeQuery(String sql, Object[] pars);

	/**
	 * 对PetOwner进行增、删、改操作
	 * 
	 * @param sql
	 * @param pars
	 * @return int
	 */
	int executeUpdate(String sql, Object[] pars);
}

 

package cn.epet.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 数据连接与关闭工具类
 * 
 * @author chaoyi
 * @2013-11-7
 */
public class BaseDao {
	// 数据库驱动字符串
	private static String driver = "oracle.jdbc.driver.OracleDriver";
	// 连接URL字符串
	private static String url = "jdbc:oracle:thin:@localhost:1521:oracle10";
	// 数据库用户名
	private static String user = "epet";
	// 用户密码
	private static String password = "accp";

	/**
	 * 获取数据库连接对象
	 * 
	 * @return 连接对象
	 */
	public Connection getConnection() {
		Connection conn = null; // 数据连接对象

		// 获取连接并捕获异常
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn; // 返回连接对象
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @param conn
	 *            数据库连接
	 * @param pstmt
	 *            PreparedStatement对象
	 * @param rs
	 *            结果集
	 */
	public void closAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		// 若结果集对象不为空,则关闭
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		// 若PreparedStatement对象不为空,则关闭
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		// 若数据库连接对象不为空,则关闭
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 执行更新、删除、插入SQL语句命令
	 * 
	 * @param sql
	 * @param pars
	 * @return int
	 */
	public int executeUpdate(String sql, Object[] pars) {
		int rowCount = 0;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			// 连接数据库
			conn = this.getConnection();
			// 创建数据库命令对象
			pstmt = conn.prepareStatement(sql);
			if (pars != null) {
				for (int i = 0; i < pars.length; i++) {
					pstmt.setObject(i + 1, pars[i]);
				}
			}
			// 执行数据库命令
			rowCount = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rowCount;
	}

}

 

实现dao:

package cn.epet.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.epet.dao.BaseDao;
import cn.epet.dao.PetDao;
import cn.epet.entity.Pet;

/**
 * PetOwnerDaoImpl类继承数据库连接,实现数据访问层接口
 * 
 * @author chaoyi
 * @2013-11-7
 */
public class PetDaoImpl extends BaseDao implements PetDao {
	/**
	 * 对Pet查询操作
	 */
	public List<Pet> executeQuery(String sql, Object[] pars) {
		// 创建ArrayList集合对象并把Pet查询信息放入其中
		List<Pet> list = new ArrayList<Pet>();

		Connection conn = null; // 数据连接对象
		PreparedStatement pstmt = null; // PreparedStatement对象
		ResultSet rs = null; // 结果集对象

		try {
			// 获取数据连接对象
			conn = super.getConnection();
			// 创建数据库命令对象
			pstmt = conn.prepareStatement(sql);

			if (pars != null) {
				for (int i = 0; i < pars.length; i++) {
					pstmt.setObject(i + 1, pars[i]);
				}
			}
			// 执行数据库命令
			rs = pstmt.executeQuery();
			// 处理结果
			while (rs.next()) {
				// 创建一个宠物对象
				Pet pet = new Pet();
				pet.setId(rs.getInt(1));
				pet.setName(rs.getString(2));
				pet.setTypeName(rs.getString(3));
				pet.setHealth(rs.getInt(4));
				pet.setLove(rs.getInt(5));
				pet.setBirthdate(rs.getDate(6));
				pet.setOwnerId(rs.getInt(7));
				pet.setStoreId(rs.getLong(8));
				list.add(pet);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 调用关闭数据库连接
			super.closAll(conn, pstmt, rs);
		}

		return list;
	}

}

 

package cn.epet.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.epet.dao.BaseDao;
import cn.epet.dao.PetOwnerDao;
import cn.epet.entity.PetOwner;

/**
 * PetOwnerDaoImpl类继承数据库连接,实现数据访问层接口
 * 
 * @author chaoyi
 * @2013-11-7
 */
public class PetOwnerDaoImpl extends BaseDao implements PetOwnerDao {
	/**
	 * 对PetOwner查询操作
	 */
	public List<PetOwner> executeQuery(String sql, Object[] pars) {
		// 创建ArrayList集合对象并把PetOwner查询信息放入其中
		List<PetOwner> list = new ArrayList<PetOwner>();

		Connection conn = null; // 数据连接对象
		PreparedStatement pstmt = null; // PreparedStatement对象
		ResultSet rs = null; // 结果集对象

		try {
			// 获取数据连接对象
			conn = super.getConnection();
			// 创建数据库命令对象
			pstmt = conn.prepareStatement(sql);

			if (pars != null) {
				for (int i = 0; i < pars.length; i++) {
					pstmt.setObject(i + 1, pars[i]);
				}
			}
			// 执行数据库命令
			rs = pstmt.executeQuery();
			// 处理结果
			while (rs.next()) {
				// 创建一个主人对象
				PetOwner owner = new PetOwner();
				owner.setId(rs.getInt(1));
				owner.setName(rs.getString(2));
				owner.setPassword(rs.getString(3));
				owner.setMoney(rs.getDouble(4));
				list.add(owner);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 调用关闭数据库连接
			super.closAll(conn, pstmt, rs);
		}

		return list;
	}

}

 

biz:

package cn.epet.biz;

import cn.epet.entity.PetOwner;

/**
 * 主人业务层接口
 * 
 * @author chaoyi
 * @2013-11-8
 */
public interface PetOwnerBiz {
	/**
	 * 实现登录方法
	 * 
	 * @param owner
	 * @return 返回登录成功结果
	 */
	PetOwner login(PetOwner owner);
}

 

package cn.epet.biz;

import cn.epet.entity.Pet;

/**
 * 主人业务层接口
 * 
 * @author chaoyi
 * @2013-11-8
 */
public interface PetBiz {
	/**
	 * 添加宠物信息
	 * 
	 * @param pet
	 * @return int
	 */
	int addPetInfo(Pet pet);
}

 

实现biz:

package cn.epet.biz.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import cn.epet.biz.PetBiz;
import cn.epet.dao.PetDao;
import cn.epet.dao.impl.PetDaoImpl;
import cn.epet.entity.Pet;

/**
 * PetBizImpl类实现宠物业务层接口
 * 
 * @author chaoyi
 * @2013-11-8
 */
public class PetBizImpl implements PetBiz {
	/**
	 * 添加宠物信息
	 */
	public int addPetInfo(Pet pet) {
		// 创建一个PetDaoImpl对象
		PetDao petDao = new PetDaoImpl();

		// 操作数据字符串
		String sql = "insert into pet (ID, NAME, TYPENAME, HEALTH, LOVE, BIRTHDAY, OWNER_ID, STORE_ID)"
				+ "values (PET_SEQUENCE.nextval, ?, ?, ?, ?, to_date(?, 'yyyy-mm-dd'), ?, 1)";

		Object[] pars = { pet.getName(), pet.getTypeName(), pet.getHealth(),
				pet.getLove(),
				new SimpleDateFormat("yyyy-MM-dd").format(new Date()),
				pet.getOwnerId() };
		// 返回增加结果
		return petDao.executeUpdate(sql, pars);
	}
}

 

package cn.epet.biz.impl;

import java.util.List;
import cn.epet.biz.PetOwnerBiz;
import cn.epet.dao.PetOwnerDao;
import cn.epet.dao.impl.PetOwnerDaoImpl;
import cn.epet.entity.PetOwner;

/**
 * PetOwnerBizImpl类实现主人业务层接口
 * 
 * @author chaoyi
 * @2013-11-8
 */
public class PetOwnerBizImpl implements PetOwnerBiz {

	/**
	 * 实现登录方法
	 */
	public PetOwner login(PetOwner owner) {
		// 创建一个PetOwnerDaoImpl对象
		PetOwnerDao ownerDao = new PetOwnerDaoImpl();
		// 操作数据字符串
		String sql = "select * from petowner where name=? and password=?";
		// 创建一个对象数组
		Object[] pars = { owner.getName(), owner.getPassword() };
		// 调用进行查询操作
		List<PetOwner> list = ownerDao.executeQuery(sql, pars);

		// 判断集合中是否有数据
		if (list.size() == 1)
			// 返回集合中的第一个对象
			return list.get(0);

		return null;
	}

}

 

test:

package cn.epet.test;

import java.util.Scanner;

import cn.epet.biz.PetBiz;
import cn.epet.biz.PetOwnerBiz;
import cn.epet.biz.impl.PetBizImpl;
import cn.epet.biz.impl.PetOwnerBizImpl;
import cn.epet.entity.Pet;
import cn.epet.entity.PetOwner;

/**
 * PetOwner测试类
 * 
 * @author chaoyi
 * @2013-11-8
 */
public class PetOwnerTest {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		// 创建一个主人对象
		PetOwner owner = new PetOwner();

		// 接收用户输入登录的信息
		System.out.println("*****欢迎光临宠物乐园*****");
		System.out.print("请输入登录名:");
		owner.setName(input.next());
		System.out.print("请输入密码:");
		owner.setPassword(input.next());

		// 创建一个PetOwnerBizImpl对象
		PetOwnerBiz biz = new PetOwnerBizImpl();
		// 接收实现登录方法
		PetOwner login = biz.login(owner);
		// 判断是否登录成功
		if (login != null) {
			// 创建一个宠物对象
			Pet pet = new Pet();
			// 实现领养宠物
			System.out.println("登录成功!用户ID号是:" + login.getId());
			System.out.print("\n请输入领养宠物的名称:");
			pet.setName(input.next());
			System.out.print("请输入领养宠物的类型:");
			pet.setTypeName(input.next());
			pet.setHealth(1);
			pet.setLove(1);
			pet.setOwnerId(login.getId());
			// 创建一个PetBizImpl对象
			PetBiz petBiz = new PetBizImpl();

			// 判断是否领养成功
			if (petBiz.addPetInfo(pet) > 0) {
				System.out.println("领养成功!");
			} else
				System.out.println("领养失败!");

		} else
			System.out.println("用户名或密码错误,登录失败!");

	}

}

 

效果图:



 

个人E-mail:chaoyi77@163.com

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值