如何使用JDBC實現數據訪問對象層(DAO)

JAVA是面向對象的語言,開發者在操做數據的時候,一般更習慣面對一個特定類型的對象,如一個用戶就是一個
User類的對象。DAO層須要作的,就是爲上層提供充分的對象支持,讓上層再也看不到具體的數據,而是一個個活生生的對象。java

增長,刪除,查詢和修改操做是DAO須要作的最基本的4項操做。查詢通常須要提供遍歷查詢和id查詢,對於遍歷查詢,DAO須要提供
User泛型的list對象,對於id查詢則提供已經裝配好數據的User對象,至於增長和修改操做,上層通常會提供一個User對象,DAO把
User對象中的數據使用Insert語句插入到表格中。刪除操做則只需提供一個id便可mysql

 class User{
	private long id;
	private String name;
	private String gender;
  public User(){
	super();
  }
  public User(long id,String name,String gender){
	super();
	this.id = id;
	this.name = name;
	this.gender = gender;
	}
	//get,set方法
  }
  
  //DAO類
  public class jdbcDao{
	static{
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}catch(Exception e){
		e.printStackTrace();
			}
		}
	private Connection  getConn(){
		try{
			return DriverManager.getConnection("jdbc:mysql://localhost:3306:xe","root","password");

			}catch(Exception e){
			e.printStackTrace();
			}
		}
		return null;
	}
	private void release(ResultSet rs,Statement ps,Connection conn){
		if(rs!=null){
			try{
				rs.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(ps!=null){
			try{
				ps.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try{
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	//用ID獲取用戶對象
	public User getUserById(long id){
		ResultSet rs = null;
		PreparedStatement ps = null;
		Connection conn = null;
		String sql = "select * from user where id = ?";
		try{
			conn = this.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setLong(1,id);
			rs = ps.executeQuery();
			if(rs.next()){
				//若是存在,則直接構建並返回用戶對象
				User user = new User(rs.getLong("id"),rs.getString("name"),rs.getString("gender"));
				return user;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.release(rs,ps,conn);
		}
		return null;
	}
	//查詢全部用戶
	public List<User> getAllUsers(){
		List<User> list = new ArrayList<User>();
		ResultSet rs = null;
		PreparedStatement ps = null;
		Connection conn = null;
		String sql = "select * from user ";
		try{
			conn = this.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			//循環添加用戶對象
			while(rs.next()){
				User user = new User(rs.getLong("id"),rs.getString("name"),rs.getString("gender"));
				list.add(user);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.release(rs,ps,conn);
		}
		return list;
	}
	//修改用戶數據
	public User updateUser(User user){
		PreparedStatement ps = null;
		Connection conn = null;
		String sql = "update user set id =?,name=?,gender=?";
	try{
			conn = this.getConnection();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setLong(1,user.getId());
			ps.setString(2,user.getName());
			ps.setString(3,user.getGender());
			int rst = ps.executeUpdate();
			if(rst>0){
				return  new User(user.getId(),user.getName(),user.getGender());
				 
			}
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try{
				conn.rollback();
			}catch(Exception e1){
				e1.printStackTrace();
			}
		}finally{
			this.release(null,ps,conn);
		}
		return null;
			}
		}

		//刪除用戶數據
	public boolean deleteUser(long id){
		PreparedStatement ps = null;
		Connection conn = null;
		String sql = "delete from user where id =?;
	try{
			conn = this.getConnection();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setLong(1,user.getId());
			ps.setString(2,user.getName());
			ps.setString(3,user.getGender());
			int rst = ps.executeUpdate();
			if(rst>0){
				return user;
			}
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try{
				conn.rollback();
			}catch(Exception e1){
				e1.printStackTrace();
			}
		}finally{
			this.release(null,ps,conn);
		}
		return null;
			}
		}

		//插入用戶數據
		public User insertUser(User user){
		PreparedStatement ps = null;
		Connection conn = null;
		String sql = "insert into user values(?,?,?)";
	try{
			conn = this.getConnection();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setLong(1,user.getId());
			ps.setString(2,user.getName());
			ps.setString(3,user.getGender());
			int rst = ps.executeUpdate();
			if(rst>0){
				return user;
			}
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try{
				conn.rollback();
			}catch(Exception e1){
				e1.printStackTrace();
			}
		}finally{
			this.release(null,ps,conn);
		}
		return null;
			}
		}

	}
  }
相關文章
相關標籤/搜索