JavaWeb_(Hibernate框架)使用c3p0与Dbutils开发用户注册功能

 

 

 使用c3p0与Dbutils开发用户注册功能:

   用户在register.jsp表单成功后,页面跳转到login.html,数据库中会存放用户注册的信息  

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    <link rel="stylesheet" type="text/css" href="css/login.css" />
         <link rel="stylesheet" href="css/head.css" />
        <link rel="stylesheet" type="text/css" href="css/login.css" />
    </head>

    <body>
<div class="dvhead">
    <div class="dvlogo"><a href="index.html">你问我答</a></div>
    <div class="dvsearch">10秒钟注册账号,找到你的同学</div>
    <div class="dvreg">
        已有账号,立即&nbsp;<a href="login.html">登录</a>
    </div>
</div>
        <section class="sec">
        <form action="${pageContext.request.contextPath }/UserAction_register" method="post">
            <div class="register-box">
                <label for="username" class="username_label">
                    用 户 名
                    <input maxlength="20" name="username" type="text"
                        placeholder="您的用户名和登录名" />
                </label>
                <div class="tips">

                </div>
            </div>
            <div class="register-box">
                <label for="username" class="other_label">
                    设 置 密 码
                    <input maxlength="20" type="password" name="password"
                        placeholder="建议至少使用两种字符组合" />
                </label>
                <div class="tips">

                </div>
            </div>
            <div class="register-box">
                <label for="username" class="other_label">
                    确 认 密 码
                    <input maxlength="20" type="password" placeholder="请再次输入密码" />
                </label>
                <div class="tips">

                </div>
            </div>
            <div class="register-box">
                <label for="username" class="username_label">
                    真实姓名
                    <input maxlength="20" name="name" type="text"
                        placeholder="您的真实姓名" />
                </label>
                <div class="tips">
                </div>
            </div>
            
            <div class="register-box">
                <label for="username" class="username_label">
                    邮箱
                    <input maxlength="20" name="email" type="text"
                        placeholder="您的邮箱" />
                </label>
                <div class="tips">
                </div>
            </div>
            <div class="register-box">
                <label for="username" class="username_label">
                    手机号
                    <input maxlength="20" name="telephone" type="text"
                        placeholder="您的手机号" />
                </label>
                <div class="tips">
                </div>
            </div>
    
            
            <div class="arguement">
                <input type="checkbox" id="xieyi" />
                阅读并同意
                <a href="javascript:void(0)">《错题用户注册协议》</a>
                <a href="login.html">已有账号,立即登录</a>
                <div class="tips">
                </div>
            </div>
            <div class="submit_btn">
                <button type="submit" id="submit_btn">
                    立 即 注 册
                </button>
            </div>
        </form>
        </section>
        <script src="js/index.js" type="text/javascript" charset="utf-8"></script>
    </body>
</html>
register.jsp

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    
    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    
    <package name="forum" namespace="/" extends="struts-default">
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}">
            <result name="toLogin" type="redirect">/login.html</result>
        </action>
    </package>
    
    
</struts>
struts.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///hibernatest</property>
        <property name="user">root</property>
        <property name="password">123456</property>
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">20</property>
    </default-config>


    <named-config name="oracel">
        <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
        <property name="jdbcUrl">jdbc:oracle:thin:@//192.168.40.128/orcl</property>
        <property name="user">scott</property>
        <property name="password">scott</property>
    </named-config>


</c3p0-config>
c3p0-config.xml

 

package com.Gary.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import com.Gary.domain.User;
import com.yl.lain.utils.C3p0DataSourceUtils;

public class UserDao {

    public void addUser(User user) throws SQLException {
        System.out.println("456");
        QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource());
        String sql = "insert into user values(?,?,?,?,?,?)";
        runner.update(sql,user.getId(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getTelephone());
    }

}
UserDao.java

 

package com.Gary.domain;

public class User {

    private String id;
    private String username;
    private String password;
    private String name;
    private String email;
    private String telephone;
    
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    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 getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    
    
}
User.java

 

package com.Gary.service;

import java.sql.SQLException;

import com.Gary.dao.UserDao;
import com.Gary.domain.User;

public class UserService {

    public void addUser(User user) throws SQLException {
        UserDao userDao = new UserDao();
        userDao.addUser(user);
        
    }

}
UserService.java

 

package com.Gary.web;

import java.util.UUID;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{

    public User user = new User();
    
        
    public String register() throws Exception {
        
        //自动封装User
        //没有的手动封装
        user.setId(UUID.randomUUID().toString());
        //传递数据
        UserService userService = new UserService();
        userService.addUser(user);
        System.out.println("123");
        return "toLogin";
    }


    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }
    
}
UserAction.java

 

  数据库表hibernatest.user

 

 

 

  

 

   【项目结构】 

 

注册表单页面register.jsp

<form action="UserRegServlet" method="post">
            <div class="register-box">
                <label for="username" class="username_label">
                    用 户 名
                    <input maxlength="20" name="userName" type="text"
                        placeholder="您的用户名和登录名" />
                </label>
                <div class="tips">

                </div>
            </div>
            <div class="register-box">
                <label for="username" class="other_label">
                    设 置 密 码
                    <input maxlength="20" type="password" name="pwd"
                        placeholder="建议至少使用两种字符组合" />
                </label>
                <div class="tips">

                </div>
            </div>
            <div class="register-box">
                <label for="username" class="other_label">
                    确 认 密 码
                    <input maxlength="20" type="password" placeholder="请再次输入密码" />
                </label>
                <div class="tips">

                </div>
            </div>
            
            <div class="register-box">
                <label for="username" class="username_label">
                    邮箱
                    <input maxlength="20" name="userName" type="text"
                        placeholder="您的邮箱" />
                </label>
                <div class="tips">
                </div>
            </div>
            
            <div class="register-box">
                <label for="username" class="username_label">
                    真是姓名
                    <input maxlength="20" name="userName" type="text"
                        placeholder="您的真是姓名" />
                </label>
                <div class="tips">
                </div>
            </div>
            
            <div class="register-box">
                <label for="username" class="username_label">
                    手机号
                    <input maxlength="20" name="telephone" type="text"
                        placeholder="您的手机号" />
                </label>
                <div class="tips">
                </div>
            </div>

            <div class="arguement">
                <input type="checkbox" id="xieyi" />
                阅读并同意
                <a href="javascript:void(0)">《错题用户注册协议》</a>
                <a href="login.html">已有账号,立即登录</a>
                <div class="tips">
                </div>
            </div>
            <div class="submit_btn">
                <button type="submit" id="submit_btn">
                    立 即 注 册
                </button>
            </div>
        </form>

 

  

 

  用户实体层

    private String id;
    private String username;
    private String password;
    private String name;
    private String email;
    private String telephone;

 

   用户提交表单,发送请求UserAction_register到Web层userService.java

    public String register() throws Exception {
        
        //自动封装User
        //没有的手动封装
        user.setId(UUID.randomUUID().toString());
        //传递数据
        UserService userService = new UserService();
        userService.addUser(user);
        System.out.println("123");
        return "toLogin";
    }

 

   Service层接收到WeB层发送过来的请求后,进行处理,并发送到Dao层UserDao.java进行数据处理

    public void addUser(User user) throws SQLException {
        System.out.println("456");
        QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource());
        String sql = "insert into user values(?,?,?,?,?,?)";
        runner.update(sql,user.getId(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getTelephone());
    }

 

   使用c3p0链接MySQL数据库在c3p0-config.xml中进行配置

    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///hibernatest</property>
        <property name="user">root</property>
        <property name="password">123456</property>
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">20</property>
    </default-config>

 

  

 

 

 

posted @ 2019-11-11 13:40  Cynical丶Gary  阅读(671)  评论(0编辑  收藏  举报