📄 videocommentdao.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.VideoComment;
public class VideoCommentDAO extends BaseDAO{
/**
* 视频评论表 插入方法
* @param videocomment
* @return
*/
public static VideoComment save(VideoComment videocomment){
PreparedStatement pstmt=null;
String sql="insert into videocomment (userId,commentText,getScore,publishTime) values (?,?,?,?)";
try {
pstmt=connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, videocomment.getUserId());
pstmt.setString(2, videocomment.getCommentText());
pstmt.setInt(3, videocomment.getGetScore());
pstmt.setTimestamp(4,new Timestamp(videocomment.getPublishTime().getTime()));
if(pstmt.executeUpdate()==1){
ResultSet rs=pstmt.getGeneratedKeys();
if(rs!=null){
rs.next();
videocomment.setRemarkId(rs.getInt(1));
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
pstmt=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return videocomment;
}
/**
* 视频评论表 删除方法
* @param id
*/
public static void delete(int remarkId){
PreparedStatement pstmt=null;
String sql="delete from videocomment where remarkId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1, remarkId);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 视频评论表 更新方法
* @param videocomment
*/
public static void update(VideoComment videocomment){
PreparedStatement pstmt=null;
String sql="update videocomment set userId=?,commentText=?,getScore=?,publishTime=? where remarkId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1, videocomment.getUserId());
pstmt.setString(2, videocomment.getCommentText());
pstmt.setInt(3, videocomment.getGetScore());
pstmt.setTimestamp(4,new Timestamp(videocomment.getPublishTime().getTime()));
pstmt.setInt(5, videocomment.getRemarkId());
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 VideoComment findById(int remarkId){
PreparedStatement pstmt=null;
ResultSet rs=null;
VideoComment videocomment=null;
String sql="select * from videocomment where remarkId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1, remarkId);
rs=pstmt.executeQuery();
videocomment=new VideoComment();
while(rs.next()){
videocomment.setRemarkId(rs.getInt(1));
videocomment.setUserId(rs.getInt(2));
videocomment.setCommentText(rs.getString(3));
videocomment.setGetScore(rs.getInt(4));
videocomment.setPublishTime(rs.getTimestamp(5));
}
} catch (SQLException e) {
e.printStackTrace();
}
return videocomment;
}
/**
* 视频评论表 查询全部 无参数方法
* @return
*/
public static List<VideoComment> findAll(){
PreparedStatement pstmt=null;
ResultSet rs=null;
List<VideoComment> rtnList =null;
String sql="select *from videocomment";
try {
pstmt=connection.prepareStatement(sql);
rs=pstmt.executeQuery();
rtnList =new ArrayList<VideoComment>();
while(rs.next()){
VideoComment videocomment=new VideoComment();
videocomment.setRemarkId(rs.getInt(1));
videocomment.setUserId(rs.getInt(2));
videocomment.setCommentText(rs.getString(3));
videocomment.setGetScore(rs.getInt(4));
videocomment.setPublishTime(rs.getTimestamp(5));
rtnList.add(videocomment);
}
} 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<VideoComment> findAll(int first,int max){
PreparedStatement pstmt=null;
ResultSet rs=null;
List<VideoComment> rtnList =null;
String sql="select *from videocomment limit ?,?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1, first);
pstmt.setInt(2,max);
rs=pstmt.executeQuery();
rtnList =new ArrayList<VideoComment>();
while(rs.next()){
VideoComment videocomment=new VideoComment();
videocomment.setRemarkId(rs.getInt(1));
videocomment.setUserId(rs.getInt(2));
videocomment.setCommentText(rs.getString(3));
videocomment.setGetScore(rs.getInt(4));
videocomment.setPublishTime(rs.getTimestamp(5));
rtnList.add(videocomment);
}
} 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 + -