首页 > 数据库技术 > 详细

struts2操作数据库

时间:2014-06-04 13:55:42      阅读:424      评论:0      收藏:0      [点我收藏+]

struts2操作数据库是初学者的一个难点也是一个重点,现在我为大家讲解一下struts2操作数据库,使用struts2对数据库进行增、删、改、查和分页查询,请看下面的代码:bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

User类

bubuko.com,布布扣bubuko.com,布布扣

public class UserAction extends ActionSupport{
    List<User> userList;
    UserDao userDao=new UserDao();
    User user;
    User2 user2;
public User2 getUser2() {
return user2;
}








public void setUser2(User2 user2) {
this.user2 = user2;
}








public User getUser() {

return user;
}








public void setUser(User user) {
this.user = user;
}








public int getFirstRecord() {
return firstRecord;
}








public void setFirstRecord(int firstRecord) {
this.firstRecord = firstRecord;
}


private  int currentPage =1;

private int Records ;

private int  pageSize =5;

private  int pages ;

int firstRecord;



public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getRecords() {
return Records;
}
public void setRecords(int records) {
Records = records;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
    public void pages(){
    Records=userDao.getAllCount();
 
 
  pages = Records/pageSize+((Records%pageSize==0)?0:1);
  firstRecord =(currentPage-1)*pageSize;
    }
public String listAll() throws Exception {


        this.pages();
userList =userDao.doList(firstRecord, pageSize);
return SUCCESS;
}
  public String addUser(){
userDao.addUser(user);
return SUCCESS;
}
    public String deleteUser(){
    userDao.deleteUser(user);
    return SUCCESS;
    }
    public String updateUser(){
    userDao.updateUser(user);
    return SUCCESS;
    }
    public String getInfo(){   
   
    user=userDao.getInfo(user.uid);
   
    return SUCCESS;
    }

数据库连接自己写就可以了,之后再action里配置一下就可以运行了,如果需要源代码可以到http://blog.csdn.net/u014626784资源中下载。

struts2操作数据库,布布扣,bubuko.com

struts2操作数据库

原文:http://blog.csdn.net/u014626784/article/details/28253185

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!