newsdao.java

来自「模拟网上购物系统」· Java 代码 · 共 230 行

JAVA
230
字号
package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import po.newsPO;

public class newsDAO {
	
	Connection conn = null;
	Statement state = null;
	ResultSet rs = null;
	
	public ArrayList findByID(int id)
	{
		ArrayList array = new ArrayList();
		newsPO po = null;
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			rs = state.executeQuery("select * from news where typeid = " + id);
			
			while(rs.next())
			{
				po = new newsPO();
				po.setTypeid(rs.getInt("typeid"));
				po.setNewsid(rs.getInt("newsid"));
				po.setNewstital(rs.getString("newstitle"));
				po.setAddtime(rs.getString("addtime"));
				
				array.add(po);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(rs != null)
				rs.close();
				if(state != null)
					state.close();
				if(conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	     }
		return array;
	}
	
	
	//增加
	public boolean addNews(newsPO po)
	{
		boolean ok = false;
		conn = Tools.getConnection();
		try {
			state = conn.createStatement();
			int i = state.executeUpdate("insert into news values("+po.getTypeid()+","+po.getNewsid()+",'"+po.getNewstital()+"',sysdate)");
			
			if(i > 0)
			{
				ok = true;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(state != null)
					state.close();
				if(conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	     }
		return ok;
	}
	
	//
	public int NextID()
	{
		int id = 0;
		conn = Tools.getConnection();
		try {
			state = conn.createStatement();
			rs = state.executeQuery("select max(newsid) max from news");
			
			if(rs.next())
			{
				id = rs.getInt("max");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(rs != null)
				rs.close();
				if(state != null)
					state.close();
				if(conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	     }
		return ++id;
	}
	
	//
	public boolean delnews(int newsid,int typeid)
	{
		boolean isok = false;
		conn = Tools.getConnection();
		try {
			state = conn.createStatement();
			int i = state.executeUpdate("delete from news where newsid = "+newsid+" and typeid = " + typeid);
		
		    if(i > 0)
		    {
		    	isok = true;
		    }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(state != null)
					state.close();
				if(conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	     }
		return isok;
	}
	
	//
	public boolean updatanews(newsPO po)
	{
		boolean isok = false;
		conn = Tools.getConnection();
		try {
			state = conn.createStatement();
			int i = state.executeUpdate("update news set newstitle = '"+po.getNewstital()+"',addtime = sysdate where newsid = "+po.getNewsid()+" and typeid = " + po.getTypeid());
		
		    if(i > 0)
		    {
		    	isok = true;
		    }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(state != null)
					state.close();
				if(conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	     }
		return isok;
	}
	
	//
	public newsPO findByTNID(int newsid,int typeid)
	{
		newsPO po = null;
		
		conn = Tools.getConnection();
		try {
			state = conn.createStatement();
			rs  = state.executeQuery("select * from news where newsid = "+newsid+" and typeid = " + typeid);
		
		    if(rs.next())
		    {
		    	po = new newsPO();
		    	po.setNewsid(rs.getInt("newsid"));
		    	po.setTypeid(rs.getInt("typeid"));
		    	po.setNewstital(rs.getString("newstitle"));
		    	po.setAddtime(rs.getString("addtime"));
		    }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(state != null)
					state.close();
				if(conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	     }
		return po;
	}
	
}



⌨️ 快捷键说明

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