package org.QQroom.dao.impl; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.QQroom.dao.DiaryDao; import org.QQroom.entity.Diary; import org.QQroom.entity.PageBean; import org.QQroom.entity.UserLogin; import org.QQroom.utils.BaseDao; /** * 日志实现类 * @author lenovo * */ public class DiaryDaoImpl extends BaseDao implements DiaryDao { /** *(当前用户)得到所有的日志 */ @Override public List<Diary> getAllDiary(int userId) {//wirteTime, String sql = "select id,wirteTime,title,content, userID from diary where userID=? order by id desc"; List<Diary> list = new ArrayList<Diary>(); ResultSet rs = super.getResultSet(sql, userId); if(rs!=null){ try { while(rs.next()){ Diary diary = new Diary(); diary.setId(rs.getInt("id")); diary.setWriteTime(rs.getDate("wirteTime")); diary.setWriteTime(rs.getTimestamp("wirteTime")); diary.setTitle(rs.getString("title")); diary.setContent(rs.getString("content")); diary.setUserId(rs.getInt("userID")); list.add(diary); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(rs!=null){ try { super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return list; } /** *(当前用户)得到所有的日志 */ /*public List<Diary> getAllDiary(PageBean pageBean,int userId) {//wirteTime, String sql = "select top (?) * from diary where id not in(select top (?) id from diary ) and userID=? order by id desc"; List<Diary> list = new ArrayList<Diary>(); Object params[] = {pageBean.getPageSize(),(pageBean.getPageNum()-1)*pageBean.getPageSize(),userId}; ResultSet rs = super.getResultSet(sql, params); if(rs!=null){ try { while(rs.next()){ Diary diary = new Diary(); diary.setId(rs.getInt("id")); diary.setWriteTime(rs.getDate("wirteTime")); diary.setWriteTime(rs.getTimestamp("wirteTime")); diary.setTitle(rs.getString("title")); diary.setContent(rs.getString("content")); diary.setUserId(rs.getInt("userID")); list.add(diary); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(rs!=null){ try { super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return list; }*/ /** *添加日志 */ @Override public int addDiary(Diary diary) { String sql = "insert into diary(wirteTime, title, content, userID) values(default,?,?,?)"; Object[] params = {diary.getTitle(),diary.getContent(),diary.getUserId()}; return super.updateDate(sql, params); } /** * 修改日志(通过当前登录的账号和日志编号) * @return */ public int updateDiary(Diary diary){ String sql = "update diary set wirteTime=getdate(),title=?,content=? where id=? and userID=?"; Object[] params = {diary.getTitle(),diary.getContent(),diary.getId(),diary.getUserId()}; return super.updateDate(sql, params); } /** *删除日志 */ @Override public int deleteDiary(int id,int userId) { String sql = "delete diary where id = ? and userID = ?"; Object[] params = {id,userId}; return super.updateDate(sql, params); } /** * 获取日志对象 (根据日志编号 和 用户的acc ) */ @Override public Diary getOntDiay(Diary diary) { String sql = "select id,wirteTime,title,content, userID from diary where userID=? and id=? "; Diary diaryOne = null; Object[] params = {diary.getUserId(),diary.getId()}; ResultSet rs = super.getResultSet(sql, params); if(rs!=null){ try { while(rs.next()){ diaryOne = new Diary(); diaryOne.setId(rs.getInt("id")); /*diary.setWriteTime((rs.getDate("wirteTime")).toString());*/ /*diaryOne.setWriteTime(new Date(rs.getTime("wirteTime").getTime()));*/ diaryOne.setWriteTime(rs.getDate("wirteTime")); /*diaryOne.setWriteTime(rs.getTimestamp("wirteTime"));*/ diaryOne.setTitle(rs.getString("title")); diaryOne.setContent(rs.getString("content")); diaryOne.setUserId(rs.getInt("userID")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(rs!=null){ try { super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return diaryOne; } /** * 得到当前用户日志的总数 */ @Override public int getDiarySum(UserLogin user) { String sql = "select count(0) as c from diary where userID=?"; ResultSet rs = super.getResultSet(sql, user.getAcc()); int result = 0; if(rs!=null){ try { while(rs.next()){ result=rs.getInt("c"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(rs!=null){ try { super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return result; } }
最近下载更多
阿萨大大123213 LV2
2月24日
liu2022 LV14
2022年7月31日
最代码安逸 LV15
2022年3月6日
谢小饭_ LV8
2022年1月21日
nsz123456 LV11
2021年12月5日
17614867633 LV3
2021年6月13日
Orianna LV2
2021年5月8日
王东东 LV17
2021年4月16日
荆童123456 LV2
2021年3月22日
newscc LV8
2021年2月7日
最近浏览更多
Ashbell
2月25日
暂无贡献等级
阿萨大大123213 LV2
2月24日
微信网友_6878791569330176
2月23日
暂无贡献等级
℡ LV8
2023年10月23日
cksndh LV4
2023年8月16日
微信网友_6444139264921600 LV6
2023年4月29日
臧家旺 LV3
2023年4月27日
zl2569837365 LV1
2023年3月14日
WanLiuYun LV12
2023年3月12日
2410068425 LV23
2023年3月3日