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

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

package lcx;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class l {
    public void regUser(User user) {
        String sql = "insert into stu(uname,password,age) value(?,?,?)";
        Connection cn = null;
        PreparedStatement ps = null;
        // 创建连接
        cn = llcx.getCon();
        try {
            // 预编译
            ps = cn.prepareStatement(sql);
            // 替换占位符‘?’
            ps.setString(1, user.getUname());
            ps.setString(2, user.getPassword());
            ps.setInt(3, user.getAge());
            // 编译执行sql语句
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                cn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public User getUserByName(String uname) {
        String sql = "select * from stu where uname=?";
        Connection cn = null;
        PreparedStatement ps = null;
        ResultSet res = null;
        User user = null;
        cn = llcx.getCon();
        try {
            ps = cn.prepareStatement(sql);
            ps.setString(1, uname);
            res = ps.executeQuery();
            if (res.next()) {
                user = new User(res.getString("uname"), res.getString("password"), res.getInt("age"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                cn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return user;
    }

}

  

package lcx;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class llcx {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Connection getCon() {
        Connection cn = null;
        try {
            cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return cn;
    }

}

  

package lcx;



public class User {
    private Integer stuId;
    private String uname;
    private String password;
    private Integer age;
    public User(String uname, String password, Integer age) {
        super();
        this.uname = uname;
        this.password = password;
        this.age = age;
    }
    public User() {
        super();
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Integer getStuId() {
        return stuId;
    }
    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }
    

}

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'register.jsp' starting page</title>
    
  </head>
  
  <body>

    <form  action="index3.jsp" method="post">

        用户名:<input type="text" name="uname"><br> 
        密   码: <input type="password" name="password"> <br>
        年   龄:<input type="text" name="age"><br>
        <input type="submit" value="注册"><hr>
    </form><hr>
    <a href="index1.jsp">返回登录</a>
</body>
</html>

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'login.jsp' starting page</title>
    
  </head>
  <body>
  <script type="text/javascript">
        function validate() {
            if (loginForm.uname.value == "") {
                alert("账号不能为空!");
                return;
            }
            if (loginForm.password.value == "") {
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>
    <form  name="loginForm" action="index2.jsp" method="post">
        用户名:<input type="text" name="uname"><br> 
        密   码: <input type="password" name="password"> <br>
        <input type="button" value="登录" onclick="validate()"><hr>
    </form>
    <hr>
    <a href="index.jsp">返回注册</a>
</body>
 
</html>

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.ym.mysql.User" %>
<%@ page import="com.ym.mysql.StuDao" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index2.jsp' starting page</title>
    
  </head>
  
  <body >
    <%
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        StuDao stuDao = new StuDao();
        User user = stuDao.getUserByName(uname);
       if (password.equals(user.getPassword())) {
         response.sendRedirect("index5.jsp");
       } else {
        response.sendRedirect("index6.jsp");
        }
    %>
</body>
</html>

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="lcx.User" %>
<%@ page import="lcx.l" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index3.jsp' starting page</title>
    
  </head>
  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    String uname=request.getParameter("uname");
    String password=request.getParameter("password");
    Integer age=Integer.parseInt(request.getParameter("age"));
    User user=new User(uname,password,age);
    l l=new l();
    l.regUser(user);
    response.sendRedirect("index1.jsp");
     %>
  </body>
</html>

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index4.jsp' starting page</title>
    
  </head>
  
  <body>
    <h1>登录成功!!!</h1>
  </body>
</html>

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index5.jsp' starting page</title>
    
  </head>
  
  <body>
       <h1>登录失败!3秒后将返回登录界面。。。</h1>
<%
    response.setHeader("refresh", "3;url=index1.jsp");
%>

  </body>
</html>

  

 

 

 

posted on 2022-05-08 17:21  李昌璇  阅读(4)  评论(0编辑  收藏  举报