⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 replycommentsdao.java

📁 模拟的土豆网视频网站
💻 JAVA
字号:
package cn.myvideosite.data.model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import cn.myvideosite.data.model.bean.ReplyComments;

public class ReplyCommentsDAO extends BaseDAO{
	/**
	 * 回复评论表 插入方法
	 * @param replycomments
	 * @return
	 */
      public static ReplyComments save(ReplyComments replycomments){
    	  PreparedStatement pstmt=null;
    	  String sql="insert into replycomments (userId,replyTime,replyText) values (?,?,?)";
    	  try {
			pstmt=connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
					
			pstmt.setInt(1, replycomments.getUserId());
			pstmt.setTimestamp(2,new Timestamp(replycomments.getReplyTime().getTime()));
			pstmt.setString(3, replycomments.getReplyText());
			
			if(pstmt.executeUpdate()==1){
				ResultSet rs=pstmt.getGeneratedKeys();
				if(rs!=null){
					rs.next();
					replycomments.setReplyId(rs.getInt(1));
				}
			}
		} catch (SQLException e) {
		
			e.printStackTrace();
		}finally{
			if(pstmt!=null){
				try {
					pstmt.close();
					pstmt=null;
				} catch (SQLException e) {
					
					e.printStackTrace();
				}
			
			}
		}   	   
		return replycomments;   	  
      }
      /**
       * 回复评论表 删除方法
       * @param id
       */
      public static void delete(int replyId){
    	  PreparedStatement pstmt=null;
    	  String sql="delete from replycomments where replyId=?";
    	  try {
			pstmt=connection.prepareStatement(sql);
			
			pstmt.setInt(1, replyId);
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally{
			try {
				if(pstmt!=null){
					pstmt.close();
					pstmt=null;
				}			
			} catch (SQLException e) {				
				e.printStackTrace();
			}
		}  
      }
      /**
       * 回复评论表 更新方法
       * @param replycomments
       */
      public static void update(ReplyComments replycomments){
    	  PreparedStatement pstmt=null;
    	  String sql="update replycomments set userId=?,replyTime=?,replyText=? where replyId=?";
    	  try {
			pstmt=connection.prepareStatement(sql);
			
			
			pstmt.setInt(1,replycomments.getUserId());
			pstmt.setTimestamp(2,new Timestamp(replycomments.getReplyTime().getTime()));
			pstmt.setString(3, replycomments.getReplyText());
			pstmt.setInt(4,replycomments.getReplyId());
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
		
			e.printStackTrace();
		}finally{
			try {
				if(pstmt!=null){
				pstmt.close();
				pstmt=null;
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}		  
      }
      /**
       * 回复评论表 查找方法 单条记录
       * @param id
       * @return
       */
      public static ReplyComments findById(int replyId){
    	  PreparedStatement pstmt=null;
    	  ResultSet rs=null;
    	  ReplyComments replycomments=null;
    	  String sql="select * from replycomments where replyId=?";
    	  try {
			pstmt=connection.prepareStatement(sql);
			pstmt.setInt(1,replyId);
			rs=pstmt.executeQuery();
			replycomments=new ReplyComments();
			while(rs.next()){
				
				replycomments.setReplyId(rs.getInt(1));				
				replycomments.setUserId(rs.getInt(2));
				replycomments.setReplyTime(rs.getTimestamp(3));
				replycomments.setReplyText(rs.getString(4));
				
			}
			
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		} finally{
			try {
				if(rs!=null){
				rs.close();
				rs=null;
				}
				if(pstmt!=null){
				pstmt.close();
				pstmt=null;
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		return replycomments;
    	  
      }
      /**
       * 回复评论表 查询全部 无参数方法
       * @return
       */
      public static List<ReplyComments> findAll(){
    	  PreparedStatement pstmt=null;
    	  ResultSet rs=null;
    	  List<ReplyComments> rtnList =null;
    	  String sql="select * from replycomments";
    	  try {
			pstmt=connection.prepareStatement(sql);
			rs=pstmt.executeQuery();
			rtnList =new ArrayList<ReplyComments>();
			while(rs.next()){
				ReplyComments replycomments=new ReplyComments();
				
				replycomments.setReplyId(rs.getInt(1));				
				replycomments.setUserId(rs.getInt(2));
				replycomments.setReplyTime(rs.getTimestamp(3));
				replycomments.setReplyText(rs.getString(4));
				
				rtnList.add(replycomments);
			}
			
		} catch (SQLException e) {
		
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
				rs.close();
				rs=null;
				}
				if(pstmt!=null){
				pstmt.close();
				pstmt=null;
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}    	   
		return rtnList;
    	  
      }
      /**
       * 回复评论表 查询全部 有参数方法
       * @param first
       * @param max
       * @return
       */
      public static List<ReplyComments> findAll(int first,int max){
    	  PreparedStatement pstmt=null;
    	  ResultSet rs=null;
    	  List<ReplyComments> rtnList =null;
    	  String sql="select * from replycomments limit ?,?";
    	  try {
			pstmt=connection.prepareStatement(sql);
			pstmt.setInt(1, first);
			pstmt.setInt(2,max);
			rs=pstmt.executeQuery();
			rtnList =new ArrayList<ReplyComments>();
			while(rs.next()){
				ReplyComments replycomments=new ReplyComments();
				
				replycomments.setReplyId(rs.getInt(1));				
				replycomments.setUserId(rs.getInt(2));
				replycomments.setReplyTime(rs.getTimestamp(3));
				replycomments.setReplyText(rs.getString(4));
				
				rtnList.add(replycomments);
			}
			
		} catch (SQLException e) {
		
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
				rs.close();
				rs=null;
				}
				if(pstmt!=null){
				pstmt.close();
				pstmt=null;
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}    	   
		return rtnList;
      	  
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -