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

📄 newsdaoimpl.java

📁 struts2+spring+Hibernate整合的一个网站案例
💻 JAVA
字号:
package myitem.dao.impl;

import myitem.dao.NewsDAO;
import myitem.db.*;

import java.util.*;
import java.sql.*;
import myitem.vo.News;


public class NewsDAOImpl implements NewsDAO 
{

	 public void insert(News news) throws Exception
	 {
		 
	 }
	
	 public List newsAll() throws Exception
	 
	 {
		
		 List allnews=new ArrayList();
		 String sql="select id,title,content,uptime,seecs,upname from new where newtype=1 order by uptime desc";
		 PreparedStatement pstmt=null;
		 ConnectionDatabase dbc=null;
		 dbc = new ConnectionDatabase();	 		 
		 try
		 {
			 pstmt=dbc.getConn().prepareStatement(sql);
			 ResultSet rss=pstmt.executeQuery();
			 while(rss.next())
			 {
				 News news=new News();
				 news.setId(rss.getInt(1));
				 news.setTitle(rss.getString(2));
				 news.setContent(rss.getString(3));				
				 news.setUptime(rss.getString(4));
				 news.setSeecs(rss.getInt(5));
				 news.setUpname(rss.getString(6));
				 allnews.add(news);
				 
				
			 }
			 rss.close();
			 pstmt.close();
		 }
		 catch(Exception e)
		 {
			 
		 }
		 finally
		 {
			 try
			 {
				 if(dbc!=null)
				 {
					 dbc.close();
				 }
			 }
			 catch(Exception e)
			 {
				 
			 }
		 }
		 return allnews;
	 }
	 
public List newsAlll() throws Exception
	 
	 {
		
		 List all=new ArrayList();
		 String sql="select id,title,content,uptime,seecs,upname from new where newtype=2 order by uptime desc";
		 PreparedStatement pstmt=null;
		 ConnectionDatabase dbc=null;
		 dbc = new ConnectionDatabase();	 		 
		 try
		 {
			 pstmt=dbc.getConn().prepareStatement(sql);
			 ResultSet rs=pstmt.executeQuery();
			 while(rs.next())
			 {
				 News news=new News();
				 news.setId(rs.getInt(1));
				 news.setTitle(rs.getString(2));
				 news.setContent(rs.getString(3));				
				 news.setUptime(rs.getString(4));
				 news.setSeecs(rs.getInt(5));
				 news.setUpname(rs.getString(6));
				 all.add(news);
				 
				
			 }
			 rs.close();
			 pstmt.close();
		 }
		 catch(Exception e)
		 {
			 
		 }
		 finally
		 {
			 try
			 {
				 if(dbc!=null)
				 {
					 dbc.close();
				 }
			 }
			 catch(Exception e)
			 {
				 
			 }
		 }
		 return all;
	 }
    public News newsId(int id) throws Exception
    
    {
    	String sql="select title,content,uptime,seecs,upname from new where id=?";
    	News news=null;
    	 PreparedStatement pstmt=null;
		 ConnectionDatabase dbc=null;
		 dbc = new ConnectionDatabase();	
		 try
		 {
			 pstmt = dbc.getConn().prepareStatement(sql) ;
				pstmt.setInt(1,id) ;
				ResultSet rs = pstmt.executeQuery() ;
				if(rs.next())
				{
					news=new News();
					news.setTitle(rs.getString(1));
					news.setContent(rs.getString(2));
					news.setUptime(rs.getString(3));
					news.setSeecs(rs.getInt(4));
					news.setUpname(rs.getString(5));
				}
				rs.close();
				pstmt.close();
		 }
		 catch(Exception e)
		 {
			 
		 }
		 finally
		 {
			 try
			 {
				 if(dbc!=null)
				 {
					 dbc.close();
				 }
			 }
			 catch(Exception e)
			 {
				 
			 }
		 }
		 return news;
    }
    public List adminews() throws Exception
	 
	 {
		
		 List allnews=new ArrayList();
		 String sql="select id,title,content,uptime,seecs,upname,newtype from new order by uptime desc";
		 PreparedStatement pstmt=null;
		 ConnectionDatabase dbc=null;
		 dbc = new ConnectionDatabase();	 		 
		 try
		 {
			 pstmt=dbc.getConn().prepareStatement(sql);
			 ResultSet rss=pstmt.executeQuery();
			 while(rss.next())
			 {
				 News news=new News();
				 news.setId(rss.getInt(1));
				 news.setTitle(rss.getString(2));
				 news.setContent(rss.getString(3));				
				 news.setUptime(rss.getString(4));
				 news.setSeecs(rss.getInt(5));
				 news.setUpname(rss.getString(6));
				 news.setNewtype(rss.getString(7));
				 allnews.add(news);
				 
				
			 }
			 rss.close();
			 pstmt.close();
		 }
		 catch(Exception e)
		 {
			 
		 }
		 finally
		 {
			 try
			 {
				 if(dbc!=null)
				 {
					 dbc.close();
				 }
			 }
			 catch(Exception e)
			 {
				 
			 }
		 }
		 return allnews;
	 }
    public void delnews(int id) throws Exception
    
    {
    	 String sql="delete from new where id=?";
    	 PreparedStatement pstmt=null;
    	 ConnectionDatabase dbc=null;
    	 dbc=new ConnectionDatabase();
    	 
    	 try
    	 {
    		 pstmt=dbc.getConn().prepareStatement(sql);
    		 pstmt.setInt(1,id);
    		 pstmt.executeUpdate();
    		 pstmt.close();
    	 }
    	 catch(Exception e)
    	 {
    		 System.out.print("发生SQL异常");
    	 }
    	 finally
    	 {
    	 try
    	   {
    		 if(dbc!=null)
    		 {
    			 dbc.close();
    		 }
    	    }
    	  catch(Exception e)
    	    {
    		 
    	     }
    	 }
    }
}

⌨️ 快捷键说明

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