dao增删查改

DAO编程标准步骤
重点:1、创建实体类的时候需要注意和数据库中数据对应 包括数据类型
2、接口的设定。先构思好需要实现哪些功能,实现功能的时候需要注意的是返回值类型,以及参数的个数和类型
1、建表

create table emp(
id number(10) primary key,
name varchar2(30),
salary number(10,2),
age number(3) 
)	

2、创建实体类

public class Emp {
private int id;
private String name;
private double salary;
private int age;
}

3、定义DAO接口

public interface EmpDAO {
//插入员工信息
public void insertEmp(Emp emp);
//删除员工
public void deleteEmp(int id);
//修改员工信息
public void updateEmp(Emp emp);
//查询所有员工
public List<Emp> selectAllEmps();
//模糊查询
public Emp fuzzySelect(int id);
//模糊查询
List<Emp> selectByKey(String name);
//分页查询
List<Emp> selectByPage(int pageIndex);

}
4、实现DAO接口(JDBC提供实现)

public class EmpDAOImpl implements EmpDAO{

@Override
public void insertEmp(Emp emp) {
	Connection conn = null;
	PreparedStatement pstm = null;
	try{
		conn = JDBCUtil.getConnection();
		String sql="insert into emp values(?,?,?,?)";
		pstm = conn.prepareStatement(sql);
		pstm.setInt(1, emp.getId());
		pstm.setString(2,emp.getName());
		pstm.setDouble(3, emp.getSalary());
		pstm.setInt(4, emp.getAge());
		pstm.executeUpdate();
	}catch(Exception e){
		e.printStackTrace();
		throw new RuntimeException("注册员工出现异常");
	}finally{
		JDBCUtil.close(null, pstm);
	}
}

@Override
public void deleteEmp(int id) {
	Connection conn = null;
	PreparedStatement pstm = null;
	try{
		conn = JDBCUtil.getConnection();
		String sql="delete from emp where id=?";
		pstm = conn.prepareStatement(sql);
		pstm.setInt(1, id);
		pstm.executeUpdate();
	}catch(Exception e){
		e.printStackTrace();
		throw new RuntimeException("删除员工DAO出现异常");
	}finally{
		JDBCUtil.close(null, pstm);
	}
}

@Override
public void updateEmp(Emp emp) {
	Connection conn = null;
	PreparedStatement pstm = null;
	try{
		conn = JDBCUtil.getConnection();
		String sql="update emp set name=?,salary=?,age=?  where id=?";
		pstm = conn.prepareStatement(sql);
		pstm.setString(1, emp.getName());
		pstm.setDouble(2, emp.getSalary());
		pstm.setInt(3, emp.getAge());
		pstm.setInt(4, emp.getId());
		pstm.executeUpdate();
	}catch(Exception e){
		e.printStackTrace();
		throw new RuntimeException("修改DAO出现异常");
	}finally{
		JDBCUtil.close(null, pstm);
	}
}

@Override
public List<Emp> selectAllEmps() {
	Connection conn = null;
	PreparedStatement pstm = null;
	ResultSet rs = null;
	List<Emp> emps = new ArrayList<Emp>();
	try{
		conn = JDBCUtil.getConnection();
		String sql="select * from emp";
		pstm = conn.prepareStatement(sql);
		rs = pstm.executeQuery();
		while(rs.next()){
			Emp emp = new Emp();
			emp.setId(rs.getInt(1));
			emp.setName(rs.getString(2));
			emp.setSalary(rs.getDouble(3));
			emp.setAge(rs.getInt(4));
			emps.add(emp);
		}
		return emps;
	}catch(Exception e){
		e.printStackTrace();
		throw new RuntimeException("查询所有DAO出现异常");
	}finally{
		JDBCUtil.close(conn, pstm, rs);
	}
}
public Emp fuzzySelect(int id){
		Connection conn=null;
		PreparedStatement psmt=null;
		ResultSet rs=null;
		try{
			conn = JDBCUtil.getConnection();
			String sql="select *  from stu where id=?";
			psmt=conn.prepareStatement(sql);
			psmt.setInt(1, id);
			rs=psmt.executeQuery();
			 
			 Emp e = new Emp();
			while(rs.next()){
				 e.setId(rs.getInt(1));
				 e.setName(rs.getString(2));
				 e.setSalary(rs.getDouble(3));
				 e.setAge(rs.getInt(4));
				
			}
			return e;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			JDBCUtil.close(conn, psmt,rs);
		}
	}

@Override
public List<Emp> selectByKey(String name) {
	Connection conn=null;
	PreparedStatement psmt=null;
	ResultSet rs=null;
	try{
		conn=JDBCUtil.getConnection();
		String sql="select * from emp where name like ?";
		psmt = conn.prepareStatement(sql);
		psmt.setString(1,"%"+name+"%" );
		rs = psmt.executeQuery();
		List<Emp> list = new ArrayList<Emp>();
		while(rs.next()){
			Emp employees = new Emp();
			employees.setId(rs.getInt(1));
			employees.setName(rs.getString(2));
			employees.setSalary(rs.getDouble(3));
			employees.setAge(rs.getInt(4));
			list.add(employees);
		}
		return list;
	}catch(Exception e){
		e.printStackTrace();
		return null;
	}
}

@Override
public List<Emp> selectByPage(int pageIndex) {
	Connection conn=null;
	PreparedStatement pamt=null;
	ResultSet rs=null;
	try{
		conn=JDBCUtil.getConnection();
		String sql="select * from(select t.*,rownum as rn from telbook t) where rn >= ? and  rn<= ? ";
	    pamt=conn.prepareStatement(sql);
	    pamt.setInt(1,(pageIndex-1)*3+1);
	    pamt.setInt(2,pageIndex*3);
	    rs=pamt.executeQuery();
	    List<Emp> list = new ArrayList<Emp>();
	    while(rs.next()){
	    	Emp employees = new Emp();
	    	employees.setId(rs.getInt(1));
	    	employees.setName(rs.getString(2));
	    	employees.setSalary(rs.getDouble(3));
	    	employees.setAge(rs.getInt(4));
	    	list.add(employees);
	    }
	    return list;
	}catch(Exception e){
	 e.printStackTrace();
	 throw new RuntimeException("查询异常");
	}finally{
		JDBCUtil.close(conn, pamt, rs);
	}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值