package com.lisi.news.logic;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.lisi.news.comm.DataSource;
import com.lisi.news.data.NewsDataBean;
//业务逻辑类
//这个类的任务是分页,并把分页后每页的所有记录放到列表中返回
//本类采用假分页算法
public class NewsLogicBean {
/**
* @param args
*/
private int allRows;//总行数
private int curPage=1;//当前页
private int rowPerPage=5;//每页多少行
private int allPages; //总页数
public int getAllRows() {
return allRows;
}
public void setAllRows(int allRows) {
this.allRows = allRows;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getRowPerPage() {
return rowPerPage;
}
public void setRowPerPage(int rowPerPage) {
this.rowPerPage = rowPerPage;
}
public int getAllPages() {
return allPages;
}
public void setAllPages(int allPages) {
this.allPages = allPages;
}
public List findAll()
{
// List列表、Set集合 为单值
// Map键值对 为双值
List list= new ArrayList();
Statement st=null;
ResultSet rs=null;
try {
st=DataSource.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//该行进行 try catch 并在其下加入 结果集可进行滚动 游标只读
rs=st.executeQuery(" select newsid,newstitle,newscontent,newscount,newsdate from news order by newsdate desc");
rs.last(); //定位到最后一行
this.allRows=rs.getRow();//总共多少行
if(this.allRows%this.rowPerPage==0){
this.allPages=this.allRows/this.rowPerPage;
}else{
this.allPages=this.allRows/this.rowPerPage+1;
}
if(this.curPage==1){
rs.beforeFirst();
}else{
rs.absolute((this.curPage-1)*this.rowPerPage);
}
int i=0;
while(rs.next()&&i<this.rowPerPage){
NewsDataBean bean=new NewsDataBean();
bean.setNewsid(rs.getString(1));
bean.setNewstitle(rs.getString(2));
bean.setNewscontent(rs.getString(3));
bean.setNewscount(rs.getString(4));
bean.setNewsdate(rs.getString(5));
list.add(bean);
i++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;//list.size()可能为5,也可能为0
}
public int insertNews(String newsTitle,String newsContent){
return modify("insert into news(newstitle,newscontent,newscount,newsdate) values('"+newsTitle+"','"+newsContent+"',0,now())");
}
public NewsDataBean findById(String newsid){
NewsDataBean bean=new NewsDataBean();
Statement st=null;
ResultSet rs=null;
try {
st=DataSource.getConnection().createStatement();
rs=st.executeQuery("select newsid,newstitle,newscontent,newscount,newsdate from news where newsid="+newsid);
if(rs.next()){
bean.setNewsid(rs.getString(1));
bean.setNewstitle(rs.getString(2));
bean.setNewscontent(rs.getString(3));
bean.setNewscount(rs.getString(4));
bean.setNewsdate(rs.getString(5));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bean;
}
public int modify(String sql){
int result=0;
Statement st=null;
try {
st=DataSource.getConnection().createStatement();
result=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public int updateNews(String newsid,String newsTitle,String newsContent){
return modify("update news set newstitle='"+newsTitle+"',newscontent='"+newsContent+"' where newsid="+newsid);
}
public int deleteId(String newsid){
return modify("delete from news where newsid="+newsid);
}
public NewsDataBean detail(String newsid){
NewsDataBean bean=new NewsDataBean();
Statement st=null;
ResultSet rs=null;
try {
st=DataSource.getConnection().createStatement();
st.executeUpdate("update news set newscount=newscount+1 where newsid="+newsid);
rs=st.executeQuery("select newsid,newstitle,newscontent,newscount,newsdate from news where newsid="+newsid);
if(rs.next()){
bean.setNewsid(rs.getString(1));
bean.setNewstitle(rs.getString(2));
bean.setNewscontent(rs.getString(3));
bean.setNewscount(rs.getString(4));
bean.setNewsdate(rs.getString(5));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bean;
}
// public static void main(String[] args) {
// // TODO Auto-generated method stub
//
// List list=new NewsLogicBean().findAll();
// System.out.println(list.size());
// }
}