第十周作业

1.设计一个注册页面,实现用户注册功能

package yxf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class BaseDao {
     
     
    public static Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
            System.out.println(con);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
         
    }
     
     
    protected static 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 yxf;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class StuDao  {
     
    public int Register(String uname, String password, int age) {
        int i = -1;
        Connection con = BaseDao.getConnection();
         
        String sql = "insert into stu(uname,password,age)values(?,?,?)";
        PreparedStatement pred = null;
        try {
            pred=con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            pred.setInt(3, age);
            i = pred.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(con, pred, null);
        }
        return i;
    }
 
     
    public boolean Login(String uname, String password) {
        boolean f = false;
        Connection con = BaseDao.getConnection();
        String sql = "select * from stu where uname=? and password=?";
        PreparedStatement pred = null;
        ResultSet resultSet = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            resultSet = pred.executeQuery();
            while (resultSet.next()) {
                f = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(con, pred, resultSet);
        }
        return f;
    }
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE HTML>
<html>
<head>
<title>login</title>
</head>
<body>
 
    <form action="dologin.jsp" method="post">
        账号<input type="text" name="uname"><br>
        密码<input type="password" name="password"><br>
        <input type="submit" value="登录">
        <a href="register.jsp">注册</a>
    </form>
 
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  <!DOCTYPE HTML>
<html>
<head>
<title>dologin</title>
</head>
<body>
      登陆成功
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
 
<!DOCTYPE HTML>
<html>
<head>
<title>注册</title>
</head>
<body>
    <form action="doregister.jsp" method="post">
        账号<input type="text" name="uname"><br>
        密码<input type="password" name="password"><br>
        年龄<input type="number" name="age"><br>
        <input type="submit" value="注册">
        <input type="reset" value="重置">
    </form>
</body>
</html>
<%@page import="yxf.StuDao"%>
<%@page import="javax.xml.bind.ParseConversionEvent"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
 
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
    <%
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        String age = request.getParameter("age");
        int age1 = age == null ? -1 : Integer.parseInt(age);
        StuDao sd=new StuDao();
        int i=sd.Register(uname, password, age1);
        if(i>0){
        request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    %>
</body>
</html>

 

posted @ 2022-05-08 14:09  叶鑫宇  阅读(25)  评论(0编辑  收藏  举报