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

📄 newsdao.java

📁 新闻发布系统,界面简单
💻 JAVA
字号:
package com.javaBeanDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import com.sql.*;

//import com.aptech.util.DatabaseTool;
import com.javaBean.*;
/**
 * 这是一个对news表的数据javaBean进行操作的javaBean
 * 对该表执行添,删,改,查的方法
 * @author ACCP
 *
 */
public class NewsDao {
	/**
	 * 这是一个专门用来对news表的数据javaBean进行添加操作的方法
	 * @param news
	 * @return 返回true就添加成功,否则就失败
	 */
     public boolean getNewsInsert(News news)
     {
    	 String sql="insert into news values(''{0}'',{1},{2},{3},''{4}'')";
    	 MessageFormat mf=new MessageFormat(sql);
    	 Object[] obj=new Object[]{news.getNtitle(),news.getMid()+"",news.getNtid()+"","default",news.getNcontent()};
    	 sql=mf.format(obj);    	
    	 return SQL.getSQL().getInsertAndUpdateAndDelete(sql);
     }
     /**
      * 这是一个专门用来对news表的数据javaBean进行删除操作的方法
      * @param nid
      * @return 返回true就删除成功,否则就失败
      */
     public boolean getNewsDelete(int nid)
     {
    	 String sql="delete from news where nid="+nid;
    	
		return SQL.getSQL().getInsertAndUpdateAndDelete(sql);
		
     }
     /**
      * 这是一个专门用来对news表的数据javaBean进行修改操作的方法
      * @param news
      * @return 返回true就修改成功,否则就失败
      */
     public boolean getNewsUpdate(News news)
     {
    	 
    	 String sql="update news set ntitle=''{0}'',mid={1},ntid={2},ncontent=''{3}'' where nid={4}";
		 MessageFormat mf=new MessageFormat(sql);
		 Object[] obj=new Object[]{news.getNtitle(),news.getMid()+"",news.getNtid()+"",news.getNcontent(),news.getNid()+""};
		 sql=mf.format(obj);
		
    	 return SQL.getSQL().getInsertAndUpdateAndDelete(sql);
    	 
     }
     /**
      * 这是一个专门用来对news表的数据javaBean进行查询操作的方法      * 
      * @return 返回List就查询成功,否则就失败
      */
     public List getNewsSelect()
     {
    	 String sql="select a.nid,a.ntitle,b.mname,c.ntname,a.ndatetime,a.ncontent from news a inner join manager b on a.mid=b.mid  inner join newsType c on a.ntid=c.ntid order by nid desc";
		
		 ResultSet rs=SQL.getSQL().getSelect(sql);
		
		 List list=new ArrayList();
		 News news=null;
		 try {
			while(rs.next())
			 {
				 news=new News();
				 news.setNid(rs.getInt(1));
				 news.setNtitle(rs.getString(2));
				 news.setMname(rs.getString(3));
				 news.setNname(rs.getString(4));
				 news.setNdatetime(rs.getString(5));
				 news.setNcontent(rs.getString(6));
				 list.add(news);
			 }
			 System.out.println("ID"+news.getNtid());
			
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally
		{
			SQL.getSQL().getClass();
		}
		
    	 
     }
     /**
      * 这是一个专门用来对news表的数据javaBean根据新闻ID进行查询操作的方法      * 
      * @return 返回News就查询成功,否则就失败
      */
     public News getNidSelect(int nid)
     {
    	 
    	 String sql="select a.nid,a.ntid,a.ntitle,b.mname,c.ntname,a.ndatetime,a.ncontent from news a inner join manager b on a.mid=b.mid  inner join newsType c on a.ntid=c.ntid where a.nid="+nid+"order by nid desc";
		
		 ResultSet rs=SQL.getSQL().getSelect(sql);
		 NewsReplyDao nr=new NewsReplyDao();
		 List list=nr.getMaxNewsReplyCount();
		 int nidcount=0;
		
		
		
		 News news=null;
		 try {
			if(rs.next())
			 {
				 news=new News();
				 news.setNid(rs.getInt(1));
				 news.setNtid(rs.getInt(2));
				 news.setNtitle(rs.getString(3));
				 news.setMname(rs.getString(4));
				 news.setNname(rs.getString(5));
				 news.setNdatetime(rs.getString(6));
				 news.setNcontent(rs.getString(7));
				
			 }
			
			return news;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally
		{
			SQL.getSQL().getClass();
		}
		
    	 
     }
     /**
      * 根据新闻类别ID找出相应的所有新闻标题
      * @param ntid
      * @return
      */
     
     public List getNtidSelectList(int ntid)
     {
    	 
    	 String sql="select a.nid,a.ntitle,b.mname,c.ntname,a.ndatetime,a.ncontent from news a inner join manager b on a.mid=b.mid  inner join newsType c on a.ntid=c.ntid where a.ntid="+ntid+"order by nid desc";
		
		 ResultSet rs=SQL.getSQL().getSelect(sql);
		
		 List list =new ArrayList();
		 News news=null;
		 try {
			if(rs != null){
			while(rs.next())
			 {
				 news=new News();
				 news.setNid(rs.getInt(1));
				 news.setNtitle(rs.getString(2));
				 news.setMname(rs.getString(3));
				 news.setNname(rs.getString(4));
				 news.setNdatetime(rs.getString(5));
				 news.setNcontent(rs.getString(6));
				// news.setn
				 list.add(news);
			 }
			}
			 //System.out.println("ID"+news.getNtid());
			 //System.out.println("发布人"+news.getMname());
			// System.out.println("新闻标题"+news.getNname());
			 
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally
		{
			SQL.getSQL().getClass();
		}
		
    	 
     }
     
     /**
      * 根据新闻类别名称查找其ID,方便将数据添加到新闻表中
      * @param ntname
      * @return
      */
     public int getNewsTypeNtid(String ntname)
     {
    	 String sql="select ntid from newsType where ntname='"+ntname+"'";
    	 
    	 ResultSet rs=SQL.getSQL().getSelect(sql);
    	
    	 int ntid=0;
    	 try {
			if(rs.next())
			 {
				 ntid=rs.getInt(1);
			 }
			return ntid;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
     }
     /**
      * 根据管理员名称查找其ID,方便将管理员ID数据添加到新闻表的新闻发布字段
      * @param mname
      * @return
      */
     public int getManagerMid(String mname)
     {
    	 String sql="select mid from manager where mname='"+mname+"'";
    	 ResultSet rs=SQL.getSQL().getSelect(sql);
    	 int mid=0;
    	 try {
			if(rs.next())
			 {
				 mid=rs.getInt(1);
			 }
			return mid;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
     }
     /**
      * 查询到新闻的总记录条数,为分页做计算
      * @return
      */
     public int getNewsCount(int ntid)
     {
    	 String sql="select count(*) from news where ntid="+ntid;
    	 ResultSet rs=SQL.getSQL().getSelect(sql);
    	 
    	 int count=0;
    	 try {
			if(rs.next())
			 {
				 count=rs.getInt(1);
				 
			 }
			
			return count;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
     }
     /**
      * 通过得到新闻部条数和页大小(每页有的条数)计算最多有多少页
      * @param pageSize
      * @return
      */
     public int getPageSize(int pageSize, int count)
     {
    	 if(count%pageSize==0)
    	 {
    		
    		 return count/pageSize;
    		 
    	 }
    	 else 
    	 {
    		
    		 return count/pageSize+1;
    	 }
    	
     }
     /**
      * 分页的方法,返回指定页数和指定页大小的数据
      * @param curpage
      * @param pageSize
      * @return
      */
     public List getNewsPageCount(int curpage,int pageSize,int ntid)
     {
    	 String sql="select top {0} a.nid,a.ntitle,b.mname,a.ntid,c.ntname,a.ndatetime,a.ncontent from news a inner join manager b on a.mid=b.mid  inner join newsType c on a.ntid=c.ntid where nid not in("+"select top {1} nid from news where ntid={2} order by nid desc "+")and a.ntid={3} order by nid desc";
    	 MessageFormat mf=new MessageFormat(sql);
    	 Object[] obj=new Object[]{pageSize+"",((curpage-1)*pageSize)+"",ntid+"",ntid+""};
    	 sql=mf.format(obj);
    	
    	 ResultSet rs=SQL.getSQL().getSelect(sql);
    	
    	 List list=new ArrayList();
    	 News news=null;
    	 try {
    		 if(rs!=null){
			while(rs.next())
			 {
				 news=new News();
				 news.setNid(rs.getInt(1));
				 news.setNtitle(rs.getString(2));
				 news.setMname(rs.getString(3));
				 news.setNtid(rs.getInt(4));
				 news.setNname(rs.getString(5));
				 news.setNdatetime(rs.getString(6));
				 news.setNcontent(rs.getString(7));
				 list.add(news);
			 }
    		 }
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
     }
     /**
      * 查询最大的ID,即最新的新闻
      * @return
      */
     public int getMaxNewsid()
     {
    	 String sql="select max(nid) from news";
    	 ResultSet rs=SQL.getSQL().getSelect(sql);
    	 int maxid=0;
    	 try {
			if(rs.next())
			 {
				 maxid=rs.getInt(1);
				 
			 }
			return maxid;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
     }
     /**
 	 * 批量删除留言
 	 * @param ids "sid1,sid2,sid3"
 	 * @return
 	 */
 	public boolean deleteBatchSaying(String nid){
 		String sql = "delete from news where nid in(" + nid +")";
 		System.out.println("批量删除的SQL语句是:"+sql);
 		return SQL.getSQL().getInsertAndUpdateAndDelete(sql);
 	}
     
}

⌨️ 快捷键说明

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