通过oracle存储过程实现用户登陆验证

来源:互联网 发布:淘宝商家添加商品 编辑:程序博客网 时间:2024/06/05 10:09

1.创建用户表:

create table t_user(
u_id number constraint pk_tuser primary key,
u_name varchar2(15),
u_pwd varchar2(20),
sex varchar2(2) check(sex in ('男','女')),
email varchar2(20),
address varchar2(100)
)


2.创建错误登陆信息表,存储用户登陆的错误信息。当30分钟内错误登陆5次,怎进禁止登陆

简化错误登陆信息表:error_login

创建表:

create table error_login(
u_name varchar2(15), 
err_login_time date
)


3.登陆验证存储过程如下:

create or replace procedure pro_validate_user(o_code out number,
o_note out varchar2,
i_uname in t_user.u_name%type,
i_pwd in t_user.u_pwd%type)
as
v_count number;
begin
  o_code:=1;
  o_note:='校验成功,准许登陆';
  if i_uname is null then
    o_code:=-1;
    o_note:='用户名不能为空';   
  end if;
  if i_pwd is null then
    o_code:=-2;
    o_note:='密码不能为空';    
  end if;
  select count(1) into v_count from t_user where u_name=i_uname and u_pwd=i_pwd;
  if v_count=0 then
    o_code:=-3;
    o_note:='用户名或者密码错误';
    insert into error_login values (i_uname,sysdate);
    commit;
    select count(1) into v_count from error_login where u_name=i_uname  and err_login_time>(sysdate-30/1440);    
    if v_count>=5 then 
      o_code:=-99;
      o_note:='当前用户错误登陆次数过多,已被锁定,请联系管理员';
    end if;    
  elsif v_count=1 then
    o_code:=1;
    o_note:='校验成功,准许登陆';
  else 
    o_code:=-4;
    o_note:='用户名存在重复,请联系管理员';    
  end if;
exception
  when others then
    o_code:=-5;
    o_note:='查询结果出现错误';
end pro_validate_user;
/



原创粉丝点击