第十周上机作业

 


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

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

 

package Dao;

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");
                // 2.建立连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false", "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();
        }
    }
    
}

 

package Dao;

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 uname,String upwd){
        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, uname);
            ps.setString(2, upwd);
            rs=ps.executeQuery();
            if(rs.next())
                f=true;        
            
        } catch (SQLException e) {
            // TODO: handle exception
            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(?,?,?)";
            
            ps=conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            ps.setInt(3, uage);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            closeAll(conn,ps,null);
        }
    }

}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>login</title>
    
  </head>
  
  <body>
  
   <form  name="form" action="dologin.jsp" method="post">
      用户名:<input type="text" name="uname"><br>
      密码:<input type="password" name="upwd"><br>
    <input type="submit" value="登录" >    <br>
    <a href="zhuce.jsp">注册新用户</a>
    </form>
  </body>
</html>
<%@page import="Dao.StuDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>do login</title>
    
  </head>
  
  <body>
     <%
     request.setCharacterEncoding("utf-8");
    String uname=request.getParameter("uname");
    String upwd=request.getParameter("upwd");
    
    StuDao sd=new StuDao();
    if(sd.login(uname, upwd)){
        request.getRequestDispatcher("yes.jsp").forward(request,response);
    }else{
    request.getRequestDispatcher("no.jsp").forward(request,response);
    }
     %>
  </body>
</html>
<%@page import="Dao.StuDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>do login</title>
    
  </head>
  
  <body>
     <%
     request.setCharacterEncoding("utf-8");
    String uname=request.getParameter("uname");
    String upwd=request.getParameter("upwd");
    
    StuDao sd=new StuDao();
    if(sd.login(uname, upwd)){
        request.getRequestDispatcher("yes.jsp").forward(request,response);
    }else{
    request.getRequestDispatcher("no.jsp").forward(request,response);
    }
     %>
  </body>
</html>

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>    
    <title></title>
  </head>
  
  <body>
  <a href="dozhuce.jsp">返回登录界面</a>
  <form  name="form" action="dozhuce.jsp" method="post">
      姓名:<input type="text" name="uname"><br>
      密码:<input type="password" name="upwd"><br>
      年龄:<input type="text" name="uage"><br>
    <input type="submit" value="注册完成" >    
    </form>
  </body>
</html>

 

<%@page import="Dao.StuDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>    
    <title>do register</title>
  </head>
  
  <body>
    <%
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        String uages = request.getParameter("uage");
 
        int uage = Integer.parseInt(uages);
        StuDao sd = new StuDao();
        sd.reg(uname, upwd, uage);
        request.getRequestDispatcher("login.jsp").forward(request, response);
    %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>


<title>My JSP 'index.jsp' starting page</title>

</head>

<body>
    <h1>登陆成功!!</h1>
<a href="login.jsp">返回登录界面</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>no</title>
</head>
<body>
    <h1>登陆失败!!5秒后返回登录界面</h1>
   <%response.setHeader("refresh", "5;url=login.jsp");%>
</body>
</html>
 

 

 

 

 

posted @ 2022-05-07 15:36  叼个奶嘴闯天下  阅读(6)  评论(0编辑  收藏  举报