我想显示错误的用户名&将输入的用户名和密码与java中的用户数据库进行比较后的密码。
问题是它对每一行执行if else语句,直到它到达右侧行b4显示“用户名和密码正确”但我希望它检查所有并且如果它不存在那么它显示“请检查用户名和密码“
注意:请忽略命名约定,问题在于while,if和任何其他推荐的循环语句的排列,但我不确定如何组织它以获得我想要的结果
这是我的带注释的代码
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);
}
答案 0 :(得分:3)
不,你在做什么是错的。
加载所有记录不是检查凭据的好方法。
将username
参数传递给您的查询并签入数据库。
1)如果没有用户,请告诉username
不存在。
2)如果用户存在,则检查现有数据库用户password
的{{1}}
答案 1 :(得分:1)
首先不要以明文形式存储密码。其次加载所有记录是做错的方法。
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
}
答案 2 :(得分:0)
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);
}
答案 3 :(得分:0)
您可以使用:
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 ");
}
但你应该用这个:
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();
答案 4 :(得分:0)
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 ");
}
答案 5 :(得分:0)
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 ");
}