使用MVC实现用户登录注册系统—银行管理系统

版权声明:原创文章 欢迎参考 请勿抄袭 https://blog.csdn.net/aA518189/article/details/78858323


一.介绍:使用MVC架构,设计一个银行账户余额显示程序


二.功能:实现了登录和注册功能,根据用户帐户余额的不同,跳到不同的页面显示不同的信息:

负的余额:警示信息

正常余额:标准信息

高余额:  包括广告

未知的客户ID:错误页面

三:用到的知识:javabean,html,jsp,servlet,css,javascript,服务器Tomcat,已经MVC设计思想。

(注册功能没有实现,读者可以自己试下哦,和登录的servlet差不多,就是向数据库中添加一个你要注册的用户而已。)

 

 

四  实验步骤

程序结构,如下图,采用MVC模式进行程序设计


1.    建立一个用户数据库,如下图


2.    登录界面,如下图


3.    用户信息输入错误时,进行相应的提示,如下图

 

 

 

 

 

5.用户登录 如果账户剩余金额大于等于2000RMB。则跳到广告界面,如:用户Jack余额为2000RMB,登录的返回页面如下

 

 

 

 

4.    当账户剩余金额小于0RMB,则调到警告界面

用户jack余额为 0 RMB,登录的返回页面如下

 

 

 

 

 

 

5.    当账户余额在0RMB和2000RMB之间,则调到正常欢迎界面

如:用户adear余额为:10RMB登录的返回页面如下

 

五 主要代码

1.  数据库连接类: my_connection

package db;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

 

public class  my_connection {

    public Connection get_connection() {

        Connection conn = null;

        String name = "root";

        String password = "518189";

        try {

           Class.forName("com.mysql.jdbc.Driver");

            String url ="jdbc:mysql://localhost:3306/back?charaterEncoding=utf-8";

            conn =DriverManager.getConnection(url, name, password);

        } catch (Exception E) {

       }

        return conn;

    }

    public void closeDB(ResultSet rs, Statementsta, Connection conn) {

        try {

            rs.close();

            sta.close();

            conn.close();

        } catch (Exception E) {

        }

    }

}

2. 

1.  VO:用户实体类 users

packageVO;

publicclass users {

    private String name;

    private String password ;

    private Double  balance ;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }

    public Double getBalance() {

        return balance;

    }

    public void setBalance(Double balance) {

        this.balance = balance;

    }

}

2. DAO:数据访问对象: use_dao

 

package DAO;

import VO.*;

import  db.*;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.*;

 

public class use_dao {

    public void insertUsers(users u ){

        my_connection db=new my_connection();

        Connection conn=null;

        Statement sta=null;

        try {

            conn=db.get_connection();

            sta=conn.createStatement();

            String sql="insert intouser(name,password)values('"+u.getName()+"','"+u.getPassword()+"')";

            sta.executeUpdate(sql);

        }catch (Exception E){

 

        }finally {

    }

    }

    public ResultSet select(users u){

        my_connection db=new my_connection();

        Connection conn=null;

        PreparedStatement  sta=null;

        ResultSet  rs=null;

        String uname=u.getName();

        String upassword=u.getPassword();

        try {

            String sql="select * from userwhere name=? and pwd=?";

            conn=db.get_connection();

            sta=conn.prepareStatement(sql);

            sta.setObject(1,uname);

 

            rs= sta.executeQuery(sql);

        }catch (Exception E){

 

        }finally {

 

        }

        return rs;

    }

}

 

3.  servlet:    user_servlet

 package servlet

importjavax.servlet.ServletException;

importjavax.servlet.annotation.WebServlet;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.sql.ResultSet;

import  VO.users;

import  DAO.use_dao;

 

public class user_servlet extendsHttpServlet {

    protected void doPost(HttpServletRequestrequest, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequestrequest, HttpServletResponse response) throws ServletException, IOException {

        String uname =request.getParameter("name");

        String upassword =request.getParameter("password");

        users u = new users();

        u.setName(uname);

        u.setPassword(upassword);

        use_dao dao = new use_dao();

        ResultSet RS = dao.select(u);

        try{   if (RS.next()) {

            Doublemoney=RS.getDouble("money");

            if(money<=0){

               request.getRequestDispatcher("/alter.jsp").forward(request,response);

            }elseif(0<money&&money<2000){

               request.setAttribute("money",money);

               request.setAttribute("name",uname);

                request.getRequestDispatcher("/view.jsp").forward(request,response);

            }else {

               request.getRequestDispatcher("/high.jsp").forward(request,response);

            }

 

            }

        }catch (Exception E){

 

     }

}

4. 银行系统登录界面:login.jsp

<%--

  Created by IntelliJ IDEA.

  User: 王志华

  Date: 17-11-17

  Time: 下午7:09

  To change this template use File | Settings |File Templates.

--%>

<%@ pagecontentType="text/html;charset=UTF-8" pageEncoding="utf-8"language="java" %>

<html>

<head>

    <meta charset="utf-8">

    <title > 王志华的设计页面</title>

    <scripttype="text/javascript">

        window.onload=function()

        {

            varbt=document.getElementById("p3");

            bt.onclick=function()

            {

                if(document.myform.name.value=="")

                {

                    alert("用户名不能为空!");

                   document.myform.name.focus();

                    return false;

                }

                elseif(document.myform.password.value=="")

                {

                    alert("密码不能为空!");

                   document.myform.password.focus();

                    return false;

                }

            }

        }

    </script>

    <style >

        form{

            width: 1200px;  height: 800px;background:-webkit-linear-gradient(#4686de 0%, #ffffff 100%);padding: 20px;

        }

        input{ width: 200px ;height: 30px;

            background: seashell       }

        input:focus{background: whitesmoke}

        <!--颜色加文本框一起加聚焦-->

 

            a{ color:#ff32d1;width: 200px;height: 50px}

 

        legend { color:black;font-size:40px;font-style: normal;

        }

        #p3{width: 60px;height: 30px;background: powderblue;}

        #p4{width: 60px;height: 30px;background: powderblue;}

        #p5{width: 60px;height: 30px ;background:powderblue;}

    </style>

 

 

 

</head>

<bodybgcolor="#e3e3e3">

<center>

    <form action="user_select"method="get" name="myform">

        <fieldset  >

            <legendalign="center">银行系统</legend>

            <div>

                <labelfor="p1">账号:</label>

                <input id="p1"type="text" name="name">

            </div>

            <br>

            <div>

                <labelfor="p2">密码:</label>

                <inputid="p2" type="password" name="password">

            </div>

            <div>

                <tablealign="center">

                    <tr>

                       <td>  <input   type="submit" id="p3"value="登录"/></td>

                        <td><input   type="reset"id="p4"   value="重置"/></td>

                       <td><input type="button" id="p5"onclick="window.location.href='register.jsp'"class="searchbut" value="注册" ></td>

                    </tr>

                </table>

            </div>

 

        </fieldset>

 

    </form>

</center>

</body>

</html>

5. 广告界面 high_page.jsp

<%@ pagecontentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

  <style>

    h1{

      font-size: xx-large;color: #ff32d1;

    }

  </style>

 

</head>

<body>

<center>

  <h1>人生如此美好,何不今朝购物今朝醉</h1>

  <img src="/photo/p1.jpg">

</center>

</body>

</html>

6. 警告界面,alter_page.jsp

<%@ pagecontentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

<style>

    body{

        background:url("photo/p3.jpg");

    }

</style>

</head>

<body>

<center>

   <h1>请你尽快还钱</h1>

    <img src="/photo/p2.jpg">

</center>

</body>

</htmlJPG

7. 正常登录界面 view.jsp

<%--

  Created by IntelliJ IDEA.

  User: apple

  Date: 17-12-1

  Time: 下午4:07

  To change this template use File | Settings |File Templates.

--%>

<%@ pagecontentType="text/html;charset=UTF-8" language="java" %>

<html>

  <head>

    <title></title>

  </head>

  <body BGCOLOR="ffeefe">

  <center>

  欢迎<% request.getParameter("name")%>

  您的余额为:<% out.print(request.getParameter("money"));

  %>:RMB

  </center>

  </body>

</html>

阅读更多
换一批

没有更多推荐了,返回首页