STRUTS2 Operating Database

Source: Internet
Author: User

Struts2 operation database is a difficult point for beginners is also a focus, and now I explain to you struts2 operation database, using STRUTS2 to the database to increase, delete, change, check and paging query, see the following code:

User class

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;

Database connection to write their own, and then the action in the configuration can be run, if you need source code can be downloaded to the resources.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.