jsp第十次作业

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

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

<%@ 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="doreg.jsp" method="post">

        用户名:<input type="text" name="uname"><br> 
        密   码: <input type="password" name="password"> <br>
        确认密码: <input type="password" name="password"> <br>
        年   龄:<input type="text" name="age"><br>
        <input type="submit" value="注册">
        <a href="login.jsp">登录</a>
    </form>
    
  </body>
</html>
package com.gb.zqj;

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

public class BaseDao {
    public Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.zqj.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
    
    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 com.gb.zqj;

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

public class StuDao extends BaseDao {
    public int Register(String uname, String password, int age) {
        int i = -1;
        Connection con = 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 {
            closeAll(con, pred, null);
        }
        return i;
    }
    public boolean Login(String uname, String password) {
        boolean f=false;
        Connection con = 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 {
            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></title>
  </head>
  <body>
  <h1>登录成功</h1>
  </body>
</html>

 

 

 

 

 

 

posted @ 2022-05-08 15:18  不知名的怪物先生  阅读(21)  评论(0编辑  收藏  举报