📄 noticedao.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.comingnet.bean.NoticeAppFiles;
import com.db.DBUtils;
import com.db.KeyGenerator;
public class NoticeDAO {
private Notice notice ;
public Notice getNotice() {
return notice;
}
public void setNotice(Notice notice) {
this.notice = notice;
}
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 NoticeDAO(){
notice = new Notice();
}
public boolean addNotice(Notice notice){
boolean flag = false ;
int count = 0 ;
Connection conn=null;
PreparedStatement stmt=null;
try
{
//保存插入公告的id,作为插入附件时的外键
int noticeId = KeyGenerator.getInstance().getNextKey("m_notice");
conn=DBUtils.getConnection();
stmt=conn.prepareStatement("insert into m_notice(n_id, n_bt, n_nr, n_fbrzh, n_fbrxm, n_xxbh, n_xxmc, n_xszt,n_userclass) values(?,?,?,?,?,?,?,?,?)");
stmt.setInt(1,noticeId);
stmt.setString(2, notice.getN_bt());
stmt.setString(3, notice.getN_nr());
stmt.setString(4, notice.getN_fbrzh());
stmt.setString(5, notice.getN_fbrxm());
stmt.setInt(6, notice.getN_xxbh());
stmt.setString(7, notice.getN_xxmc());
// stmt.setDate(8, new java.sql.Date(new Date().getTime()));//发布日期由数据库默认值设置为当前时间
stmt.setString(8, notice.getN_xszt());
stmt.setString(9,notice.getN_userclass()) ;
stmt.executeUpdate();
DBUtils.closeStatement(stmt);
//添加附件表
ArrayList<NoticeAppFiles> files = notice.getFiles() ;
if(files != null){
for(int i=0; i<files.size(); i++){
stmt = conn.prepareStatement("insert into m_notice_appendix(na_id, n_id, na_oldname, na_newname) values (?,?,?,?)");
stmt.setInt(1, KeyGenerator.getInstance().getNextKey("m_notice_appendix"));
stmt.setInt(2, noticeId);
stmt.setString(3,files.get(i).getOldFilename());
stmt.setString(4, files.get(i).getNewFilename());
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 m_notice_appendix where na_newname=?");
stmt.setString(1, filename);
count = stmt.executeUpdate();
DBUtils.closeStatement(stmt);
}
catch(Exception e){
System.out.println("NoticeDAO.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 m_notice_appendix where na_newname=?");
stmt.setString(1, files[i]);
count = stmt.executeUpdate();
DBUtils.closeStatement(stmt);
}
}
catch(Exception e){
System.out.println("NoticeDAO.deleFiles-->"+e);
}
finally{
DBUtils.closeStatement(stmt) ;
DBUtils.closeConnection(conn);
}
}
if(count>0) flag = true ;
return flag ;
}
public boolean deleteNotice(int id,String webpath){
boolean flag = false ;
int count = 0 ;
//查询附件表,并删除附件纪录
Connection conn=null;
PreparedStatement stmt=null;
try
{
conn=DBUtils.getConnection();
//删除文件
stmt=conn.prepareStatement("select na_newname from m_notice_appendix where n_id=?");
stmt.setInt(1, id);
ResultSet rs = null;
rs=stmt.executeQuery();
while(rs.next()){
String filename = rs.getString("na_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 m_notice_appendix where n_id=?");
stmt.setInt(1, id);
stmt.executeUpdate();
DBUtils.closeStatement(stmt);
//删除公告表纪录
stmt = conn.prepareStatement("delete m_notice where n_id=?");
stmt.setInt(1, id);
count = stmt.executeUpdate();
DBUtils.closeStatement(stmt) ;
}
catch(Exception e)
{
System.out.println("NoticeDAO.deleteNotice-->"+e);
}
finally
{
DBUtils.closeStatement(stmt);
DBUtils.closeConnection(conn);
}
if (count>0) flag =true ;
return flag ;
}
public boolean editNotice(Notice notice){
boolean flag = false ;
int count = 0 ;
// ArrayList<NoticeAppFiles> nafList ;
// nafList = notice.getFiles() ;
// NoticeAppFiles naf ;
Connection conn=null;
PreparedStatement stmt=null;
// 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);
// }
// }
// }
// }
try
{
conn=DBUtils.getConnection();
stmt=conn.prepareStatement("update m_notice set n_bt=?, n_nr=?, n_xszt=? where n_id=?");
stmt.setString(1, notice.getN_bt());
stmt.setString(2, notice.getN_nr());
stmt.setString(3, notice.getN_xszt());
stmt.setInt(4, notice.getN_id());
count = stmt.executeUpdate();
DBUtils.closeStatement(stmt) ;
}
catch(Exception e)
{
System.out.println("NoticeDAO.editNotice-->错误错误错误错误"+e);
}
finally
{
DBUtils.closeStatement(stmt);
DBUtils.closeConnection(conn);
}
if(count>0) flag = true ;
return flag ;
}
public Notice queryNoticeById(int id){
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try
{
conn=DBUtils.getConnection();
stmt=conn.prepareStatement("select n_bt, n_nr, n_fbrzh, n_fbsj from m_notice where n_xszt=1 and n_id=?");
stmt.setInt(1, id);
rs=stmt.executeQuery();
if(rs.next())
{
String title = rs.getString("n_bt");
String content = rs.getString("n_nr");
String author = rs.getString("n_fbrzh");
String date = formatDate(rs.getTimestamp("n_fbsj"));
//设置Notice
notice.setN_bt(title);
notice.setN_nr(content);
notice.setN_fbrzh(author);
notice.setN_fbsj(date);
}
DBUtils.closeResultSet(rs) ;
DBUtils.closeStatement(stmt) ;
}catch(Exception e)
{
System.out.println("NoticeDAO.queryNoticeById"+e);
}finally
{
DBUtils.closeResultSet(rs);
DBUtils.closeStatement(stmt);
DBUtils.closeConnection(conn);
}
/*以上代码设置Notice在公告表中获取的信息,然后调用AppFilesDao中的获取附件的方法,
设置Notice的附件信息*/
notice.setFiles(queryNoticeAllAppFilesById(id));
return notice;
}
/**
* 根据公告id查询对应的附件
* @param id 附件对应的公告id
* @return AppFiles[] 附件信息数组
*/
public ArrayList<NoticeAppFiles> queryNoticeAllAppFilesById(int id){
ArrayList<NoticeAppFiles> files = new ArrayList<NoticeAppFiles>();
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try
{
conn=DBUtils.getConnection();
stmt=conn.prepareStatement("select na_oldname, na_newname from m_notice_appendix where n_id=?");
stmt.setInt(1, id);
rs=stmt.executeQuery();
while(rs.next())
{
String oldFilename = rs.getString("na_oldname");
String newFilename = rs.getString("na_newname");
//设置NoticeAppFiles
NoticeAppFiles file = new NoticeAppFiles();
file.setOldFilename(oldFilename);
file.setNewFilename(newFilename);
files.add(file);
}
DBUtils.closeResultSet(rs) ;
DBUtils.closeStatement(stmt) ;
}catch(Exception e)
{
System.out.println("NoticeDAO.queryNoticeAppFilesById-->"+e);
}finally
{
DBUtils.closeResultSet(rs);
DBUtils.closeStatement(stmt);
DBUtils.closeConnection(conn);
}
return files;
}
/**
* 根据id值设置notice的详细属性,用于编辑notice
* @param id
* @return Notice
*/
public Notice getNoticeInfoById(int id){
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try
{
conn=DBUtils.getConnection();
stmt=conn.prepareStatement("select * from m_notice where n_id=?");
stmt.setInt(1, id);
rs=stmt.executeQuery();
if(rs.next())
{
String title = rs.getString("n_bt");
String content = rs.getString("n_nr");
System.out.println(content);
String author = rs.getString("n_fbrzh");
String authorName = rs.getString("n_fbrxm");
int schoolId = rs.getInt("n_xxbh");
String schoolName = rs.getString("n_xxmc");
String date = formatDate(rs.getTimestamp("n_fbsj"));
String flag = rs.getString("n_xszt");
//设置Notice
notice.setN_id(id);
notice.setN_bt(title);
notice.setN_nr(content);
notice.setN_fbrzh(author);
notice.setN_fbsj(date);
notice.setN_fbrxm(authorName);
notice.setN_xxbh(schoolId);
notice.setN_xxmc(schoolName);
notice.setN_xszt(flag);
}
DBUtils.closeResultSet(rs) ;
DBUtils.closeStatement(stmt) ;
}catch(Exception e)
{
System.out.println("NoticeDAO.getNoticeInfoById-->"+e);
}finally
{
DBUtils.closeResultSet(rs);
DBUtils.closeStatement(stmt);
DBUtils.closeConnection(conn);
}
/*以上代码设置Notice在公告表中获取的信息,然后调用queryNoticeAppFilesById获取附件的方法,
设置Notice的附件信息*/
notice.setFiles(queryNoticeAllAppFilesById(id));
return notice;
}
public ArrayList<Notice> queryAllNotice(){
ArrayList<Notice> allNoticesList = null ;
Notice notice = null ;
NoticeAppFiles af = null ;
Connection conn = null ;
ResultSet rs_appFiles = null ;
ResultSet rs_notice = null ;
Statement stmt = null ;
String sqlStr = "" ;
int n_id; //存储要修改的公告的id,更新公告数据时使用
String n_bt; //标题
String n_nr; //内容
String n_fbrzh; //发布人账户
String n_fbsj; //发布时间,存储格式化过后的时间
// ArrayList<NoticeAppFiles> files;//附件
ArrayList<NoticeAppFiles> fileList = null ;
// 以下字段供添加公告使用
String n_fbrxm; // 发布人姓名
int n_xxbh; // 学校编号
String n_xxmc; // 学校名称
String n_xszt; // 显示状态 1-显示,0-不显示
int na_id; // 自增id
String oldFilename;
String newFilename;
boolean fileflag ;
try {
sqlStr = "select * from m_notice" ;
conn = DBUtils.getConnection() ;
stmt = conn.createStatement() ;
rs_notice = stmt.executeQuery(sqlStr) ;
while(rs_notice.next()){
n_id = rs_notice.getInt("n_id") ;
n_bt = rs_notice.getString("n_bt") ;
n_nr = rs_notice.getString("n_nr") ;
n_fbrzh = rs_notice.getString("n_fbrzh") ;
n_fbsj = String.valueOf(rs_notice.getTime("n_fbsj")) ;
n_fbrxm = rs_notice.getString("n_fbrxm") ;
n_xxbh = rs_notice.getInt("n_xxbh") ;
n_xxmc = rs_notice.getString("n_xxmc") ;
n_xszt = rs_notice.getString("n_xszt") ;
sqlStr = "select * from m_notice_appendix where n_id = "+n_id ;
DBUtils.closeStatement(stmt) ;
stmt = conn.createStatement() ;
rs_appFiles = stmt.executeQuery(sqlStr) ;
while(rs_appFiles.next()){
na_id = rs_appFiles.getInt("na_id") ;
oldFilename = rs_appFiles.getString("oldFilename") ;
newFilename = rs_appFiles.getString("newFileName") ;
if("1".equals(n_xszt)) fileflag = true ;
else fileflag = false ;
af = new NoticeAppFiles ();
af.setNewFilename(newFilename) ;
af.setOldFilename(oldFilename) ;
af.setFileflag(fileflag) ;
fileList.add(af) ;
}
notice.setFiles(fileList) ;
notice.setN_bt(n_bt) ;
notice.setN_fbrxm(n_fbrxm) ;
notice.setN_fbrzh(n_fbrzh) ;
notice.setN_fbsj(n_fbsj) ;
notice.setN_id(n_id) ;
notice.setN_nr(n_nr) ;
notice.setN_xszt(n_xszt) ;
notice.setN_xxbh(n_xxbh) ;
notice.setN_xxmc(n_xxmc) ;
allNoticesList.add(notice) ;
}
DBUtils.closeResultSet(rs_appFiles) ;
DBUtils.closeResultSet(rs_notice) ;
DBUtils.closeStatement(stmt) ;
DBUtils.closeConnection(conn) ;
} catch (Exception e) {
System.out.println("NoticeDAO.queryAllNotice-->"+e) ;
}finally{
DBUtils.closeResultSet(rs_appFiles) ;
DBUtils.closeResultSet(rs_notice) ;
DBUtils.closeStatement(stmt) ;
DBUtils.closeConnection(conn) ;
}
return allNoticesList ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -