前臺開發------開發添加用戶數據庫的方法

建立用戶的表和數據模型類

           當即購買這個模塊須要事先有用戶才能實現,所以咱們須要建立用戶表:usersjavascript

: usercss

列名html

類型java

說明jquery

備註sql

id數據庫

intapache

idbootstrap

主鍵jsp

username

varchar

用戶名

 

email

varchar

郵箱

 

password

varchar

密碼

 

name

varchar

收貨人

 

phone

varchar

收貨電話

 

address

varchar

收貨地址

 

isadmin

bit

是不是管理員

 

isvalidate

bit

郵箱是否驗證

 

 

在MySQL中建立該表:user

 

在model層建立用戶的數據模型類:

User.java

package com.guiyan.model;

public class User {
    private String username;
    private String email;
    private String password;
    private String name;
    private String phone;
    private String address;
    private boolean isadmin;
    private boolean isvalidate;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public boolean isIsadmin() {
        return isadmin;
    }
    public void setIsadmin(boolean isadmin) {
        this.isadmin = isadmin;
    }
    public boolean isIsvalidate() {
        return isvalidate;
    }
    public void setIsvalidate(boolean isvalidate) {
        this.isvalidate = isvalidate;
    }
    public User(String username, String email, String password, String name, String phone, String address,
            boolean isadmin, boolean isvalidate) {
        super();
        this.username = username;
        this.email = email;
        this.password = password;
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.isadmin = isadmin;
        this.isvalidate = isvalidate;
    }
    public User() {
        super();
    }
    
    
    
    
    

}

 

開發註冊頁面,建立處理註冊的Servlet

註冊頁面:

register.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 4 
 5 <!DOCTYPE html>
 6 <html>
 7 <head>
 8     <title>用戶註冊</title>
 9     <meta name="viewport" content="width=device-width, initial-scale=1">
10     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
11     <link type="text/css" rel="stylesheet" href="css/bootstrap.css">
12     <link type="text/css" rel="stylesheet" href="css/style.css">
13     <script type="text/javascript" src="js/jquery.min.js"></script>
14     <script type="text/javascript" src="js/bootstrap.min.js"></script>
15     <script type="text/javascript" src="js/simpleCart.min.js"></script>
16 </head>
17 <body>
18 
19     
20 
21 
22 
23 
24 
25     <!--header-->
26     <jsp:include page="/header.jsp"></jsp:include>
27     
28     <!--account-->
29     <div class="account">
30         <div class="container">
31             <div class="register">
32                 
33                 <form action="${pageContext.request.contextPath }/user_register" method="post"> 
34                     <div class="register-top-grid">
35                         <h3>註冊新用戶</h3>
36                         <div class="input">
37                             <span>用戶名 <label style="color:red;">*</label></span>
38                             <input type="text" name="username" placeholder="請輸入用戶名" required="required"> 
39                         </div>
40                         <div class="input">
41                             <span>郵箱 <label style="color:red;">*</label></span>
42                             <input type="text" name="email" placeholder="請輸入郵箱" required="required"> 
43                         </div>
44                         <div class="input">
45                             <span>密碼 <label style="color:red;">*</label></span>
46                             <input type="text" name="password" placeholder="請輸入密碼" required="required"> 
47                         </div>
48                         <div class="input">
49                             <span>收貨人<label></label></span>
50                             <input type="text" name="name" placeholder="請輸入收貨"> 
51                         </div>
52                         <div class="input">
53                             <span>收貨電話<label></label></span>
54                             <input type="text" name="phone" placeholder="請輸入收貨電話"> 
55                         </div>
56                         <div class="input">
57                             <span>收貨地址<label></label></span>
58                             <input type="text" name="address" placeholder="請輸入收貨地址"> 
59                         </div>
60                         <div class="clearfix"> </div>
61                     </div>
62                     <div class="register-but text-center">
63                        <input type="submit" value="提交">
64                        <div class="clearfix"> </div>
65                     </div>
66                 </form>
67                 <div class="clearfix"> </div>
68             </div>
69         </div>
70     </div>
71     <!--//account-->
72 
73     
74 
75 
76 
77 
78     <!--footer-->
79     <jsp:include page="/footer.jsp"></jsp:include>
80     <!--//footer-->
81 
82     
83 </body>
84 </html>
register.jsp

 

測試看是否能獲取到數據,測試結果以下:

 

從圖中能夠看出已經得到了表單中的內容。

測試得該結果的代碼:

 UserRegisterServlet.java

@WebServlet("/user_register")
public class UserRegisterServlet extends HttpServlet {
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //參數的取得
    User user=new User();
        
        try {
            BeanUtils.copyProperties(user, request.getParameterMap());
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        System.out.println(user);
    }
    

}

 

在dao層建立一個UserDao.java,裏面進行書寫查詢的語句,用來校驗用戶名是否存在以及校驗郵箱是否激活。

package com.guiyan.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.guiyan.model.User;
import com.guiyan.utils.DBUtil;

public class UserDao {
    
        public void addUser(User user) throws SQLException {
            QueryRunner r = new QueryRunner(DBUtil.getDataSource());
            String sql = "insert into user(username,email,password,name,phone,address,isadmin,isvalidate) values(?,?,?,?,?,?,?,?)";
            r.update(sql,user.getUsername(),user.getEmail(),user.getPassword(),user.getName(),user.getPhone(),user.getAddress(),user.isIsadmin(),user.isIsvalidate());
        }
        
        ///校驗用戶名是否存在數據庫
        public boolean isUsernameExist(String username) throws SQLException {
            QueryRunner r = new QueryRunner(DBUtil.getDataSource());
            String sql = "select * from user where username = ?";
            User u = r.query(sql, new BeanHandler<User>(User.class),username);
            if(u==null) {
                return false;
            }else {
                return true;
            }
        }
        
        
        public boolean isEmailExist(String email) throws SQLException {
            QueryRunner r = new QueryRunner(DBUtil.getDataSource());
            String sql = "select * from user where email = ?";
            User u = r.query(sql, new BeanHandler<User>(User.class),email);
            if(u==null) {
                return false;
            }else {
                return true;
            }
        }
}

 

 

在service層建立一個UserService.java,用來進行對異常的處理:

package com.guiyan.service;

import java.sql.SQLException;

import com.guiyan.dao.UserDao;
import com.guiyan.model.User;

public class UserService {
    
    private UserDao uDao=new UserDao();
    public boolean register(User user) {
        try {
            if(uDao.isUsernameExist(user.getUsername())){
                return false;
            }
            if(uDao.isEmailExist(user.getEmail())){
                return false;
            }
            uDao.addUser(user);
        
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
            return false;
        
        
    }

 

在UserRegisterServlet.java進行數據的獲取:

package com.guiyan.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.guiyan.model.User;
import com.guiyan.service.UserService;

/**
 * Servlet implementation class UserRegisterServlet
 */
@WebServlet("/user_register")
public class UserRegisterServlet extends HttpServlet {
    
    
    private UserService uService=new UserService();
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //參數的取得
    User user=new User();
        
        try {
            BeanUtils.copyProperties(user, request.getParameterMap());
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        uService.register(user);
        request.getRequestDispatcher("user_login.jsp").forward(request, response);
    }
    

}
相關文章
相關標籤/搜索