www.pudn.com > restrant.zip > FeelbackMsgDao.java, change:2014-03-06,size:3051b


package com.alex.restrantol.dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 
 
import com.alex.restrantol.base.BaseDao; 
import com.alex.restrantol.entity.FeelbackMsgEntity; 
import com.alex.restrantol.entity.PageEntity; 
 
 
public class FeelbackMsgDao { 
 
	Connection conn = null; 
	PreparedStatement ps = null; 
	ResultSet rs = null; 
	 
	public PageEntity getPageEntity(int currentPage) { 
		PageEntity pageEntity = new PageEntity(); 
		pageEntity.setCurrentPage(currentPage); 
		pageEntity.setTotalCount(this.getTotalCount()); 
		pageEntity.setCountAllPage(this.getTotalPage(pageEntity)); 
		pageEntity.setList(this.getFeelbackMsgEntity(currentPage,pageEntity)); 
		return pageEntity; 
	} 
	 
	private List<FeelbackMsgEntity> getFeelbackMsgEntity(int currentPage,PageEntity pageEntity) { 
		 List<FeelbackMsgEntity>  list = new ArrayList<FeelbackMsgEntity>(); 
		 int startRow = (currentPage-1) * pageEntity.getCountPerPage(); 
		 BaseDao dao = new BaseDao(); 
		 FeelbackMsgEntity msg = null; 
		 try{ 
			 conn = dao.getConn(); 
			 ps = conn.prepareStatement("select * from feedBack order by msgId desc limit ?,?"); 
			 ps.setInt(1, startRow); 
			 ps.setInt(2, pageEntity.getCountPerPage()); 
			 rs = ps.executeQuery(); 
			 while(rs.next()){ 
				 msg = new FeelbackMsgEntity(); 
				 msg.setMsgId(rs.getInt("msgId")); 
				 msg.setUserId(rs.getInt("userId")); 
				 msg.setUserName(rs.getString("userName")); 
				 msg.setSendNote(rs.getString("sendNote")); 
				 msg.setSendTime(rs.getString("sendTime")); 
				 list.add(msg); 
			 } 
		 }catch(Exception e){ 
			 e.printStackTrace(); 
		 }finally{ 
			 dao.closeAll(conn, ps, rs); 
		 } 
		 return list; 
	} 
 
	public int getTotalCount(){ 
		int totalCount = 0; 
		BaseDao dao = new BaseDao(); 
		try{ 
			conn = dao.getConn(); 
			ps = conn.prepareStatement("select count(*) from feedBack"); 
			rs = ps.executeQuery(); 
			if(rs.next()){  
				totalCount = rs.getInt(1); 
			}  
		}catch(Exception e){ 
			e.printStackTrace(); 
		}finally{ 
			dao.closeAll(conn, ps, rs); 
		} 
		return totalCount; 
	} 
 
	public int getTotalPage(PageEntity pageEntity) { 
		int totalCount = this.getTotalCount();  
		int totalPage = 0;  
		if(totalCount%pageEntity.getCountPerPage()==0) 
			totalPage = totalCount/pageEntity.getCountPerPage();  
		else  
			totalPage = totalCount/pageEntity.getCountPerPage()+1;  
		return totalPage;  
	} 
	 
	public void addFeelbackMsg(FeelbackMsgEntity msg){ 
		BaseDao dao = new BaseDao(); 
		try { 
			conn =dao.getConn(); 
			ps = conn.prepareStatement("insert into feedBack(userId,userName,sendNote,sendTime) values(?,?,?,?)"); 
			ps.setInt(1, msg.getUserId()); 
			ps.setString(2, msg.getUserName()); 
			ps.setString(3, msg.getSendNote()); 
			ps.setString(4, msg.getSendTime()); 
			ps.executeUpdate(); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		}finally{ 
			dao.closeAll(conn, ps, rs); 
		} 
	} 
}