新闻的业务处理类

package com.msit.biz;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.msit.entity.News;
import com.msit.util.SqlHelper;


/**
 * 新闻的业务处理类
 * @author Jeson
 * 
 */
public class NewsControl {
/**
* 添加 新闻方法
* @param news 新闻对象
* @return 状态 小于等于0:失败;大于等于1:成功
* @throws SQLException 
*/
public int addNew(News news) throws SQLException {
int num=0;
//1.创建 添加的sql语句
String sql="insert into news(ntid,ntitle,nauthor,ncreatedate,npicpath,ncontent,nsummary) " +
"values(?,?,?,?,?,?,?)";
//2.得到链接对象
Connection conn = SqlHelper.getConnection();
try {
//3.创建预编译执行对象
PreparedStatement pstmt = conn.prepareStatement(sql);
   //4.给预编译sql设置数据
pstmt.setString(1, news.getNtid()+"");
pstmt.setString(2, news.getNtitle());
pstmt.setString(3, news.getNauthor());
pstmt.setString(4, news.getNcreatedate());
pstmt.setString(5, news.getNpicpath());
pstmt.setString(6, news.getNcontent());
pstmt.setString(7, news.getNsummary());
//5.执行预编译语句
num=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
//6.关闭资源
conn.close();
}
return num;
}


/**
* 删除新闻方法
* @param nid 新闻主键
* @return 成功或失败状态
* @throws SQLException 
*/
public int deleteNew(int nid) throws SQLException {
int num=0;
String sql ="delete from news where nid="+nid;
Connection con = SqlHelper.getConnection();
Statement stm = con.createStatement();
num = stm.executeUpdate(sql);
return num;
}


/**
* 根据新闻id得到单个新闻
* @param nid
* @return 一条新闻
* @throws SQLException 
*/
public News getNewsByid(String nid) throws SQLException{
News n =null;
String sql="select * from news where nid="+nid;
//n = selectAll(sql).get(0);
List<News> ns = selectAll(sql);
if(null!=ns && ns.size()>=1){
n = ns.get(0);
}
return n;
}
/**
* 查询所有新闻数据
* @return 新闻集合
* @throws SQLException 
*/
public List<News> selectNews() throws SQLException {
String sql = "select * from news";
return selectAll(sql);
}


private List<News> selectAll(String sql) throws SQLException {
List<News> news =new ArrayList<News>();
Connection con = SqlHelper.getConnection();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(sql);
while (rs.next()) {
News n = new News();
n.setNid(rs.getInt("nid"));
n.setNtid(rs.getInt("ntid"));
n.setNtitle(rs.getString("ntitle"));
n.setNauthor(rs.getString("nauthor"));
n.setNcontent(rs.getString("ncontent"));
n.setNcreatedate(rs.getString("ncreatedate"));
n.setNmodifydate(rs.getString("nmodifydate"));
n.setNpicpath(rs.getString("npicpath"));
n.setNsummary(rs.getString("nsummary"));
//将新闻对象添加到集合当中
news.add(n);
}
stm.close();
rs.close();
con.close();
return news;
}


/**
* 更新新闻方法
* @param news 新闻对象
* @return 0或1
* @throws SQLException 
*/
public int updateNews(News news) throws SQLException {
int num=0;
String sql ="update news set ntid=?,ntitle=?,nauthor=?," +
"npicpath=?,ncontent=?,nmodifydate=?,nsummary=? where nid=?";
Connection con = SqlHelper.getConnection();
PreparedStatement pstm = con.prepareStatement(sql);
pstm.setString(1, news.getNtid()+"");
pstm.setString(2, news.getNtitle());
pstm.setString(3, news.getNauthor());
pstm.setString(4, news.getNpicpath());
pstm.setString(5, news.getNcontent());
pstm.setString(6, news.getNmodifydate());
pstm.setString(7, news.getNsummary());
pstm.setInt(8, news.getNid());
//执行预编译语句
num = pstm.executeUpdate();
con.close();
return num;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值