package org.microblog.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.microblog.po.Notify; import org.microblog.util.DBConn; public class NotifyDao { public boolean add(final Notify notify){ String sql_addNotify = "insert into notify (n_id,u_id,u_id2,m_id,n_type,n_isread,n_time) values(null,?,?,?,?,?,?)"; DBConn dbConn = new DBConn(); int u_id = notify.getU_id(); int u_id2 = notify.getU_id2(); int m_id = notify.getM_id(); int n_type = notify.getN_type(); boolean n_isread = false; String n_time = notify.getN_time(); int affectedRows = dbConn.execOther(sql_addNotify, new Object[]{u_id,u_id2,m_id,n_type,n_isread,n_time}); return affectedRows>0?true:false; } public boolean read(final int n_id){ String sql_readNotify = "update notify set n_isread = 1 where n_id = ?"; DBConn dbConn = new DBConn(); int affectedRows = dbConn.execOther(sql_readNotify, new Object[]{n_id}); return affectedRows>0?true:false; } public boolean remove(final int n_id){ String sql_removeNotify = "delete from notify where n_id = ?"; DBConn dbConn = new DBConn(); int affectedRows = dbConn.execOther(sql_removeNotify, new Object[]{n_id}); return affectedRows>0?true:false; } public List<Notify> getNotifyByUserId(int u_id){ List<Notify> notifyList = new ArrayList<Notify>(); String sql_getNotifyByUserId = "select * from notify where u_id = ?"; DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(sql_getNotifyByUserId, new Object[]{u_id}); try { while(rs.next()){ Notify notify = new Notify(); notify.setN_id(rs.getInt("n_id")); notify.setU_id(rs.getInt("u_id")); notify.setU_id2(rs.getInt("u_id2")); notify.setM_id(rs.getInt("m_id")); notify.setN_type(rs.getInt("n_type")); notify.setN_isread(rs.getBoolean("n_isread")); notify.setN_time(rs.getString("n_time")); notifyList.add(notify); } return notifyList; } catch (SQLException e) { e.printStackTrace(); }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } dbConn.closeConn(); } return notifyList; } public long getNotifyAmountByUserId(final int u_id){ long notifyAmount = 0; String sql_getNotifyByUserId = "select count(*) from notify where u_id = ? and n_isread = 0"; DBConn dbConn = new DBConn(); ResultSet rs = dbConn.execQuery(sql_getNotifyByUserId, new Object[]{u_id}); try { while(rs.next()){ notifyAmount = rs.getLong("count(*)"); } return notifyAmount; } catch (SQLException e) { e.printStackTrace(); }finally{ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } dbConn.closeConn(); } return notifyAmount; } }