📄 articledao.java
字号:
package com.comingnet.bean;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import com.db.DBUtils;
import com.db.KeyGenerator;
public class ArticleDAO {
private Article article;
public ArticleDAO()
{
article=new Article();
}
public Article getArticle() {
return article;
}
public void setArticle(Article article) {
this.article = article;
}
private String formatDate(Timestamp time){
String formattedDate = "";
if(time != null){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formattedDate = formatter.format(time);
}
return formattedDate;
}
public boolean addArticle(Article article){
System.out.println("开始执行文章信息保存");
boolean flag = false ;
int count = 0 ;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
int arlm1=article.getAr_lm1();
int arlm2id=0;
String arlm2=article.getAr_lm2();
try
{
//保存插入公告的id,作为插入附件时的外键
int articleId = KeyGenerator.getInstance().getNextKey("w_article");
conn=DBUtils.getConnection();
stmt=conn.prepareStatement("select slmid from w_sublmb where slmmc='"+arlm2+"' and sjlmid="+arlm1);
rs=stmt.executeQuery();
while(rs.next())
{
arlm2id=rs.getInt(1);
}
System.out.println(arlm2id+"arlm2id");
DBUtils.closeResultSet(rs);
DBUtils.closeStatement(stmt);
stmt=conn.prepareStatement("insert into w_article(ar_id, ar_bt, ar_nr, ar_fbrzh, ar_fbrxm, ar_xxbh, ar_xxmc, ar_xszt,ar_userclass,ar_lm1,ar_lm2,ar_lm2id) values(?,?,?,?,?,?,?,?,?,?,?,?)");
stmt.setInt(1,articleId);
stmt.setString(2, article.getAr_bt());
stmt.setString(3, article.getAr_nr());
stmt.setString(4, article.getAr_fbrzh());
stmt.setString(5, article.getAr_fbrxm());
stmt.setInt(6, article.getAr_xxbh());
stmt.setString(7, article.getAr_xxmc());
// stmt.setDate(8, new java.sql.Date(new Date().getTime()));//发布日期由数据库默认值设置为当前时间
stmt.setString(8, article.getAr_xszt());
stmt.setString(9,article.getAr_userclass());
stmt.setInt(10, article.getAr_lm1());
stmt.setString(11, article.getAr_lm2());
stmt.setInt(12,arlm2id);
//stmt.setDate(12, new java.sql.Date(new java.util.Date().getTime()));
stmt.executeUpdate();
DBUtils.closeStatement(stmt);
//添加附件表
ArrayList<ArticleAppFiles> files = article.getFiles() ;
if(files != null){
for(int i=0; i<files.size(); i++){
stmt = conn.prepareStatement("insert into w_article_appendix(aa_id, a_id, aa_oldname, aa_newname,aa_ms) values (?,?,?,?,?)");
stmt.setInt(1, KeyGenerator.getInstance().getNextKey("w_article_appendix"));
stmt.setInt(2, articleId);
stmt.setString(3,files.get(i).getOldFilename());
stmt.setString(4, files.get(i).getNewFilename());
stmt.setString(5, article.getAr_pic());
count=stmt.executeUpdate();
DBUtils.closeStatement(stmt);
}
}
}catch(Exception e)
{
System.out.println("NoticeDAO.addNotice-->"+e);
}finally
{
DBUtils.closeStatement(stmt);
DBUtils.closeConnection(conn);
}
if(count>0)
flag = true ;
return flag ;
}
//删除一个附件
public boolean deleteFile(String webpath ,String filename){
int count = 0 ;
boolean flag = false ;
File file = new File(webpath+"notice\\uploadFile\\"+filename);
if(file.exists()&&file.isFile()){
file.delete() ;
}
//删除数据库纪录
Connection conn=null;
PreparedStatement stmt=null;
try{
conn = DBUtils.getConnection();
stmt = conn.prepareStatement("delete w_article_appendix where aa_newname=?");
stmt.setString(1, filename);
count = stmt.executeUpdate();
DBUtils.closeStatement(stmt);
}
catch(Exception e){
System.out.println("ArticleDAO.deleteFile-->"+e);
}
finally{
DBUtils.closeStatement(stmt) ;
DBUtils.closeConnection(conn);
}
if(count>0) flag = true ;
return flag ;
}
//删除所有的附件
public boolean deleteAllFiles(String webpath, String[] files){
int count = 0 ;
boolean flag = false ;
if(files != null){
//删除文件
for(int i=0; i<files.length; i++){
File file = new File(webpath+"notice\\uploadFile\\"+files[i]);
if(file.exists() && file.isFile()){
file.delete();
}
}
//删除数据库纪录
Connection conn=null;
PreparedStatement stmt=null;
try{
conn = DBUtils.getConnection();
for(int i=0; i<files.length; i++){
stmt = conn.prepareStatement("delete w_article_appendix where aa_newname=?");
stmt.setString(1, files[i]);
count = stmt.executeUpdate();
DBUtils.closeStatement(stmt);
}
}
catch(Exception e){
System.out.println("ArticleDAO.deleFiles-->"+e);
}
finally{
DBUtils.closeStatement(stmt) ;
DBUtils.closeConnection(conn);
}
}
if(count>0) flag = true ;
return flag ;
}
public boolean deleteArticle(int id,String webpath){
boolean flag = false ;
int count = 0 ;
//查询附件表,并删除附件纪录
Connection conn=null;
PreparedStatement stmt=null;
try
{
conn=DBUtils.getConnection();
//删除文件
stmt=conn.prepareStatement("select aa_newname from w_article_appendix where a_id=?");
stmt.setInt(1, id);
ResultSet rs = null;
rs=stmt.executeQuery();
while(rs.next()){
String filename = rs.getString("aa_newname");
filename = webpath+"notice\\uploadFile\\"+filename;
File file = new File(filename);
if(file.exists() && file.isFile()){
file.delete();
}
}
DBUtils.closeResultSet(rs);
DBUtils.closeStatement(stmt);
//删除附件表纪录
stmt = conn.prepareStatement("delete w_article_appendix where a_id=?");
stmt.setInt(1, id);
stmt.executeUpdate();
DBUtils.closeStatement(stmt);
//删除公告表纪录
stmt = conn.prepareStatement("delete w_article where ar_id=?");
stmt.setInt(1, id);
count = stmt.executeUpdate();
DBUtils.closeStatement(stmt) ;
}
catch(Exception e)
{
System.out.println("ArticleDAO.deleteNotice-->"+e);
}
finally
{
DBUtils.closeStatement(stmt);
DBUtils.closeConnection(conn);
}
if (count>0) flag =true ;
return flag ;
}
public boolean editArticle(Article article){
boolean flag = false ;
int count = 0 ;
// ArrayList<NoticeAppFiles> nafList ;
// nafList = notice.getFiles() ;
// NoticeAppFiles naf ;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String arlm2="";
int arlm2id=0;
int arlm1id=0;
// naf = notice.getFiles().get(0) ;
// System.out.println("newname"+naf.getNewFilename()) ;
// System.out.println("isFileflag"+naf.isFileflag()) ;
//
// //删除已经标记的附件
// int size = 0 ;
// size = nafList.size();
//
// if(size >0){
// for (int i = 0; i < size; i++) {
// naf = notice.getFiles().get(i) ;
//// System.out.println("newname"+naf.getNewFilename()) ;
//// System.out.println("isFileflag"+naf.isFileflag()) ;
// if(naf.isFileflag()){
// try {
// conn = DBUtils.getConnection() ;
// stmt = conn.prepareStatement("delete m_notice_appendix where newFileName = ?") ;
// stmt.setString(1,naf.getNewFilename()) ;
// stmt.executeUpdate() ;
//
// DBUtils.closeStatement(stmt) ;
// } catch (Exception e) {
// System.out.println("NoticeDAO.editNotice-->" + e);
// } finally {
// DBUtils.closeStatement(stmt);
// DBUtils.closeConnection(conn);
// }
// }
// }
// }
arlm2=article.getAr_lm2();
arlm1id=article.getAr_lm1();
try{
conn=DBUtils.getConnection();
stmt=conn.prepareStatement("select slmid from w_sublmb where slmmc='"+arlm2+"' and sjlmid="+arlm1id);
rs=stmt.executeQuery();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -