📄 articledaoim.java
字号:
package com.yhcms.article.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.yhcms.artclass.dao.ArtClassDaoIm;
import com.yhcms.article.bean.Article;
import com.yhcms.article.dto.ArtIndexDto;
import com.yhcms.article.dto.ArticleDto;
import com.yhcms.article.dto.PreNextDto;
import com.yhcms.article.itface.ArticleDao;
import com.yhcms.db.DBConnException;
import com.yhcms.db.DBConnect;
import com.yhcms.utils.StringUtils;
/**
* <p>Title:系统文章的相关操作</p>
* <li>文章与数据库相关的各项操作</li>
* <br><b>CopyRight: yyhweb[由由华网]</b>
* @author stephen
* @version YH-2.0
*/
public class ArticleDaoIm implements ArticleDao {
private static Logger yhlog = Logger.getLogger(ArtClassDaoIm.class.getName());
private DBConnect dbconn = null;
private static ArticleDaoIm articledao = new ArticleDaoIm();
/**
* @return 取得一个系统文章操作对象
*/
public static ArticleDaoIm getInstance(){
return articledao;
}
public boolean addArticle(Article article) throws DBConnException {
int i = 0;
// 取得系统文章最大Id+1 作为新录入文章的Id
int maxid = getArtMaxId()+1;
Article curArticle = article;
String ftitle = curArticle.getFirsttitle();
String stitle = curArticle.getSecondtitle();
String cmfrom = curArticle.getComefrom();
String author = curArticle.getAuthor();
String editor = curArticle.getEditor();
int bigclassid = curArticle.getBigclassid();
String bigclass = curArticle.getBigclass();
String content = curArticle.getContent();
String ftime = curArticle.getFdatetime();
String fip = curArticle.getFip();
int readtimes = curArticle.getReadtimes();
int bid = curArticle.getBid();
// String sql = "insert into article(id,ftitle,stitle,cmfrom,author,editor,bigclassid,bigclass," +
// "content,ftime,fip,readtimes,bid) values ("+
// maxid+",'"+ftitle+"','"+stitle+"','"+cmfrom+"','"+author+"','"+editor+"',"+bigclassid+",'"+
// bigclass+"','"+content+"','"+ ftime+"','"+fip+"','"+readtimes+"','"+bid+"')";
String sql = "insert into article(id,ftitle,stitle,cmfrom,author,editor,bigclassid,bigclass," +
"content,ftime,fip,readtimes,bid) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
try{
dbconn = new DBConnect(sql);
dbconn.setInt(1, maxid);
dbconn.setString(2, ftitle);
dbconn.setString(3, stitle);
dbconn.setString(4, cmfrom);
dbconn.setString(5, author);
dbconn.setString(6, editor);
dbconn.setInt(7, bigclassid);
dbconn.setString(8, bigclass);
dbconn.setString(9, content);
dbconn.setString(10,ftime);
dbconn.setString(11,fip);
dbconn.setInt(12, readtimes);
dbconn.setInt(13, bid);
i = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When add an article,throw an Exception!");
}finally{
dbconn.close();
}
if(i>0){
yhlog.info("Add an article successfully,the article id is:"+maxid+".");
return true;
}else{
yhlog.info("Add one article unsuccessfully!");
return false;
}
}
public boolean delArticle(int id) throws DBConnException {
int i = 0;
int j = 0;
int bid = 0;
String sqlbid = "select bid from article where id="+id;
String sql = "delete from article where id="+id;
String sqlup = "";
try{
dbconn = new DBConnect(sqlbid);
dbconn.executeQuery();
if(dbconn.next()){
bid = dbconn.getInt(1);
}
}catch(Exception e){
yhlog.warn("When delete an article,throw an Exception!the article id is:"+id+".");
}finally{
dbconn.close();
}
try{
dbconn = new DBConnect(sql);
i = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When delete an article,throw an Exception!the article id is:"+id+".");
}finally{
dbconn.close();
}
if(i>0){ // delete article ok,then update other article bid
try{
sqlup = "update article set bid="+bid+" where bid="+id;
dbconn = new DBConnect(sqlup);
j = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When delete an article,throw an Exception!the article id is:"+id+".");
}finally{
dbconn.close();
}
if(j>=0){
yhlog.info("Delete an article successfully,the article id is:"+id+".");
return true;
}else{
yhlog.info("When update an article next one's bid unsuccessfully!");
return false;
}
}else{
yhlog.info("Delete an article unsuccessfully!");
return false;
}
}
public boolean updateArticle(Article article) throws DBConnException {
int i = 0;
Article curArticle = article;
int id = 0;
String ftitle = "";
String stitle = "";
String cmfrom = "";
String author = "";
String editor = "";
int bigclassid = 0;
String bigclass = "";
String content = "";
int bid = 0;
id = curArticle.getId();
ftitle = curArticle.getFirsttitle();
stitle = curArticle.getSecondtitle();
cmfrom = curArticle.getComefrom();
author = curArticle.getAuthor();
editor = curArticle.getEditor();
bigclassid = curArticle.getBigclassid();
bigclass = curArticle.getBigclass();
content = curArticle.getContent();
bid = curArticle.getBid();
String sql = "update article set ftitle=?,stitle=?,cmfrom=?,author=?,editor=?,bigclassid=?,bigclass=?," +
"content=?,bid=? where id="+id;
// String sql = "update article set ftitle='"+ftitle+"',stitle='"+stitle+"',cmfrom='"+cmfrom+"',author='"+
// author+"',editor='"+editor+"',bigclassid="+bigclassid+",bigclass='"+bigclass+
// "',content='"+content+"',bid="+bid+" where id="+id;
try{
dbconn = new DBConnect(sql);
dbconn.setString(1, ftitle);
dbconn.setString(2, stitle);
dbconn.setString(3, cmfrom);
dbconn.setString(4, author);
dbconn.setString(5, editor);
dbconn.setInt(6, bigclassid);
dbconn.setString(7, bigclass);
dbconn.setString(8, content);
dbconn.setInt(9, bid);
i = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When update an article,throw an Exception!The article id is:"+id+".");
}finally{
dbconn.close();
}
if(i>0){
yhlog.info("Update an article successfully,the article id is:"+id+".");
return true;
}else{
yhlog.info("Update an article unsuccessfully!");
return false;
}
}
public List getArtByClassId(int classid,int begin,int size) throws DBConnException {
ArrayList artlist = new ArrayList();
ArticleDto curArt = null;
String str = "";
String sql = "select a.id,a.ftitle,a.author,a.ftime,a.readtimes from article as a " +
"where a.bigclassid="+classid+" order by a.id desc limit "+begin+","+size;
try{
dbconn = new DBConnect(sql);
dbconn.executeQuery();
while(dbconn.next()){
curArt = new ArticleDto();
curArt.setId(dbconn.getInt(1));
str = dbconn.getString(2);
if(str.length()>16){
curArt.setFirsttitle(str.substring(0,16)+"......");
}else{
curArt.setFirsttitle(str);
}
curArt.setAuthor(dbconn.getString(3));
curArt.setFdatetime(dbconn.getString(4));
curArt.setReadtimes(dbconn.getInt(5));
artlist.add(curArt);
}
}catch(Exception e){
e.printStackTrace();
yhlog.warn("When get a class's article,throw an Exception!The class id is:"+classid+".");
}finally{
dbconn.close();
}
return artlist;
}
public List getIndexArt(int classid,int begin,int size) throws DBConnException {
ArrayList artlist = new ArrayList();
ArtIndexDto curArt = null;
String str = "";
String sql = "select a.id,a.ftitle,a.ftime from article as a " +
"where a.bigclassid="+classid+" order by a.id desc limit "+begin+","+size;
try{
dbconn = new DBConnect(sql);
dbconn.executeQuery();
while(dbconn.next()){
curArt = new ArtIndexDto();
curArt.setId(dbconn.getInt(1));
str = dbconn.getString(2);
if(str.length()>18){
curArt.setFtitle(str.substring(0,18)+"......");
}else{
curArt.setFtitle(str);
}
curArt.setFtime(dbconn.getString(3));
artlist.add(curArt);
}
}catch(Exception e){
e.printStackTrace();
yhlog.warn("When get a class's article,throw an Exception!The class id is:"+classid+".");
}finally{
dbconn.close();
}
return artlist;
}
public List getAllArts(int begin, int size) throws DBConnException {
ArrayList artlist = new ArrayList();
ArticleDto curArt = null;
String str = "";
String sql = "select a.id,a.ftitle,a.bigclassid,a.bigclass,a.author,a.ftime,a.readtimes from article as a " +
" order by a.id desc limit "+begin+","+size;
try{
dbconn = new DBConnect(sql);
dbconn.executeQuery();
while(dbconn.next()){
curArt = new ArticleDto();
curArt.setId(dbconn.getInt(1));
str = dbconn.getString(2);
if(str.length()>16){
curArt.setFirsttitle(str.substring(0,16)+"......");
}else{
curArt.setFirsttitle(str);
}
curArt.setBigclassid(dbconn.getInt(3));
curArt.setBigclass(dbconn.getString(4));
curArt.setAuthor(dbconn.getString(5));
curArt.setFdatetime(dbconn.getString(6));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -