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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
jsp项目s2结业项目
共658个文件
gif:216个
jpg:131个
bmp:129个
需积分: 10 41 下载量 78 浏览量
2009-06-13
21:37:16
上传
评论
收藏 12.74MB RAR 举报
温馨提示
Javascript jsp Java SQLServer2005
资源推荐
资源详情
资源评论
收起资源包目录
jsp项目s2结业项目 (658个子文件)
newsType.bmp 151KB
110.bmp 6KB
112.bmp 6KB
106.bmp 6KB
108.bmp 6KB
107.bmp 6KB
111.bmp 6KB
109.bmp 6KB
35.bmp 5KB
38.bmp 5KB
56.bmp 5KB
63.bmp 5KB
8.bmp 5KB
65.bmp 5KB
81.bmp 5KB
19.bmp 5KB
114.bmp 5KB
11.bmp 5KB
16.bmp 5KB
85.bmp 5KB
82.bmp 5KB
31.bmp 5KB
99.bmp 5KB
44.bmp 5KB
69.bmp 5KB
3.bmp 5KB
75.bmp 5KB
41.bmp 5KB
2.bmp 5KB
100.bmp 5KB
22.bmp 5KB
9.bmp 5KB
30.bmp 5KB
79.bmp 5KB
25.bmp 5KB
97.bmp 5KB
93.bmp 5KB
60.bmp 5KB
89.bmp 5KB
98.bmp 5KB
57.bmp 5KB
12.bmp 5KB
78.bmp 5KB
15.bmp 5KB
26.bmp 5KB
37.bmp 5KB
86.bmp 5KB
14.bmp 5KB
46.bmp 5KB
10.bmp 5KB
29.bmp 5KB
73.bmp 5KB
choose.bmp 5KB
52.bmp 5KB
50.bmp 5KB
23.bmp 5KB
6.bmp 5KB
20.bmp 5KB
27.bmp 5KB
45.bmp 5KB
96.bmp 5KB
4.bmp 5KB
76.bmp 5KB
49.bmp 5KB
115.bmp 5KB
87.bmp 5KB
36.bmp 5KB
7.bmp 5KB
58.bmp 5KB
53.bmp 5KB
55.bmp 5KB
95.bmp 5KB
47.bmp 5KB
21.bmp 5KB
59.bmp 5KB
67.bmp 5KB
54.bmp 5KB
83.bmp 5KB
66.bmp 5KB
72.bmp 5KB
77.bmp 5KB
17.bmp 5KB
28.bmp 5KB
18.bmp 5KB
91.bmp 5KB
5.bmp 5KB
40.bmp 5KB
71.bmp 5KB
51.bmp 5KB
116.bmp 5KB
88.bmp 5KB
1.bmp 5KB
48.bmp 5KB
24.bmp 5KB
68.bmp 5KB
32.bmp 5KB
84.bmp 5KB
61.bmp 5KB
43.bmp 5KB
92.bmp 5KB
共 658 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
xianew
- 粉丝: 19
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功