【struts2】一个BBS论坛初步显示service

/**
* 书本:【struts2】
* 功能:业务实现
* 文件:CategoryService.java
* 时间:2014年10月30日16:28:26
* 作者:cutter_point
*/

package com.whqg.bbs2009.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.whqg.bbs2009.model.Category;
import com.whqg.bbs2009.util.DB;

public class CategoryService 
{
	public void add(Category c)
	{
		Connection conn=DB.createConn();	//创建数据库连接
		String sql="insert into _category values (null, ?, ?)";
		PreparedStatement ps=DB.prepare(conn, sql);	//设置好SQL语句
		
		try 
		{
			ps.setString(1, c.getName());
			ps.setString(2, c.getDescription());
			//向数据库提交数据
			ps.executeUpdate();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		
		//关闭数据库
		DB.close(ps);
		DB.close(conn);
		
	}
	
	public List<Category> list() throws SQLException
	{
		//创建数据库连接
		Connection conn=DB.createConn();
		//输出SQL语句
		String sql="select * from _category";
		PreparedStatement ps=DB.prepare(conn, sql);
		//创建一个保存全部结果的容器
		List<Category> categories=new ArrayList<Category>();
		
		//数据库查询的结果保存
		try 
		{
			ResultSet rs=ps.executeQuery();		//记得异常处理
			Category c=null;	//床架一个空的等会一个一个的取出来
			while(rs.next())	//只要接下来还有没取出的就可以继续取
			{
				c=new Category();
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setDescription(rs.getString("description"));
				//把c添加到容器中
				categories.add(c);
			}
			
			DB.close(rs);
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
			throw(e);
		}
		
		DB.close(ps);
		DB.close(conn);
		
		//返回容器
		return categories;
	}
	
	public void delete(Category c)
	{
		deleteById(c.getId());
	}
	
	public void deleteById(int id)
	{
		Connection conn=DB.createConn();	//创建数据库连接
		String sql="delete from _category where id = ?";
		PreparedStatement ps=DB.prepare(conn, sql);	//设置好SQL语句
		
		try 
		{
			ps.setInt(1, id);
			//向数据库提交数据
			ps.executeUpdate();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		
		//关闭数据库
		DB.close(ps);
		DB.close(conn);
	}
	
	public void update(Category c)
	{
		Connection conn=DB.createConn();	//创建数据库连接
		String sql="update `_category` set `name` = ?, `description` = ? where id = ?";
		PreparedStatement ps=DB.prepare(conn, sql);	//设置好SQL语句
		
		try 
		{
			ps.setString(1, c.getName());
			ps.setString(2, c.getDescription());
			ps.setInt(3, c.getId());
			//向数据库提交数据
			ps.executeUpdate();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		
		//关闭数据库
		DB.close(ps);
		DB.close(conn);
	}
	
	//为了更新数据的时候我们得先看到原来的数据
	public Category loadById(int id)
	{
		//创建数据库连接
		Connection conn=DB.createConn();
		//设置SQL语句
		String sql="select * from _category where id = ?";
		//一个吧SQL语句传到数据库的函数
		PreparedStatement ps=DB.prepare(conn, sql);
		
		//我们要返回那个对象的数据以便更改
		Category c=null;
		try 
		{
			//设置id的值
			ps.setInt(1, id);
			//执行语句,并保存返回结果
			ResultSet rs=ps.executeQuery();
			//把得到的值传入对象中
			if(rs.next())
			{
				//如果查找到了,给c创建对象
				c=new Category();
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setDescription(rs.getString("description"));
			}
			
			DB.close(rs);
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}

		DB.close(ps);
		DB.close(conn);
		return c;
	}
	
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值