⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 noticedao.java

📁 功能是公安和学校的管理系统,用struts+hibernate+spring等匡架组成,在eclepse就能编译
💻 JAVA
字号:
package com.comingnet.notice;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import com.db.DBUtils;
import com.db.KeyGenerator;

public class NoticeDao {
	private Notice notice=null;

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}
	/**
	 * 构造函数,初始化notice
	 *
	 */
	public NoticeDao(){
		notice = new 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;
	}
	/**
	 * 根据id值设置notice的属性
	 * @param id
	 * @return Notice
	 */
	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);
		   }
		}catch(Exception e)
		{
		   System.out.println(e);
		}finally
		{
		  DBUtils.closeResultSet(rs);
		  DBUtils.closeStatement(stmt);
		  DBUtils.closeConnection(conn);
		}
		/*以上代码设置Notice在公告表中获取的信息,然后调用AppFilesDao中的获取附件的方法,
		  设置Notice的附件信息*/
		notice.setFiles(queryAppFilesById(id));
		return notice;
	}
	/**
	 * 根据公告id查询对应的附件
	 * @param id	附件对应的公告id
	 * @return	AppFiles[]	附件信息数组
	 */
	public ArrayList<AppFiles> queryAppFilesById(int id){
		ArrayList<AppFiles> files = new ArrayList<AppFiles>();
		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");      
		      //设置AppFiles
		      AppFiles file = new AppFiles();
		      file.setOldFilename(oldFilename);
		      file.setNewFilename(newFilename);
		      files.add(file);
		   }
		}catch(Exception e)
		{
		   System.out.println(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");
		      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);
		   }
		}catch(Exception e)
		{
		   System.out.println(e);
		}finally
		{
		  DBUtils.closeResultSet(rs);
		  DBUtils.closeStatement(stmt);
		  DBUtils.closeConnection(conn);
		}
		/*以上代码设置Notice在公告表中获取的信息,然后调用queryAppFilesById获取附件的方法,
		  设置Notice的附件信息*/
		notice.setFiles(queryAppFilesById(id));
		return notice;
	}
	
	/**
	 * 添加一条公告,并添加该公告对应的附件
	 * @param notice	公告内容
	 * @return	成功返回true,否则返回false
	 */
	public boolean addNotice(Notice notice){
		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) 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.executeUpdate();
		   DBUtils.closeStatement(stmt);
		   //添加附件表
		   ArrayList<AppFiles> 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());
				 stmt.executeUpdate();
				 DBUtils.closeStatement(stmt);
			  }
		   }		   
		}catch(Exception e)
		{
		   System.out.println(e);
		   return false;
		}finally
		{
		  DBUtils.closeConnection(conn);
		}
		return true;
	}
	
	/**
	 * 更新公告标题、内容、是否显示
	 * @param notice
	 * @return
	 */
	public boolean editNotice(Notice notice){
		Connection conn=null;
		PreparedStatement stmt=null;
		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());
		   stmt.executeUpdate();	 
		}
		catch(Exception e)
		{
		   System.out.println(e);
		   return false;
		}
		finally
		{
		  DBUtils.closeStatement(stmt);
		  DBUtils.closeConnection(conn);
		}
		return true;
	}
	/**
	 * 删除附件文件和附件数据库中对应的纪录
	 * @param webpath 
	 * @param files
	 * @return
	 */
	public boolean deleteFiles(String webpath, String[] files){
		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]);
					stmt.executeUpdate();
					DBUtils.closeStatement(stmt);
				}
			}
			catch(Exception e){
				System.out.println(e);
				return false;
			}
			finally{				
				DBUtils.closeConnection(conn);
			}
		}
		return true;
	}
	
	public boolean deleteNotice(int id, String webpath){
		//查询附件表,并删除附件纪录
		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);
		   stmt.executeUpdate();		   
		}
		catch(Exception e)
		{
		   System.out.println(e);
		   return false;
		}
		finally
		{
		  DBUtils.closeStatement(stmt);
		  DBUtils.closeConnection(conn);
		}
		return true;
	}
	
	public static void main(String[] args){
		NoticeDao dao1 = new NoticeDao();
		dao1.getNoticeInfoById(18);
		Notice notice = dao1.getNotice();
		System.out.println("标题:"+notice.getN_bt());
		System.out.println("内容:"+notice.getN_nr());
		System.out.println("发布人账号:"+notice.getN_fbrzh());		  
		System.out.println("发布时间:"+notice.getN_fbsj());
		System.out.println("显示标记:"+notice.getN_xszt());
		ArrayList list = dao1.queryAppFilesById(1);
		Iterator iter = list.iterator();
		while(iter.hasNext()){
			AppFiles file = (AppFiles)iter.next();
			System.out.println("原文件名:"+file.getOldFilename()+"  新文件名:"+file.getNewFilename());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -