---
title: "struts框架访问数据库"
author: "cakin24"
date: "2017-10-27T19:13:01Z"
tags: []
categories: ["编程语言"]
slug: "struts"
draft: false
lastmod: "2017-10-27T19:13:01Z"
id: 2397859
url: "https://cakin24.iteye.com/blog/2397859"
---

一 在MySQL数据库中增加一张用户表

 

二 创建domain对象

package com.cakin.domain;

 

public class Users {

        private int user\_id;

        private String user\_pwd;

        private String user\_name;

        private String user\_email;

        public int getUser\_id() {

                return user\_id;

        }

        public void setUser\_id(int user\_id) {

                this.user\_id = user\_id;

        }

        public String getUser\_pwd() {

                return user\_pwd;

        }

        public void setUser\_pwd(String user\_pwd) {

                this.user\_pwd = user\_pwd;

        }

        public String getUser\_name() {

                return user\_name;

        }

        public void setUser\_name(String user\_name) {

                this.user\_name = user\_name;

        }

        public String getUser\_email() {

                return user\_email;

        }

        public void setUser\_email(String user\_email) {

                this.user\_email = user\_email;

        }

        

}

 

三 创建service对象

package com.cakin.service;

 

import java.util.ArrayList;

import com.cakin.domain.Users;

 

public class UsersService {

        public boolean checkUser(Users user)

        {

                String sql = "select * from users where user\_name =? and user\_pwd=? ";

                SqlHelper sqlHelper= new SqlHelper();

                String \[] parameters={user.getUser\_name(),user.getUser\_pwd()};

                ArrayList al=sqlHelper.executeQuery(sql,parameters);

                if(al.size()==1)

                {

                        return true;

                }

                else

                {

                        return false;

                }       

        }

}

 

四 action验证用户

package com.cakin.actions;

//这是一个action（表示小队长，需要继承Action）

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import com.cakin.domain.Users;

import com.cakin.forms.UserForm;

import com.cakin.service.UsersService;

public class LoginAction extends Action {

    private int num=0;

    //我们需要重新编写一个方法：execute会被自动调用，有点类似servlet的service方法。

    @Override

    public ActionForward execute(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response)

            throws Exception {

            //把form转成对应的UserForm对象

        UserForm userForm=(UserForm)form;

        

        //使用service（士兵到数据库去验证）

        UsersService usersService =new UsersService();

        Users user = new Users();

        user.setUser\_name(userForm.getUsername());

        user.setUser\_pwd(userForm.getPassword());

        if(usersService.checkUser(user))

        {

            return mapping.findForward("ok");

        }

        else

        {

            return mapping.findForward("err");

        }

    }

}