我创建了一个登录系统,可以在阻止帐户之前检查尝试,以防密码失败超过 3 次。问题是我需要检查用户名是否存在,如果不存在,则打印一条消息,说明 id 不存在;但我不知道该怎么办。我的意思是,我有如何做到这一点的想法,但我不知道如何在 Java 上做到这一点。
这些是我存储的程序
登录
create proc usp_Login
@id varchar(50),
@pw varchar(50)
as
select id, pw from Login where id = @id and pw = @pw and lock = 0 and tries < 4
update Login set tries = 0 where id = @id and pw = @pw;
尝试
create proc usp_Attempts
@id varchar(50)
as
select id, tries from Login where id = @id and tries < 4
update Login set tries = tries+1 where tries < 4 and id = @id
阻止用户
create proc usp_Lock
@id varchar(50)
as
update Login set lock = 1 where id = @id
而java部分是这样的:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
CallableStatement cStmt = null;
ResultSet rs = null;
try {
cStmt = conn.prepareCall("{call usp_Login(?, ?)}");
cStmt.setString(1, this.txtID.getText());
cStmt.setString(2, this.txtPW.getText());
cStmt.execute();
rs = cStmt.getResultSet();
if (rs.next()) {
System.out.println("Login done");
cStmt.close();
rs.close();
conn.close();
} else {
cStmt = conn.prepareCall("{call usp_Attempts(?)}");
cStmt.setString(1, this.txtID.getText());
cStmt.execute();
rs = cStmt.getResultSet();
if (rs.next()) {
System.out.println("Invalid login");
} else {
cStmt = conn.prepareCall("{call usp_Lock(?)}");
cStmt.setString(1, this.txtID.getText());
cStmt.execute();
System.out.println("Login blocked");
cStmt.close();
rs.close();
conn.close();
}
}
} catch (SQLException ex) {
System.out.print(ex);
}
}
我知道这可能不是创建登录系统的最佳方式,但我仍在学习,这是我目前得到的最好的想法。希望有人可以帮助我进行身份验证部分,在此先感谢。