jsp页面操作
后台impl定义了增删改查4个方法我想在jsp页面中用最精简的代码首先实现表格查询就是一进入页面就能查询出数据库表中的所有数据已SqlServer2005为主然后点击某一...
后台impl定义了增删改查 4个方法
我想在jsp页面中用最精简的代码首先实现表格查询
就是一进入页面就能查询出数据库表中的所有数据已SqlServer2005为主
然后点击某一数据(一个超链接)跳转到update的一个页面执行更改数据信息的操作
接着点击首页的A标签或者按钮实现Add的一个页面执行增加一条信息的操作
接着在Add页面写一个删除A标签执行delete的操作主要是小脚本中的逻辑关系麻烦高手帮下我写的清楚些最好代写注释定有重谢(*^__^*) 嘻嘻……
用MyEclipse环境进行编程
impl的定义方法为
public List findNews();//查询所有新闻
public int addNewsByID(News n);//增加新闻
public int updateNews(News n);//更新新闻
public int deleteNewsById(int id);根据id号删除新闻
如果查询时分页查询就用
public List findNews(int page,int pageSize);
可以不用News您用什么都行主要是小脚本这里的逻辑不太明白 看了几本书没有介绍详细的我想看个案例
彻底从头到尾解析一遍就该差不多了!
其实就是个Template Model 展开
我想在jsp页面中用最精简的代码首先实现表格查询
就是一进入页面就能查询出数据库表中的所有数据已SqlServer2005为主
然后点击某一数据(一个超链接)跳转到update的一个页面执行更改数据信息的操作
接着点击首页的A标签或者按钮实现Add的一个页面执行增加一条信息的操作
接着在Add页面写一个删除A标签执行delete的操作主要是小脚本中的逻辑关系麻烦高手帮下我写的清楚些最好代写注释定有重谢(*^__^*) 嘻嘻……
用MyEclipse环境进行编程
impl的定义方法为
public List findNews();//查询所有新闻
public int addNewsByID(News n);//增加新闻
public int updateNews(News n);//更新新闻
public int deleteNewsById(int id);根据id号删除新闻
如果查询时分页查询就用
public List findNews(int page,int pageSize);
可以不用News您用什么都行主要是小脚本这里的逻辑不太明白 看了几本书没有介绍详细的我想看个案例
彻底从头到尾解析一遍就该差不多了!
其实就是个Template Model 展开
展开全部
你的要求还真多
这是我最近做设计写的 呵呵 仅供参考
方法有了 自己调用一下就OK啦!
其实方法的实现都一样 就是SQL语句不一样而已哦
用hibernate来持久化对象 不用写这么多重复代码了!
//查询所有
public List<News> getNews(){
List<News> list = new ArrayList<News>();
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<News> getNews(int pageNo,int pageSize){
List<News> list = new ArrayList<News>();
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<News> getTop6News(){
List<News> list = new ArrayList<News>();
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<News> getHot10News(){
List<News> list = new ArrayList<News>();
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);
}
}
这是我最近做设计写的 呵呵 仅供参考
方法有了 自己调用一下就OK啦!
其实方法的实现都一样 就是SQL语句不一样而已哦
用hibernate来持久化对象 不用写这么多重复代码了!
//查询所有
public List<News> getNews(){
List<News> list = new ArrayList<News>();
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<News> getNews(int pageNo,int pageSize){
List<News> list = new ArrayList<News>();
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<News> getTop6News(){
List<News> list = new ArrayList<News>();
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<News> getHot10News(){
List<News> list = new ArrayList<News>();
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询