首页>代码>java开发微博项目源码>/项目源码与数据库/MicroBlog/src/org/microblog/dao/CommentDao.java
package org.microblog.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.microblog.po.Comment;
import org.microblog.po.User;
import org.microblog.util.DBConn;

public class CommentDao {
	
public int addComment(final Comment comment){
		
		try {
			int u_id = comment.getU_id();
			int m_id = comment.getM_id();
			int u_id2 = comment.getU_id2();
			String c_content = comment.getC_content();
			String c_comtime = comment.getC_comtime();
			String strSQL ="insert into comment (c_id,u_id,u_id2,m_id,c_content,c_comtime) values(null,?,?,?,?,?)";
		    Object[] params = new Object[]{u_id,u_id2,m_id,c_content,c_comtime};
		//调用DBConn,传送到数据库
		    DBConn dbConn= new DBConn();
		
		    int AffectedRows = dbConn.execOther(strSQL, params);
		    dbConn.closeConn();
		    return AffectedRows;
			
		} catch (Exception e) {
			return 0;
		}
	}
	
public List<Comment> getCommentListByBlogId(final int m_id){
	List<Comment> commentList = new ArrayList<Comment>();
	 String strSQL="select * from comment where m_id =?";
	//调用DBConn,传送到数据库
	 DBConn dbConn= new DBConn();
	 ResultSet rs=dbConn.execQuery(strSQL, new Object[]{m_id});
	try {
			while(rs.next()) {
				Comment comm = new Comment();
				comm.setU_id2(rs.getInt("u_id2"));//
				comm.setC_content(rs.getString("c_content"));
				comm.setU_id(rs.getInt("u_id"));
				comm.setM_id(rs.getInt("m_id"));
				comm.setC_comtime(rs.getString("c_comtime"));
				comm.setC_id(rs.getInt("c_id"));
				commentList.add(comm);
			}
		
			
			return commentList;
		 
	} catch (Exception e) {
		return null;
	}finally{
		try {
			//关闭数据库	
			rs.close();
			dbConn.closeConn();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
	
	
	public User getCommentUserByCommentId(final int u_id){
		try {
			String strSQL="select * from users where u_id="+u_id;
			//调用DBConn,传送到数据库
			 DBConn dbConn= new DBConn();
			 ResultSet rs=dbConn.execQuery(strSQL, new Object[]{});
			 User user = new User();
			 if(rs.next()) {
					
					user.setU_avatar(rs.getString("u_avatar"));
					user.setU_nickname(rs.getString("u_nickname"));
					user.setU_email(null);
					user.setU_birthday(null);
					user.setU_gender(null);
					user.setU_info(null);
					user.setU_locate(null);
					user.setU_msn(null);
					user.setU_password(null);
					user.setU_qq(null);
					user.setU_realname(null);
					user.setU_regdate(null);
					user.setU_remark(null);
				}
			 dbConn.closeConn();
			 return user;
			
		} catch (Exception e) {
			return null;
		}
		
	}


	
	public long getCommentAmountByBlogId(final int m_id ){
		String sql_getCommentAmountByBlogId="SELECT count(*) FROM `microblog`.`comment` where m_id = ?";
		DBConn dbConn = new DBConn();
		ResultSet rs = dbConn.execQuery(sql_getCommentAmountByBlogId, new Object[]{m_id});
		
		long amount = 0;
	
		try {
			while(rs.next()){
				amount=rs.getLong("count(*)");
				
			}
			
			return amount;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
			dbConn.closeConn();
		}
		return amount;
	}

}
最近下载更多
guzhoumingyue  LV1 2023年11月15日
543539666  LV7 2023年6月19日
彭星旭  LV1 2023年6月12日
qingyu1  LV1 2023年2月11日
Yingxuan  LV1 2023年1月16日
森屿elf  LV2 2022年5月29日
微信网友_5952843817488384  LV2 2022年5月10日
葡萄树下酒庄  LV9 2022年2月16日
酸欠hhhh  LV1 2021年12月22日
lk030111  LV1 2021年12月16日
最近浏览更多
pangzhihui  LV12 2月28日
ccccccc1  LV1 2023年12月26日
k666k6  LV1 2023年11月24日
guzhoumingyue  LV1 2023年11月15日
weiyoung 2023年8月6日
暂无贡献等级
三十一  LV2 2023年6月28日
罗清晨  LV11 2023年6月20日
彭星旭  LV1 2023年6月12日
543539666  LV7 2023年5月30日
dmyafda  LV5 2023年3月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友