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

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

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

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

package jw;

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 jw;

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>
</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 language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="dao.StuDao"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String name = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        String uname = new String(name.getBytes("ISO-8859-1"), "utf-8");
        try {
            int pwd = Integer.parseInt(upwd);
            StuDao s = new StuDao();
            if (s.login(uname, upwd)) {
                out.println("登录成功!");
            } else {
                out.println("登陆失败!");
                out.println("三秒后跳转到主页面...");
                response.setHeader("refresh", "3;url=login.jsp");
            }
        } catch (Exception e) {
            out.println("异常!!");
            out.println("三秒后跳转到主页面...");
            response.setHeader("refresh", "3;url=login.jsp");
        }

    %>
    <a href="login.jsp">返回</a>
</body>
</html>

  

posted on 2022-05-07 22:13  王运聪  阅读(4)  评论(0编辑  收藏  举报