检查java数据库中的用户名和密码,如果错误则给出错误的密码信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20074897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check username and password in java DataBase and give wrong password message if false
提问by prince_ade
i want to display an error wrong username & password after comparing entered username and password with database of users in java.
在将输入的用户名和密码与 java 中的用户数据库进行比较后,我想显示错误的用户名和密码。
the problem is it does the if else statement against each row until it gets to the right row b4 displaying "username and password correct" but i want it to check against all and if it doesn't exist then it displays "Please Check Username and Password "
问题是它对每一行执行 if else 语句,直到它到达显示“用户名和密码正确”的右行 b4,但我希望它检查所有内容,如果它不存在,则显示“请检查用户名和密码 ”
note: please ignore the naming convention the problem is in the arrangement of the while, if and any other recommended loop statements but i am not sure on how to organise it to get my desired result
注意:请忽略命名约定问题在于 while、if 和任何其他推荐的循环语句的排列,但我不确定如何组织它以获得我想要的结果
here is my code with comments
这是我的带有注释的代码
public void displayUsers(String f, String s) {
try {
String queryString = "SELECT SName, SPwd FROM staff";
ResultSet results = Statement.executeQuery(queryString);
while (results.next()) {
String staffname = results.getString("snameeee");
String password = results.getString("SPwd");
if ((f.equals(staffname)) && (s.equals(password))) {
JOptionPane.showMessageDialog(null, "Username and Password exist");
}else {
//JOptionPane.showMessageDialog(null, "Please Check Username and Password ");
}
results.close();
} catch (SQLException sql) {
out.println(sql);
}
采纳答案by Shoaib Chikate
First don't store password in plain text.Secondly loading all records is very wrong approach of doing above code.
首先不要以纯文本形式存储密码。其次加载所有记录是执行上述代码的非常错误的方法。
public void displayUsers(String f, String s) {
try {
String queryString = "SELECT * FROM staff where SName=? and SPwd=?";
//set this values using PreparedStatement
ResultSet results = ps.executeQuery(queryString); //where ps is Object of PreparedStatement
if(!results.next()) {
JOptionPane.showMessageDialog("Wrong Username and Password.");
}
} catch (SQLException sql) {
out.println(sql);
}finally{
//closing ResultSet,PreparedStatement and Connection object
}
回答by Suresh Atta
No, what you are doing is wrong.
不,你的做法是错误的。
Loading all records is not good practice to check credentials.
加载所有记录不是检查凭据的好习惯。
Pass username
parameter to your query and check in database.
将username
参数传递给您的查询并签入数据库。
1)If no user exists, tell username
not exists.
1)如果没有用户存在,告诉username
不存在。
2)If user exists then check password
of with existed database user password.
2)如果用户存在,则检查password
现有的数据库用户password.
回答by subash
using flag you can solve this problem easily. like this..
使用标志可以轻松解决这个问题。像这样..
public void displayUsers(String f, String s) {
boolean flag = false;
try {
String queryString = "SELECT SName, SPwd FROM staff";
ResultSet results = Statement.executeQuery(queryString);
while (results.next()) {
String staffname = results.getString("SName");
String password = results.getString("SPwd");
if ((f.equals(staffname)) && (s.equals(password))) {
flag = true;
JOptionPane.showMessageDialog(null, "Username and Password exist");
}
results.close();
if(!flag){
JOptionPane.showMessageDialog(null, "Please Check Username and Password ");
}
} catch (SQLException sql) {
out.println(sql);
}
回答by maiklahoz
You can use:
您可以使用:
boolean exist = false;
String queryString = "SELECT SName, SPwd FROM staff";
ResultSet results = Statement.executeQuery(queryString);
while (results.next()) {
String staffname = results.getString("SName");
String password = results.getString("SPwd");
if ((f.equals(staffname)) && (s.equals(password))) {
exist = true;
JOptionPane.showMessageDialog(null, "Username and Password exist");
}
}
results.close();
if(!exist){
JOptionPane.showMessageDialog(null, "Please Check Username and Password ");
}
But you should use this:
但是你应该使用这个:
String queryString = "SELECT SName, SPwd FROM staff where SName=? and SPwd=?";
ps = con.prepareStatement(queryString);
ps.setString(1,f);
ps.setString(2,s);
ResultSet results = ps.executeQuery();
if (results.next()) {
JOptionPane.showMessageDialog(null, "Username and Password exist");
}else{
JOptionPane.showMessageDialog(null, "Please Check Username and Password ");
}
results.close();
con.close();
回答by ramesh koppolu
String queryString =" select count(*) as \"exists\" from credit where username=? and password=?";
//set this values using PreparedStatement
ps = con.prepareStatement(queryString);
ps.setString(1,f);
ps.setString(2,s);
ResultSet results = ps.executeQuery();
if (results.next()) {
int i = results.getInt("exists");
if(i==1)
{
JOptionPane.showMessageDialog(null, "Username and Password exist");
}
else{
JOptionPane.showMessageDialog(null, "Please Check Username and Password ");
}
回答by savan patidar
if (username.length()>0 && password.length()>0)
{
String query = "Select * from adminlogin Where Username='" + username + "' and Password='" + password + "'";
rs = sta.executeQuery(query);
if (rs.next())
{
home hme=new home();
this.setVisible(false);
hme.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null,"username and password are wrong ");
}
}
else
{
JOptionPane.showMessageDialog(null,"please field username and password ");
}