第十周作业

package test;

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", "123456");
                } 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 test;

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

public class Stu extends BaseDao{
    public boolean login(String uname,String upwd){
        boolean flag = false;
        Connection c = getConnection();
        String sql = "select * from stu where uname=? and upwd=?";
        PreparedStatement p = null;
        ResultSet r = null;
        try{
            p = c.prepareStatement(sql);
            p.setString(1, uname);
            p.setString(2, upwd);
            r = p.executeQuery();
            if(r.next())
                flag = true;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(c, p, r);
        }
        return flag;
    }
     public void main(String uname,String upwd,int age){
            Connection c=getConnection();
            PreparedStatement p=null;
            try {
                String sql = "insert into stu(uname,upwd,age) values(?,?,?)";
                
                p=c.prepareStatement(sql);
                p.setString(1, uname);
                p.setString(2, upwd);
                p.setInt(3, age);
                p.executeUpdate();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally{
                closeAll(c,p,null);
            }
        }
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
 </head>
  
  <body>
    <form name="login" action="dologin.jsp" method="post">
   用户名 :<input name="uname" type="text"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:<input name="upwd" type="password"><br>
  <input type="submit" value="登录"> <br>
  <a href="zhuce.jsp">注册</a>
  </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>    
    <title></title>
  </head>
  
  <body>
  <form name="zhuce" action="zhuce.jsp" method="post">
      姓名:<input type="text" name="uname"><br>
      密码:<input type="password" name="upwd"><br>
      年龄:<input type="text" name="age"><br>
      <input type="submit" value="完成注册" >
  </form>
    <a href="login.jsp">返回登陆主页</a>
  </body>
</html>
<%@page import="test.Stu"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>    
    <title></title>
  </head>
  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    Stu id = new Stu();
    if(id.login(uname, upwd)){
        request.getRequestDispatcher("true.jsp").forward(request,response);
    }else{
        request.getRequestDispatcher("false.jsp").forward(request,response);
    }
     %>
  </body>
</html>
<%@page import="test.Stu"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>   
  </head>
  
  <body>
     <%
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        String uage = request.getParameter("age");
 
        int age = Integer.parseInt(uage);
        Stu id = new Stu();
        id.main(uname, upwd, age);
        request.getRequestDispatcher("login.jsp").forward(request, response);
    %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  </head>
  
  <body>
    <h1>登陆成功!</h1>
<a href="login.jsp">返回登录界面</a>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  </head>
  
  <body>
     <h1>登陆失败,3秒后返回登录界面</h1>
     <%response.setHeader("refresh", "3;url=login.jsp");%>
  </body>
</html>

 

 

 

 

 

posted @ 2022-05-07 16:57  热血青年小刘  阅读(6)  评论(0编辑  收藏  举报