第十周作业

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

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

package glg;
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", "990511");
            } 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();
        }
    }
}
package glg;

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, String pwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from stu where uname=? and upwd=?";
        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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return f;
    }
 
    public void reg(String uname, String upwd,int uage) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(uname,upwd,uage) values(?,?,?)";
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            ps.setInt(3, uage);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
 
    }
 
}
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
<form action="verification.jsp" method="post">
            用户名:
            <input type="text" name="uname" />
            <br />
            密   码:
            <input type="text" name="upwd" />
            <br />
            <input type="submit" value="登录" />
            <a href="register.jsp">注册</a>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="glg.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 uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
 
        StuDao s1 = new StuDao();
        if (s1.login(uname, upwd)) {
            request.getRequestDispatcher("main.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("false.jsp").forward(request, response);
        }
    %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
     
<!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>
 
<form action="jump.jsp" method="post">
           用户名:
            <input type="text" name="uname" />
            <br />
            密   码:
            <input type="text" name="upwd" />
            <br />
            年   龄:
            <input type="text" name="uage"/>
            <br />
            <input type="submit" value="注册" />
            <a href="login.jsp">登录</a>
        </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="glg.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 uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        String uages = request.getParameter("uage");
 
        int uage = Integer.parseInt(uages);
        StuDao s1 = new StuDao();
        s1.reg(uname, upwd, uage);
        request.getRequestDispatcher("login.jsp").forward(request, response);
    %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
    <h1>登录成功</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
    <h1>登录失败,5秒后返回登录界面</h1>
    <%
        response.setHeader("refresh", "5;url=login.jsp");
    %>
</body>
</html>

 

posted @ 2022-05-08 18:42  无言De钉钉  阅读(4)  评论(0编辑  收藏  举报