第十周作业

<%@ 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">
    <b>用户名</b>
    <input type="text" name="sname" />
    <br />
    <b>密码</b>
    <input type="text" name="pwd" />
    <br />
    <b>年龄</b>
    <input type="text" name="age"/>
    <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="dao.StudentDao"%>
<!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 sname = request.getParameter("sname");
    String pwd = request.getParameter("pwd");
    String age = request.getParameter("age");
    System.out.println(sname);
    int newage = Integer.parseInt(age);

    StudentDao studentDao = new StudentDao();
    studentDao.reg(sname,pwd,newage);
    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>
<form action="verification.jsp" method="post">
    <b>用户名</b>
    <input type="text" name="sname" />
    <br />
    <b>密码</b>
    <input type="text" name="pwd" />
    <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"%>
<!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>登录失败,3秒后返回登录界面</h1>
<%
    response.setHeader("refresh", "3;url=login.jsp");
%>
</body>
</html>

 

package pojo;

public class Student {
    private int sid;
    private String sname;
    private String pwd;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "Sid=" + sid +
                ", Sname='" + sname + '\'' +
                ", pwd='" + pwd + '\'' +
                ", age=" + age +
                '}';
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        sname = sname;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

package dao;

import util.DBUtil;

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

public class StudentDao {
    public boolean login(String name, String pwd) {
        boolean flag = false;
        Connection conn = DBUtil.getCon();
        String sql = "select * from stu where sname=? and pwd=?";
        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())
                flag = true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, ps, conn);
        }
        return flag;
    }

    public void reg(String sname, String pwd,int age) {
        Connection conn = DBUtil.getCon();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(sname,pwd,age) values(?,?,?)";
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, sname);
            ps.setString(2, pwd);
            ps.setInt(3, age);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DBUtil.close(null, ps, conn);
        }

    }
}

 

package util;

import java.sql.*;

//工具库工具类
public class DBUtil {

    // 实现驱动加载
    // 类的静态块随着jvm(Java虚拟机)加载类的字节码而开始执行
    // 静态只执行一次
    // 利用静态块的特点,完成JDBC驱动加载,而且在内存中只有一个驱动程序对象
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 可以创建连接对象
    public static Connection getCon() {
        Connection con = null;
        String url = "jdbc:mysql://localhost/mysqldb?useSSL=true&useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "root";
        try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;

    }

    // 可以关闭资源
    public static void close(ResultSet rs, PreparedStatement statement, Connection con) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                        if (con != null) {
                            try {
                                con.close();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }

            }
        } else {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if (con != null) {
                        try {
                            con.close();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }

        }
    }

}

 

 

posted on 2022-05-07 23:18  chenyulin11  阅读(6)  评论(0编辑  收藏  举报

导航