• 博客园Logo
  • 首页
  • 新闻
  • 博问
  • 专区
  • 闪存
  • 班级
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 简洁模式 ... 退出登录
    注册 登录
Jraino3o
博客园    首页    新随笔    联系   管理    订阅  订阅

JSP连接数据库实现注册登录

1.数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)

 

 

2.设计一个注册页面,实现用户注册功能

3.设计一个登陆页面,实现用户名密码登陆

4.两个页面可以互相超链接

 

 

 

 

//代码
//StuDao.java
public class StuDao extends BaseDao {
    public boolean login(String name, String pwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from stu where stuName=? and stuPwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return f;
    }

    public void reg(String stuName, String stuPwd,int stuAge) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(stuName,stuPwd,stuAge) values(?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, stuName);
            ps.setString(2, stuPwd);
            ps.setInt(3, stuAge);
            ps.executeUpdate();
        } catch (SQLException e) {

            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }
}
//BaseDao.java
public class BaseDao {
    protected Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null)
                rs.close();
            if (ps != null)
                ps.close();
            if (con != null)
                con.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
//userRegister.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
</head>
<body>
<script type="text/javascript">
    function check() {
        if (login.account.value==""){
            alert("请输入账号!");
            return;
        }else if(login.psw.value=="") {
            alert("请输入密码!");
            return;
        }else if(login.psw1.value=="") {
            alert("请再次输入密码!");
            return;
        }else if(login.stuAge.value=="") {
            alert("请输入年龄!");
            return;
        }
        var pwd = document.getElementById("pwd").value;
        var pwd1 = document.getElementById("pwd1").value;
        if (pwd==pwd1){
            alert("注册成功!");
            login.submit();
        }else {
            alert("两次输入密码不一样!");
            return;
        }

    }
</script>
<form action="checkReg.jsp" method="post" name="login">
    请输入账号:<input type="text" name="account"><br>
    请输入密码:<input type="password" name="psw" id="pwd"><br>
    请再次输入密码:<input type="password" name="psw1" id="pwd1"><br>
    请输入年龄:<input type="text" name="stuAge"><br>
    <input type="button" value="注册" onclick="check()">
</form>
<form action="login.jsp" method="post">
    <input type="submit" value="已有账号?前往登录">
</form>
</body>
</html>
//checkReg.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String account=request.getParameter("account");
    String psw=request.getParameter("psw");
    String psw1=request.getParameter("psw1");
    String age=request.getParameter("stuAge");
    if (psw.equals(psw1)) {
        int stuAge=Integer.parseInt(age);
        StuDao stuDao = new StuDao();
        stuDao.reg(account,psw,stuAge);
        request.getRequestDispatcher("login.jsp").forward(request,response);
    } else {
        request.getRequestDispatcher("userRegister.jsp").forward(request,response);
    }
%>
</body>
</html>
//login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<script type="text/javascript">
    function check() {
        if (login.account.value==""){
            alert("请输入账号!");
            return;
        }else if(login.psw.value=="") {
            alert("请输入密码!");
            return;
        }
        login.submit();
    }
</script>
    <form action="check.jsp" method="post" name="login">
        账号:<input type="text" name="account"><br>
        密码:<input type="password" name="psw"><br>
        <input type="checkbox" value="注册会员">注册会员
        <input type="checkbox" value="不注册会员">不注册会员<br>
        <input type="button" value="登录" onclick="check()">
    </form><br>
<form action="userRegister.jsp" method="post">
    <input type="submit" value="立即注册账户">
</form>
</body>
</html>
//check.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>check</title>
</head>
<body>
        <%
            String account=request.getParameter("account");
            String psw=request.getParameter("psw");
            StuDao stuDao = new StuDao();
            if (stuDao.login(account,psw)){
                session.setAttribute("account",account);
                request.getRequestDispatcher("logSuccess.jsp").forward(request,response);
            }else {
                request.getRequestDispatcher("logFail.jsp").forward(request,response);
            }
        %>
</body>
</html>

 



 
posted @ 2022-05-05 21:27  Jraino3o  阅读(94)  评论(0)  编辑  收藏  举报
刷新评论刷新页面返回顶部
Copyright © 2022 Jraino3o
Powered by .NET 6 on Kubernetes