贾云鹏

jsp第十周作业

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

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

package aaa;
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/mysql", "root", "001010Chen");
            } 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 aaa;


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 pwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from stu where uname=? and pwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, pwd);
            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 void reg(String uname, String pwd, int age) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(uname,pwd,age) values(?,?,?)"; // 3个占位符
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, pwd);
            ps.setInt(3, age);
            ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }

}
package aaa;

public class student {
    String sname;
    int pwd;
    int age;
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public int getPwd() {
        return pwd;
    }
    public void setPwd(int pwd) {
        this.pwd = pwd;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "student [sname=" + sname + ", pwd=" + pwd + ", age=" + age
                + "]";
    }
    
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

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

<html>
<head>

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

</head>
<%
    
%>
<body>
    <form action="slogin.jsp">
        学生姓名:<input type="text" name="sname" /> 账号密码:<input type="password"
            name="spwd" /> <input type="submit" value="登录" />
    </form>
    <form action="slogon.jsp">
        <input type="submit" value="注册" />
    </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

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

<html>
<head>

<title></title>

</head>
<%@ page import="aaa.stuDao"%>
<%
    String name = request.getParameter("sname");
    String spwd = request.getParameter("spwd");
    String sname = new String(name.getBytes("ISO-8859-1"), "utf-8");
    try {
        int pwd = Integer.parseInt(spwd);
        stuDao studao = new stuDao();
        if (studao.login(sname, pwd) != null) {
            out.println("登录成功!");
        } else {
            out.println("登陆失败!");
            out.println("三秒后跳转到主页面...");
            response.setHeader("refresh", "3;url=student.jsp");
        }
    } catch (Exception e) {
        out.println("异常!!");
        out.println("三秒后跳转到主页面...");
        response.setHeader("refresh", "3;url=student.jsp");
    }
%>
<body>
    <a href="student.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+"/";
%>

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

<html>
<head>

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

</head>
<%
    
%>
<body>
    <h3>请输入信息来完成注册...</h3>
    <form action="slogon_link.jsp">
        用户名:<input type="text" name="sname" /> 密码:<input type="password"
            name="spwd" /> 年龄:<input type="text" name="sage" /> <input
            type="submit" value="注册" />
    </form>
    <a href="student.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+"/";
%>

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

<html>
<head>

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

</head>
<%@ page import="aaa.stuDao"%>
<%
    String sname = request.getParameter("sname");
    String spwd = request.getParameter("spwd");
    String sage = request.getParameter("sage");
    String name=new String(sname.getBytes("ISO-8859-1"),"utf-8");
    int pwd = Integer.parseInt(spwd);
    int age = Integer.parseInt(sage);
    stuDao s = new stuDao();
    if (s.reg(sname, pwd, age) != -1) {
        out.println("注册成功!");
        out.println("三秒后跳转到主页...");
        response.setHeader("refresh", "3;url=student.jsp");
    } else {
        out.println("注册失败!");
        out.println("三秒后跳转到主页...");
        response.setHeader("refresh", "3;url=student.jsp");
    }
%>
<body>
</body>
</html>

 

posted on 2022-05-08 14:47  贾云鹏  阅读(19)  评论(0编辑  收藏  举报

导航