上一章 下一章
上一个任务/上一节 下一个任务/下一节
NewsDao
package org.news.dao;
import java.sql.SQLException;
import java.util.List;
import org.news.entity.News;
public interface NewsDao {
// 获取所有新闻
public List<News> getAllnews() throws SQLException;
// 获取某主题下的所有新闻(根据主题id)
@Deprecated
public List<News> getAllnewsByTID(int Tid) throws SQLException;
// 获取某主题下的所有新闻(根据主题名称)
public List<News> getAllnewsByTname(String tname) throws SQLException;
// 获取某主题下的新闻数量
public int getNewsCountByTID(int Tid) throws SQLException;
// 获取某主题下的最新新闻
public List<News> getLatestNewsByTID(int tid, int limit) throws SQLException;
//获取某条新闻
public News getNewsByNID(int nid) throws SQLException;
// 获得新闻总数
public int getTotalCount(Integer tid) throws SQLException;
// 分页获得新闻
public List<News> getPageNewsList(Integer tid, int pageNo, int pageSize) throws SQLException;
}
实现类 NewsDaoImpl
package org.news.dao.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.news.dao.BaseDao;
import org.news.dao.NewsDao;
import org.news.entity.News;
import org.news.util.DatabaseUtil;
public class NewsDaoImpl extends BaseDao implements NewsDao {
public NewsDaoImpl(Connection conn) {
super(conn);
}
// 获取所有新闻
public List<News> getAllnews() throws SQLException {
List<News> list = new ArrayList<News>();
ResultSet rs = null;
String sql = "SELECT `nid`, `ntid`, `ntitle`, `nauthor`,"
+ " `ncreateDate`, `nsummary`, `tname` FROM `NEWS`, `TOPIC`"
+ " WHERE `NEWS`.`ntid` = `TOPIC`.`tid`"
+ " ORDER BY `ncreateDate` DESC";
try {
rs = this.executeQuery(sql);
News news = null;
while (rs.next()) {
news = new News();
news.setNid(rs.getInt("nid"));
news.setNtid(rs.getInt("ntid"));
news.setNtitle(rs.getString("ntitle"));
news.setNauthor(rs.getString("nauthor"));
news.setNcreatedate(rs.getTimestamp("ncreateDate"));
news.setNsummary(rs.getString("nsummary"));
news.setNtname(rs.getString("tname"));
list.add(news);
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
DatabaseUtil.closeAll(null, null, rs);
}
return list;
}
// 获取某主题下的所有新闻
public List<News> getAllnewsByTID(int tid) throws SQLException {
List<News> list = new ArrayList<News>();
ResultSet rs = null;
String sql = "SELECT `nid`, `ntid`, `ntitle`, `nauthor`,"
+ " `ncreateDate`, `nsummary`, `tname` FROM `NEWS`, `TOPIC`"
+ " WHERE `NEWS`.`ntid` = `TOPIC`.`tid` AND `NEWS`.`ntid` = ?"
+ " ORDER BY `ncreateDate` DESC";
try {
rs = this.executeQuery(sql, tid);
News news = null;
while (rs.next()) {
news = new News();
news.setNid(rs.getInt("nid"));
news.setNtid(rs.getInt("ntid"));
news.setNtitle(rs.getString("ntitle"));
news.setNauthor(rs.getString("nauthor"));
news.setNcreatedate(rs.getTimestamp("ncreateDate"));
news.setNsummary(rs.getString("nsummary"));
news.setNtname(rs.getString("tname"));
list.add(news);
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
DatabaseUtil.closeAll(null, null, rs);
}
return list;
}
// 获取某主题下的最新新闻
public List<News> getLatestNewsByTID(int tid, int limit) throws SQLException {
List<News> list = new ArrayList<News>();
ResultSet rs = null;
String sql = "SELECT `nid`, `ntid`, `ntitle` FROM `NEWS` WHERE"
+ " `ntid` = ? ORDER BY `ncreatedate` DESC LIMIT ?";
try {
rs = this.executeQuery(sql, tid, limit);
News news = null;
while (rs.next()) {
news = new News();
news.setNid(rs.getInt("nid"));
news.setNtid(rs.getInt("ntid"));
news.setNtitle(rs.getString("ntitle"));
list.add(news);
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
DatabaseUtil.closeAll(null, null, rs);
}
return list;
}
// 获取某主题下的新闻数量
public int getNewsCountByTID(int tid) throws SQLException {
ResultSet rs = null;
String sql = "SELECT COUNT(`ntid`) FROM `news` WHERE `ntid` = ?";
int count = -1;
try {
rs = this.executeQuery(sql, tid);
rs.next();
count = rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
DatabaseUtil.closeAll(null, null, rs);
}
return count;
}
// 获取某条新闻
public News getNewsByNID(int nid) throws SQLException {
ResultSet rs = null;
String sql = "SELECT * FROM `NEWS`, `TOPIC`"
+ " WHERE `NEWS`.`ntid` = `TOPIC`.`tid` AND `NEWS`.`nid` = ?"
+ " ORDER BY `ncreateDate` DESC";
News news = null;
try {
rs = this.executeQuery(sql, nid);
if (rs.next()) {
news = new News();
news.setNid(rs.getInt("nid"));
news.setNtid(rs.getInt("ntid"));
news.setNtitle(rs.getString("ntitle"));
news.setNauthor(rs.getString("nauthor"));
news.setNcreatedate(rs.getTimestamp("ncreateDate"));
news.setNpicpath(rs.getString("npicPath"));