1. 程式人生 > >通過oracle儲存過程實現使用者登陸驗證

通過oracle儲存過程實現使用者登陸驗證

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;
/