package s2jsp.lg.dao.impl;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.sql.*;
//import java.sql.Date;
//import s2jsp.lg.dao.impl.*;
import s2jsp.lg.dao.ReplyDao;
import s2jsp.lg.entity.Reply;
public class ReplyDaoImpl extends BaseDao implements ReplyDao {
private Connection con= null;
private PreparedStatement ps= null;
private ResultSet rs= null ;
public int addReply(Reply reply) {
String sql = "insert into TBL_REPLY(title,content,publishTime,modifyTime,uId,topicId) values(?,?,?,?," + reply.getUId()+ "," + reply.getTopicId() + ")";
String time=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
String[] parm={ reply.getTitle(), reply.getContent(), time, time };
try {
return this.chuli(sql, parm);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int deleteReply(int replyId) {
String sql = "delete from TBL_REPLY where replyId=" + replyId;
try {
return this.chuli(sql, null) ;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return replyId;
}
public int updateReply(Reply reply) {
String sql = "update TBL_REPLY set title=?, content=?, modifyTime=? where replyId="+reply.getReplyId();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String[] parm = { reply.getTitle(), reply.getContent(), time };
try {
return this.chuli(sql, parm);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public List findListReply(int page, int topicId) {
List list = new ArrayList();
con = null;
ps = null;
rs = null;
int rowBegin = 0;
if( page > 1 ) {
rowBegin = 10 * (page-1);
}
try {
con =BaseDao.getConnection();
String sql = "select top 10 * from TBL_REPLY where topicId=" + topicId + " and replyId not in(select top "+ rowBegin+ " replyId from TBL_REPLY where topicId=" + topicId + "order by publishTime )order by publishTime";
ps= con.prepareStatement(sql);
rs = ps.executeQuery();
while ( rs.next() ) {
Reply reply = new Reply();
reply.setReplyId(rs.getInt("replyId"));
reply.setTitle(rs.getString("title"));
reply.setContent(rs.getString("content"));
reply.setPublishTime(rs.getString("publishTime"));
reply.setModifyTime(rs.getString("modifyTime"));
reply.setTopicId(rs.getInt("topicId"));
reply.setUId(rs.getInt("uId"));
list.add(reply);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
BaseDao.closeConnection(con);
BaseDao.closeStatement(ps);
}
return list;
}
public int findCountReply(int topicId){
int count = 0;
Connection con= null;
PreparedStatement ps= null;
ResultSet rs= null;
String sql= "select count(*) from TBL_REPLY where topicId=" + topicId;
try {
con=BaseDao.getConnection();
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while( rs.next() )
{
count = rs.getInt(1);
}
}
catch ( Exception e)
{
e.printStackTrace();
}
finally
{
BaseDao.closeConnection(con);
BaseDao.closeResultSet(rs);
BaseDao.closeStatement(ps);
}
return count;
}
public Reply findReply(int replyId) {
String sql = "select * from TBL_REPLY where replyId=?";
Reply reply = null;
try {
con=BaseDao.getConnection();
ps=con.prepareStatement(sql);
ps.setInt(1, replyId);
rs= ps.executeQuery();
while ( rs.next() ) {
reply = new Reply();
reply.setReplyId(rs.getInt("replyId"));
reply.setTitle(rs.getString("title"));
reply.setContent(rs.getString("content"));
reply.setPublishTime(rs.getString("publishTime"));
reply.setModifyTime(rs.getString("modifyTime"));
reply.setUId(rs.getInt("uId"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
BaseDao.closeConnection(con);
BaseDao.closeResultSet(rs);
BaseDao.closeStatement(ps);
}
return reply;
}
}