知识库系统源代码——持久层(JDBC)

6 篇文章 0 订阅
package com.cr.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.cr.model.News;
import com.cr.model.PageBean;
import com.cr.util.StringUtil;

/**
 * @author 汤如义
 * @项目名称:Rbps   
 * @类名称:NewsDao   
 * @类描述:  信息dao层    
 * @创建时间:2016年7月28日 上午9:39:44      
 */
public class NewsDao {
	
	public ResultSet newsList(Connection con,PageBean pageBean,News news) throws Exception {
		StringBuffer sb=new StringBuffer("select * from t_news t  where 1=1 ");
		if(StringUtil.isNotEmpty(news.getName())){
			sb.append(" and t.name like '%"+news.getName()+"%' ");
		}
		sb.append(" order by t.createtime desc ");
		if(pageBean!=null){
			sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}
	
	public int newsCount(Connection con,News news) throws Exception{
		StringBuffer sb=new StringBuffer("select count(*) as total from t_news t  where 1=1 ");
		if(StringUtil.isNotEmpty(news.getName())){
			sb.append(" and t.name like '%"+news.getName()+"%' ");
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString());
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()){
			return rs.getInt("total");
		}else{
			return 0;
		}
	}
	
	public int newsAdd(Connection con,News news) throws Exception{
		
		String sql="insert into t_news values(null,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, news.getName());
		pstmt.setString(2, news.getText());
		pstmt.setDate(3,new java.sql.Date(news.getCreatetime().getTime()));
		int executeUpdate = pstmt.executeUpdate();
		return executeUpdate;
	}
	
	public int newDelete(Connection con,String ids)throws Exception{
		String sql="delete from t_news where id in ("+ids+")";
		PreparedStatement pstmt=con.prepareStatement(sql);
		return pstmt.executeUpdate();
	}
	
	public News showNew(Connection con,String ids)throws Exception{
		String sql="select * from t_news where id = ("+ids+")";
		PreparedStatement pstmt=con.prepareStatement(sql);
		ResultSet resultSet = pstmt.executeQuery();
		News news=new News();
		while(resultSet.next()){
			String text = resultSet.getString("text");
			int id = resultSet.getInt("id");
			String name = resultSet.getString("name");
			Date date = resultSet.getDate("createtime");
			news.setCreatetime(date);
			news.setName(name);
			news.setText(text);
			news.setId(id);
		}
		return news;
	}
	public int newsEdit(Connection con,News news) throws Exception{
		String sql="update t_news set name=?,text=? where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, news.getName());
		pstmt.setString(2, news.getText());
		pstmt.setInt(3, news.getId());
		return pstmt.executeUpdate();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值