你的要求还真多
这是我最近做设计写的 呵呵 仅供参考
方法有了 自己调用一下就OK啦!
其实方法的实现都一样 就是SQL语句不一样而已哦
用hibernate来持久化对象 不用写这么多重复代码了!
//查询所有
public List
List
Connection conn = null;
conn = DB.getConn();
PreparedStatement pstmt = null;
String sql = "select * from tb_news order by news_edittime desc";
pstmt = DB.getPStmt(conn, sql);
ResultSet rs = null;
try {
News news = null;
rs = pstmt.executeQuery();
while (rs.next()) {
news = new News();
news.setId(rs.getInt("news_id"));
news.setTitle(rs.getString("news_title"));
news.setContent(rs.getString("news_content"));
news.setAuthor(rs.getString("news_author"));
news.setAddtime(rs.getDate("news_addtime"));
news.setEdittime(rs.getDate("news_edittime"));
news.setHits(rs.getInt("news_hits"));
list.add(news);
// System.out.print(news.getContent());
// System.out.println();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeResultSet(rs);
DB.closePStmt(pstmt);
DB.closeConn(conn);
}
return list;
}
//分页查询
public List
List
Connection conn = null;
conn = DB.getConn();
PreparedStatement pstmt = null;
String sql = "select * from tb_news order by news_edittime desc limit "+pageSize*(pageNo-1)+","+pageSize;
// System.out.println(sql);
pstmt = DB.getPStmt(conn, sql);
ResultSet rs = null;
News news = null;
try {
rs = pstmt.executeQuery();
while (rs.next()) {
news = new News();
news.setId(rs.getInt("news_id"));
news.setTitle(rs.getString("news_title"));
news.setContent(rs.getString("news_content"));
news.setAuthor(rs.getString("news_author"));
news.setAddtime(rs.getDate("news_addtime"));
news.setEdittime(rs.getDate("news_edittime"));
news.setHits(rs.getInt("news_hits"));
list.add(news);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeResultSet(rs);
DB.closePStmt(pstmt);
DB.closeConn(conn);
}
return list;
}
//查询最新六条记录
public List
List
Connection conn = null;
conn = DB.getConn();
PreparedStatement pstmt = null;
String sql = "select * from tb_news order by news_edittime desc limit 6";
// System.out.println(sql);
pstmt = DB.getPStmt(conn, sql);
ResultSet rs = null;
News news = null;
try {
rs = pstmt.executeQuery();
while (rs.next()) {
news = new News();
news.setId(rs.getInt("news_id"));
news.setTitle(rs.getString("news_title"));
news.setContent(rs.getString("news_content"));
news.setAuthor(rs.getString("news_author"));
news.setAddtime(rs.getDate("news_addtime"));
news.setEdittime(rs.getDate("news_edittime"));
news.setHits(rs.getInt("news_hits"));
list.add(news);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeResultSet(rs);
DB.closePStmt(pstmt);
DB.closeConn(conn);
}
return list;
}
//查询点击率前十个
public List
List
Connection conn = null;
conn = DB.getConn();
PreparedStatement pstmt = null;
String sql = "select * from tb_news order by news_hits desc limit 10";
// System.out.println(sql);
pstmt = DB.getPStmt(conn, sql);
ResultSet rs = null;
News news = null;
try {
rs = pstmt.executeQuery();
while (rs.next()) {
news = new News();
news.setId(rs.getInt("news_id"));
news.setTitle(rs.getString("news_title"));
news.setContent(rs.getString("news_content"));
news.setAuthor(rs.getString("news_author"));
news.setAddtime(rs.getDate("news_addtime"));
news.setEdittime(rs.getDate("news_edittime"));
news.setHits(rs.getInt("news_hits"));
list.add(news);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeResultSet(rs);
DB.closePStmt(pstmt);
DB.closeConn(conn);
}
return list;
}
//查询一条记录
public News getNewsByID(int id){
Connection conn = null;
conn = DB.getConn();
PreparedStatement pstmt = null;
String sql = "select * from tb_news where news_id = " + id;
// System.out.println(sql);
pstmt = DB.getPStmt(conn, sql);
ResultSet rs = null;
News news = null;
try {
rs = pstmt.executeQuery();
if(rs.next()) {
news = new News();
news.setId(rs.getInt("news_id"));
news.setTitle(rs.getString("news_title"));
news.setContent(rs.getString("news_content"));
news.setAuthor(rs.getString("news_author"));
news.setAddtime(rs.getDate("news_addtime"));
news.setEdittime(rs.getDate("news_edittime"));
news.setHits(rs.getInt("news_hits"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeResultSet(rs);
DB.closePStmt(pstmt);
DB.closeConn(conn);
}
return news;
}
//更新点击率
public void updateNewsHits(String idString) {
// TODO Auto-generated method stub
Connection conn = DB.getConn();
String sql = "update tb_news set news_hits =news_hits+1 where news_id = '" + idString + "'";
Statement stmt = DB.getStmt(conn);
try {
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally{
DB.closeStmt(stmt);
DB.closeConn(conn);
}
}
你的这个问题也太苛刻了点吧,类似的都不行,还得现写啊。。。。