黑马prince

导航

JSP第十周作业

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

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

<html>
  <head>
  </head>
  
  <body>
    <script type="text/javascript">
    function validate(){
        if(loginForm.uname.value==""){
            alret("账号不能为空!");
            return;
        }
        if(loginForm.upwd.value=""){
            alert("密码不能为空!");
        }
        loginForm.submit();
    }  
    </script>
    <form name="doLogin.jsp" method="post">
    用户名:<input type="text" name="uname" ><br>
    密码:<input type="password" name="upwd" > 
   年龄:<input type="number" name="age"> 
    <input type="submit" value="登录">
    <a href="rejest.jsp">注册</a>
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  </head>
  
  <body>
<%
    StuDao sd=new StuDao();
    request.setCharacterEncoding("utf-8");
    String uname=request.getParameter("uname");
    String upwd=request.getParameter("upwd");
    
    if(sd.login(uname,upwd)){
    response.sendRedirect("ok.jsp");
    }else
        response.sendRedirect("rejest.jsp");
 %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  </head>
  
  <body>
       <form name="dorejest.jsp" method="post">
    用户名:<input type="text" name="uname" ><br>
    密码:<input type="password" name="upwd" >  
    年龄:<input type="number" name="sex"> 
    <input type="submit" value="注册">
    <a href="login.jsp">登录</a>
        </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  </head>
  
  <body>
<%
    StuDao sd=new StuDao();
    request.setCharacterEncoding("utf-8");
    String uname=request.getParameter("uname");
    String upwd=request.getParameter("upwd");
    
    if(sd.login(uname,upwd)){
    response.sendRedirect("ok.jsp");
    }else
        response.sendRedirect("login.jsp");
 %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>
  </head>
  
  <body>
   登录成功!
  </body>
</html>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
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","lin0520...");
            
        }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 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 Auto-generated catch block
            e.printStackTrace();
        }    finally{
            closeAll(conn, ps, rs);
        }
        return f;
}
    public static void main(String[] args){
        StuDao sd=new StuDao();
        System.out.println(sd.login("zs", "123"));
    }
}

 

 

 

 

 

posted on 2022-05-08 11:08  HumbleSwag  阅读(4)  评论(0编辑  收藏  举报