第十周作业

复制代码
数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)
1.设计一个注册页面,实现用户注册功能2.设计一个登陆页面,实现用户名密码登陆3.两个页面可以互相超链接

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { protected Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.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(); } } }
复制代码
复制代码
复制代码
复制代码
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StuDao extends BaseDao {
    public boolean login(String name, int password) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from stu where name=? and password=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setInt(2, password);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return f;
    }

    public void reg(String name, int password, int age) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(name,password,age) values(?,?,?)";
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setInt(2, password);
            ps.setInt(3, age);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }
}
复制代码
复制代码
复制代码
复制代码
<%--dao.jsp--%>
<%@ page import="Dao.StuDao"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String name = request.getParameter("name");
    String passwords = request.getParameter("password");
    String ages = request.getParameter("age");


    int age = Integer.parseInt(ages);
    int password = Integer.parseInt(passwords);
    StuDao s1 = new StuDao();
    s1.reg(name, password, age);
    request.getRequestDispatcher("login.jsp").forward(request, response);
%>
</body>
</html>
复制代码
复制代码
复制代码
复制代码
<%--dologin.jsp--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登录失败,3秒后返回登录界面</h1>
<%
    response.setHeader("refresh", "3;url=login.jsp");
%>
</body>
</html>
复制代码
复制代码
复制代码
复制代码
<%--index.jsp--%>
<%@ page import="Dao.StuDao"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String name = request.getParameter("name");
    String passwords = request.getParameter("password");
    int password = Integer.parseInt(passwords);
    StuDao s1 = new StuDao();
    if (s1.login(name, password)) {
        request.getRequestDispatcher("main.jsp").forward(request, response);
    } else {
        request.getRequestDispatcher("dologin.jsp").forward(request, response);
    }
%>
</body>
</html>
复制代码
复制代码
复制代码
复制代码
<%--login.jsp--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="index.jsp" method="post">
    <b>用户名</b>
    <input type="text" name="name" />
    <br />
    <b>密码</b>
    <input type="text" name="password" />
    <br />
    <input type="submit" value="登录" />
    <a href="register.jsp">注册</a>
</form>
</body>
</html>
复制代码
复制代码
复制代码
复制代码
<%--main.jsp--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>
复制代码
复制代码
复制代码
复制代码
<%--register.jsp--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="dao.jsp" method="post">
    <b>用户名</b>
    <input type="text" name="name" />
    <br />
    <b>密码</b>
    <input type="text" name="password" />
    <br />
    <b>年龄</b>
    <input type="text" name="age"/>
    <br />
    <input type="submit" value="注册" />
    <a href="login.jsp">登录</a>
</form>
</body>
</html>
 
 
posted @ 2022-05-08 20:29  徐涛%  阅读(3)  评论(0编辑  收藏  举报