📄 comtdaoim.java
字号:
package com.yhcms.comt.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.yhcms.comt.bean.Comment;
import com.yhcms.comt.itface.ComtDao;
import com.yhcms.db.DBConnException;
import com.yhcms.db.DBConnect;
import com.yhcms.utils.DateUtils;
/**
* <p>Title:系统文章评论的相关操作</p>
* <li>文章评论与数据库相关的各项操作</li>
* <b>CopyRight: yyhweb[由由华网]</b>
* @author stephen
* @version YH-2.0
*/
public class ComtDaoIm implements ComtDao {
private static Logger yhlog = Logger.getLogger(ComtDaoIm.class.getName());
private DBConnect dbconn = null;
private static ComtDaoIm comtdao = new ComtDaoIm();
/**
* @return 取得一个系统文章评论对象
*/
public static ComtDaoIm getInstance(){
return comtdao;
}
public boolean addComt(Comment comment) throws DBConnException {
int i = 0;
// 取得系统文章评论最大Id+1 作为新评论的Id
int maxid = getMaxId()+1;
Comment curComt = comment;
int artId = curComt.getArtid();
String name = curComt.getName();
String email = curComt.getEmail();
String content = curComt.getContent();
String ip = curComt.getIp();
String ptime = curComt.getPtime();
String sql = "insert into comt(id,artid,name,email,content,ip,ptime) values ("+
maxid+","+artId+",'"+name+"','"+email+"','"+content+"','"+ip+"','"+ptime+"')";
try{
dbconn = new DBConnect(sql);
i = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("when add a article comment,Exception occur!");
}finally{
dbconn.close();
}
if(i>0){
yhlog.info("Add one article comment successed,It's id is:"+maxid+".");
return true;
}else{
yhlog.info("Add one article comment failed!");
return false;
}
}
public boolean delComt(int id) throws DBConnException {
int i = 0;
String sql = "delete from comt where id="+id;
try{
dbconn = new DBConnect(sql);
i = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("when delete a article comment from system,Exception occur!");
}finally{
dbconn.close();
}
if(i>0){
yhlog.info("Delete one article comment successed,It's id is:"+id+".");
return true;
}else{
yhlog.info("Delete one article comment failed!");
return false;
}
}
public List getAllByArtId(int artId) throws DBConnException {
ArrayList comlist = new ArrayList();
Comment curcomt = null;
String sql = "select c.id,c.name,c.email,c.content,c.ip,c.ptime from comt as c " +
"where c.artid="+artId+" order by c.ptime";
try{
dbconn = new DBConnect(sql);
dbconn.executeQuery();
while(dbconn.next()){
curcomt = new Comment();
curcomt.setId(dbconn.getInt(1));
curcomt.setName(dbconn.getString(2));
curcomt.setEmail(dbconn.getString(3));
curcomt.setContent(dbconn.getString(4));
curcomt.setIp(dbconn.getString(5));
curcomt.setPtime(DateUtils.getStr2Str("yyyy-MM-dd hh:mm:ss",dbconn.getString(6)));
comlist.add(curcomt);
}
}catch(Exception e){
e.printStackTrace();
yhlog.warn("when get one article's comment of system,Exception occur!");
}finally{
dbconn.close();
}
return comlist;
}
public int getMaxId() throws DBConnException {
int maxId = 0;
String sql = "select max(id) from comt";
try{
dbconn = new DBConnect(sql);
dbconn.executeQuery();
if(dbconn.next()){
maxId = dbconn.getInt(1);
}
}catch(Exception e){
yhlog.warn("when get the max id of system article comment,Exception occur!");
}finally{
dbconn.close();
}
return maxId;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -