사용자 및 게시물 바인딩

2440 단어
자세히 보기
게시물의 엔티티 클래스에서
//  private int uid;
private User user;
public void setUser(User user) {
	this.user = user;
}
public User getUser() {
	return user;
}

Dao층에 있는 방법.
@Override
	public List getAllForum() {
		List list = new ArrayList();
		Connection conn = DBUtil.getConn();
		UserDao userDao = new UserDaoImpl();
		String sql = "select * from forum as p order by p.time desc ";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				Forum forum = new Forum();
				forum.setId(rs.getInt("id"));
				forum.setChoose(rs.getString("choose"));
				forum.setTitle(rs.getString("title"));
				forum.setTime(rs.getDate("time"));
				forum.setContent(rs.getString("content"));
				forum.setPic(rs.getString("pic"));
//				forum.setUid(rs.getInt("uid"));
				forum.setView(rs.getInt("view"));
				forum.setUser(userDao.selectUserById(rs.getInt("uid")));
				list.add(forum);
			  }
			}catch(SQLException e){
				e.printStackTrace();
			}
		return list;
		}

 
@Override
	public boolean addForum(Forum forum) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Connection conn = DBUtil.getConn();
		String sql = "INSERT INTO forum(choose,title,time,content,pic,uid) VALUES (?,?,?,?,?,?)";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, forum.getChoose());
			ps.setString(2, forum.getTitle());
			ps.setString(3, df.format(new Date()));
			ps.setString(4, forum.getContent());
			ps.setString(5, forum.getPic());
			ps.setInt(6, forum.getUser().getId());
			int count = ps.executeUpdate();
			ps.close();
			return count>0?true:false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

게시물의 jsp 페이지에서
 
${forum.user.username}

좋은 웹페이지 즐겨찾기