package com.renjie.dao;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.renjie.db.DatabaseUtil;
import com.renjie.entity.NewsEntity;
import com.renjie.util.CreateNewsFileUtil;
public class NewsDao {
/**
* 显示所有的新闻
* @return
*/
public List<NewsEntity> Query(){
String sql="select * from news";
List<NewsEntity> list=this.newsQuery(sql);
return list;
}
/**
* 分页查询方法
* @param type
* @param pageSize
* @param currPage
* @return
*/
public List<NewsEntity> QueryByType(String type,int pageSize,int currPage){
String sql="select top %d * from news where nType=? and " +
" nId not in (select top %d nId from news where nType=? order by nId desc)" +
" order by nId desc";
List<NewsEntity> list=this.queryByPage(sql,pageSize,currPage,type,type);
return list;
}
/**
* 根据类型查询所有的行数
* @param type
* @return
*/
public int querySunRows(String type){
String sql="select count(*) from news where nType=?";
int rows=0;
DatabaseUtil dbUtil=new DatabaseUtil();
ResultSet rs=dbUtil.excutQueryByPage(sql,null,null,type);
try {
while(rs.next()){
rows=rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rows;
}
/**
* 查询所有的页数
* @param total
* @param pageSize
* @return
*/
public int querySumPage(int total,int pageSize){
DatabaseUtil dbUtil=new DatabaseUtil();
int pages=0;
pages=dbUtil.totalPage(total, pageSize);
return pages;
}
/**
* 高级分页
* @param sql
* @param objects
* @return
*/
public List<NewsEntity> queryByPage(String sql,Integer pageSize,Integer currPage,Object...objects){
DatabaseUtil dbUtil=new DatabaseUtil();
ResultSet rs=dbUtil.excutQueryByPage(sql, pageSize, currPage, objects);
List<NewsEntity> list=new ArrayList<NewsEntity>();
try {
while(rs.next()){
NewsEntity us=new NewsEntity(rs.getInt("nId"),
rs.getString("nType"),rs.getString("nTitle"),
rs.getString("nContent"),rs.getString("nPublishTime")
);
list.add(us);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 普通查询
* @param sql
* @param objects
* @return
*/
public List<NewsEntity> newsQuery(String sql,Object...objects){
DatabaseUtil dbUtil=new DatabaseUtil();
ResultSet rs=dbUtil.ExecuteQuery(sql, objects);
List<NewsEntity> list=new ArrayList<NewsEntity>();
try {
while(rs.next()){
NewsEntity us=new NewsEntity(rs.getInt("nId"),
rs.getString("nType"),rs.getString("nTitle"),
rs.getString("nContent"),rs.getString("nPublishTime")
);
list.add(us);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 添加新闻
* @param news
* @return
*/
public boolean addNews(NewsEntity news){
String sql="insert into news values(?,?,?,?)";
DatabaseUtil dbUtil=new DatabaseUtil();
int result=-1;
result=dbUtil.executeUpdate(sql,news.getNType(),
news.getNTitle(),news.getNContent(),
new Date().toLocaleString());
if(result>0){
return true;
}else{
return false;
}
}
/**
* 查询插入的id号
* @param title
* @return
*/
public int getInsertId(){
int id=-1;
String sql="select top 1 * from news order by nId desc";
List<NewsEntity> list=this.newsQuery(sql);
id=list.get(0).getNId();
if(list.size()>0){
return id;
}else{
return id;
}
}
/**
* 创建新闻文件
* @param path
* @param fileName
* @param content
* @return
*/
public boolean writeNews(String path,String fileName,String content){
CreateNewsFileUtil newFile=new CreateNewsFileUtil();
try {
if(newFile.createFile(path,fileName)){
newFile.writeFile(path, fileName,content,false);
return true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* 根据id查询新闻
* @param id
* @return
*/
public NewsEntity queryById(int id){
String sql="select * from news where nId=?";
List<NewsEntity> list=this.newsQuery(sql,id);
NewsEntity news=null;
if(list.size()>0){
news=list.get(0);
}
return news;
}
/**
* 查询今日最新十条新闻
* @return
*/
public List<NewsEntity> queryTopTenNews(){
String sql="select top 7 * from news order by nId desc";
List<NewsEntity> list=this.newsQuery(sql);
return list;
}
/**
* 查询新闻
* @param type
* @param pageSize
* @param currPage
* @param addSql
* @return
*/
public List<NewsEntity> search(int pageSize,int currPage,String searchType,String searchWord){
String sql="select top {0} * from news where " +
" nId not in (select top {1} nId from news where "
+searchType+" like ''%"+searchWord+"%''"+
" order by nId desc) and " +
searchType+" like ''%"+searchWord+"%'' order by nId desc";
List<NewsEntity> list=this.queryByPage1(sql,pageSize,currPage);
return list;
}
/**
* 用messageformart格式化字符的分页查询
* @param sql
* @param pageSize
* @param currPage
* @param objects
* @return
*/
public List<NewsEntity> queryByPage1(String sql,Integer pageSize,Integer currPage,Object...objects){
DatabaseUtil dbUtil=new DatabaseUtil();
ResultSet rs=dbUtil.excutQueryByPage1(sql, pageSize, currPage, objects);
List<NewsEntity> list=new ArrayList<NewsEntity>();
try {
while(rs.next()){
NewsEntity us=new NewsEntity(rs.getInt("nId"),
rs.getString("nType"),rs.getString("nTitle"),
rs.getString("nContent"),rs.getString("nPublishTime")
);
list.add(us);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 查询总共有多少行
* @param searchType
* @param searchWord
* @return
*/
public int searchTotalRows(String searchType,String searchWord){
String sql="select count(*) from news where "+searchType +" like '%"+searchWord+"%'";
int rows=0;
DatabaseUtil dbUtil=new DatabaseUtil();
ResultSet rs=dbUtil.excutQueryByPage(sql,null,null);
try {
while(rs.next()){
rows=rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rows;
}
/**
* 根据Id删除新闻
* @param id
* @return
*/
public boolean deleteNewsById(int id){
DatabaseUtil dbUtil=new DatabaseUtil();
String sql="delete from news where nId=?";
return dbUtil.executeUpdate(sql,id)>0;
}
}