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

  • 相关阅读:
    Mac普通用户修改了/etc/sudoers文件的解决办法
    python对缓存(memcached,redis)的操作
    线程、进程、协程和队列
    python作用域和多继承
    sokect编程进阶
    socket编程基础
    python面相对象进阶
    python异常处理
    configparser模块
    subprocess模块
  • 原文地址:https://www.cnblogs.com/a1111/p/6540349.html
Copyright © 2020-2023  润新知