📄 newsdao.java
字号:
package edu.liusong.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import edu.liusong.common.DBConnect;
import edu.liusong.mo.News;
import edu.liusong.mo.User;
public class NewsDAO {
private DBConnect conn;
private ResultSet rs;
/**
* 保存通知
*
* @param news
* @return
*/
public int save(News news) {
int i = 0;
try {
conn = new DBConnect();
String sql="insert into news values(?,?,?,?)";
String []values={String.valueOf(news.getNid()),news.getTitle(),news.getContent(),news.getAtime().toString()};
i=conn.executeUpdate(sql, values);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return i;
}
/**
* 通过nid获得通知
*
* @param id
* @return
*/
public News getById(long id) {
News news = null;
try {
conn = new DBConnect();
String sql = "select * from news where nid=" + id;
rs = conn.executeQuery(sql, null);
if (rs.next()) {
news = new News();
news.setNid(rs.getLong(1));
news.setTitle(rs.getString(2));
news.setContent(rs.getString(3));
news.setAtime(rs.getTimestamp(4));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return news;
}
/**
* 返回最新的八条通知
*
* @return list
*/
public List getTopNews() {
List<News> list = new ArrayList<News>();
try {
conn = new DBConnect();
String sql = "select * from news order by atime desc limit 0,8";
rs = conn.executeQuery(sql, null);
while (rs.next()) {
News news = new News();
news.setNid(rs.getLong(1));
news.setTitle(rs.getString(2));
news.setContent(rs.getString(3));
news.setAtime(rs.getTimestamp(4));
list.add(news);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
/**
* 分页查询
* @param pageSize
* @param topage
* @return
*/
public List getByPage(int pageSize,int topage){
List<News> list=new ArrayList<News>();
int recordcount;
int pagecount;
try {
conn=new DBConnect();
String sql="select * from news order by atime desc ";
rs=conn.executeQuery(sql, null);
rs.last();
recordcount=rs.getRow();
pagecount=(recordcount+pageSize-1)/pageSize;
if(topage>pagecount)
topage=pagecount;
rs.absolute((topage-1)*pageSize+1);
int j=0;
while((j<pageSize)&&(!rs.isAfterLast())){
News news=new News();
news.setNid(rs.getLong(1));
news.setTitle(rs.getString(2));
news.setContent(rs.getString(3));
news.setAtime(rs.getTimestamp(4));
list.add(news);
rs.next();
j++;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
/**
* 根据nid删除记录
* @param id
*/
public void deleteById(String id){
try {
conn=new DBConnect();
String sql="delete from news where nid=?";
String[] values={id};
conn.executeUpdate(sql, values);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -