• 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;
    	}
    
    }
    

  • 相关阅读:
    CentOS7 安装 JIRA 7.2.x 教程:下载、安装、汉化、破解
    安装 GraphicsMagick
    CentOS 7 yum 安装 Nginx
    CentOS 安装 OpenResty
    软件工程技术面试个人指南
    五线谱
    中央C-高低音谱号里的中央C和其它音节
    使用管道copy同一文件至多个目录下
    refusing to merge unrelated histories
    Viewing A Specific Commit_12
  • 原文地址:https://www.cnblogs.com/a1111/p/6540349.html
Copyright © 2020-2023  润新知