entity(实体层) 和 dao(数据访问层)_sql

 

entity(实体层)

package cn.jbit.entity;

import java.io.Serializable;
public class BookManage implements Serializable {
	private int id;//id
	private String name;//图书名称
	private String author;//图书作者
	private String time;//购买时间
	private String type;//图书分类
	
	public BookManage() {}//无参构造方法
	public BookManage(int id, String name, String author, String time,
			String type) {//有参构造方法
		this.id = id;
		this.name = name;
		this.author = author;
		this.time = time;
		this.type = type;
	}
	public int getId() {//得到 id 
		return id;
	}
	public void setId(int id) {//设置 id
		this.id = id;
	}
	public String getName() {//得到 图书名称
		return name;
	}
	public void setName(String name) {//设置 图书名称
		this.name = name;
	}
	public String getAuthor() {//得到 图书作者
		return author;
	}
	public void setAuthor(String author) {//设置 图书作者
		this.author = author;
	}
	public String getTime() {//得到 购买时间
		return time;
	}
	public void setTime(String time) {//设置 购买时间
		this.time = time;
	}
	public String getType() {//得到 图书分类
		return type;
	}
	public void setType(String type) {//设置 图书分类
		this.type = type;
	}
}

 

dao(数据访问层)

package cn.jbit.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 数据库连接与关闭工具类。
 */
public class BaseDao {
	private static String driver ="oracle.jdbc.driver.OracleDriver";// 数据库驱动字符串
	private static String url = "jdbc:oracle:thin:@localhost:1521:oracle11";// 连接URL字符串
	private static String user = "book"; // 数据库用户名
	private static String password = "accp"; // 用户密码
	/**
	 * 获取数据库连接对象。
	 */
	public Connection getConnection() {
		Connection conn = null;// 数据连接对象
		// 获取连接并捕获异常
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();// 异常处理
		}
		return conn;// 返回连接对象
	}
	/**
	 * 关闭数据库连接。
	 * @param conn 数据库连接
	 * @param stmt Statement对象
	 * @param rs 结果集
	 */
	public void closeAll(Connection conn, PreparedStatement pstmt, 
					ResultSet rs) {
		// 若结果集对象不为空,则关闭
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 若Statement对象不为空,则关闭
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 若数据库连接对象不为空,则关闭
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception 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();
		}finally{
			this.closeAll(conn, pstmt, null);
		}
		return rowCount;
	}
}

 

package cn.jbit.dao;

import java.util.List;
import cn.jbit.entity.BookManage;
public interface BookManageDao {
	/**
	 * 查询一条数据
	 * @param id
	 * @return BookManage
	 */
	public BookManage select(int id);
	/**
	 * 查询所有数据
	 * @return List<BookManage>
	 */
	public List<BookManage> list();
	/**
	 * 添加一条数据
	 * @param item
	 * @return int
	 */
	public int Add(BookManage item);
	/**
	 * 删除一条数据
	 * @param id
	 * @return int
	 */
	public int delete(int id);
	/**
	 * 修改一条数据
	 * @param item
	 * @return int
	 */
	public int update(BookManage item);
}

 

package cn.jbit.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.jbit.dao.BaseDao;
import cn.jbit.dao.BookManageDao;
import cn.jbit.entity.BookManage;
//BookManageDaoImpl 继承 BaseDao 实现  BookManageDao
public class BookManageDaoImpl extends BaseDao implements BookManageDao {
	Connection conn = null;//数据库连接
	PreparedStatement pstmt = null;//数据库操作
	ResultSet rs = null;//保存查询结果
	//查询一条数据
	public BookManage select(int id) {
		BookManage item = null;//声明 BookManage 对象,用来保存查询的数据
		String sql = "select b_id, b_name, b_author, to_char(b_time,'yyyy-mm-dd'), b_type " +
				"from bookmanage where b_id=?";//编写预处理 SQL
		conn = this.getConnection();//使用父类的数据库连接
		try {
			pstmt =conn.prepareStatement(sql);//实例化 PreparedStatement  
			pstmt.setInt(1, id);//设置 “?”的内容,要使用 id 查询
			rs = pstmt.executeQuery();//实例化 ResultSet 对象  
			if(rs.next()){//指针向下移动  
				item = new BookManage(//把查询到的内容保存到 BookManage 对象中
						rs.getInt(1),
						rs.getString(2),
						rs.getString(3),
						rs.getString(4),
						rs.getString(5)
						);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//使用父类的关闭数据库连接,把所有都关闭
			this.closeAll(conn, pstmt, rs);
		}
		//返回保存的数据的 BookManage 对象
		return item;
	}
	//查询所有数据
	public List<BookManage> list() {
		//使用泛型集合来保存一组数据,约束类型是 BookManage 类型
		List<BookManage> list = new ArrayList<BookManage>();
		BookManage item = null;//声明 BookManage 对象,用来一行行数据
		//此处不需要设置任何内容  
		String sql ="select b_id, b_name, b_author, to_char(b_time,'yyyy-mm-dd'), b_type from bookmanage";
		conn = this.getConnection();//使用父类的数据库连接
		try {
			pstmt =conn.prepareStatement(sql);//实例化 PreparedStatement  
			rs = pstmt.executeQuery();//实例化 ResultSet 对象  
			while(rs.next()){//指针向下移动  
				item = new BookManage(//装进 BookManage 对象中
						rs.getInt(1),
						rs.getString(2),
						rs.getString(3),
						rs.getString(4),
						rs.getString(5)
						);
				list.add(item);//再添加到泛型集合
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//使用父类的关闭数据库连接,把所有都关闭
			this.closeAll(conn, pstmt, rs);
		}
		//返回泛型集合,里面装着所有所有数据
		return list;
	}
	//添加一条数据
	public int Add(BookManage item) {
		//编写预处理 SQL
		String sql = "insert into BookManage (B_ID, B_NAME, B_AUTHOR, B_TIME, B_TYPE)" +
				"values (SEQ_BOOKMANAGE.Nextval,?,?, to_date(?, 'yyyy-mm-dd'), ?)";
		return this.executeUpdate(sql, new Object[]{
				item.getName(),
				item.getAuthor(),
				item.getTime(),
				item.getType()
		});//返回SQL语句和要添加的 Object 对象
	}
	//删除一条数据
	public int delete(int id) {
		//编写预处理 SQL
		String sql = "delete bookmanage where b_id = ?";
		return this.executeUpdate(sql, new Object[]{id});
	}//返回SQL语句根据 id 删除

	//修改一条数据
	public int update(BookManage item) {
		//编写预处理 SQL
		String sql = 
				"update bookmanage" +
						"   set b_name = ?," + 
						"       b_author = ?," + 
						"       b_time =to_date(?,'yyyy-mm-dd')," + 
						"       b_type =?" + 
						" where b_id = ?";
		
		return this.executeUpdate(sql, new Object[]{
				item.getName(),
				item.getAuthor(),
				item.getTime(),
				item.getType(),
				item.getId()
		});//返回SQL语句和要修改的 Object 对象,根据 id 来修改
	}
}