java面向对象高级分层实例_数据库操作类

package bdqn.studentSys.Dao.impl;
/***
 * 学生表的数据库操作类
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdqn.studentSys.Dao.BaseDao;
import bdqn.studentSys.Dao.StudentDao;
import bdqn.studentSys.entity.Student;

public class StudentDaoImpl extends BaseDao implements StudentDao {
	
	//查询全部
	public List<Student> getAllStudent() {
		// TODO Auto-generated method stub
		List<Student> studentlist=new ArrayList<Student>();
		String sql="select * from Student";
		try {
			ResultSet rs=executeQurey(sql, null);
			while(rs.next()){
				Student stu=new Student();
				stu.setName(rs.getString(1));
				stu.setPwd(rs.getString(2));
				stu.setAge(rs.getInt(3));
				stu.setSex(rs.getString(4));
				studentlist.add(stu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll();
		}
		return studentlist;
	}

	//修改
	public int UpdateStudent(Student stu) {
		// TODO Auto-generated method stub
		int rel=0;
		String sql="update Student set name=?,pwd=?,age=?,sex=? where stuno=?";
		Object[]prams={stu.getName(),stu.getPwd(),stu.getAge(),stu.getSex(),stu.getStuno()};
		try {
			rel=executeUpdate(sql, prams);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll();
		}
		return rel;
	}

	//添加
	public int addStudent(Student stu) {
		int rel=0;
		String sql="insert Student (name,pwd,age,sex) values(?,?,?,?)";
		Object []prams={stu.getName(),stu.getPwd(),stu.getAge(),stu.getSex()};
		try {
			rel=executeUpdate(sql, prams);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll();
		}
		
		return rel;
	}

	//删除
	public int delStudent(int stuno) {
		int rel=0;
		String sql="delete from Student where studentno=?";
		Object[]prams={stuno};
		try {
			rel=executeUpdate(sql, prams);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll();
		}
		return rel;
	}

}

原文地址:https://www.cnblogs.com/a1111/p/12816598.html