第十周作业

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

 

 

1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接

package fyx;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.sun.corba.se.spi.legacy.connection.Connection;

public class StuDao extends BaseDao {
    public boolean login(String name, String pwd) {
        boolean f = false;
        Connection conn = (Connection) getConnection();
        String sql = "select * from stu where stuName=? and stuPwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = ((Object) 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 = (Connection) getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(stuName,stuPwd,stuAge) values(?,?,?)";
            ps = ((Object) 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);
        }

    }
}
package fyx;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.sun.corba.se.pept.transport.Connection;

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

protected void closeAll(com.sun.corba.se.spi.legacy.connection.Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
((Statement) conn).close();

} catch (SQLException e) {
e.printStackTrace();
}
}
}

 

 


<%@ 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>
>
<%@ 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>
<%@ 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>
<%@ 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>
<%@ 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-08 10:51  冯雨心  阅读(3)  评论(0编辑  收藏  举报