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