简体   繁体   English

检查用户名和密码时出错

[英]Error when checking for username and password

Im currently attempting to make a login system in Java and SQL and am currently getting an error. 我目前正在尝试使用Java和SQL创建登录系统,并且当前出现错误。

PreparedStatement s1 = con.prepareStatement("SELECT * FROM dbo.Logins WHERE Username=? AND Password=?");
s1.setString(1,loginUsername);
s1.setString(2, loginPassword.toString());
ResultSet rs = s1.executeQuery();

if(rs.next()){
    JOptionPane.showMessageDialog(null, "User Found");
}
else{
    JOptionPane.showMessageDialog(null, "Error");
}

When I run that snippet of code it always displays the else part of the if/else and im not sure why. 当我运行该代码段时,它始终显示if / else的else部分,而我不确定原因。 Thank in advance. 预先感谢。

Edit: Yes the DB is populated with users. 编辑:是的,数据库中填充了用户。 Edit2: I changed the function to require two string args. Edit2:我将函数更改为需要两个字符串args。

You must try to use while(rs.next()) instead of if(rs.next()) . 您必须尝试使用while(rs.next())而不是if(rs.next())

If that does not work, does your DB (which you have mentioned is populated), contain those particular values of username and password that you are searching for ? 如果这样不起作用,您的数据库(是否已填充您提到的数据库)是否包含要搜索的用户名和密码的特定值? If not, that may be the problem. 如果没有,那可能是问题所在。

If that too does not work, you may have closed some objects (eg. s1.close() or con.close() ) where you shouldn't. 如果那也行不通,则可能已关闭了一些不应该关闭的对象(例如s1.close()或con.close())。 For further help on that you will need to provide a more detailed flow of what you have done in the whole code. 要获得进一步的帮助,您将需要提供整个代码中所做操作的更详细的流程。

You can fetch number of rows available in your table for given query and if it is great than 0 it will execute User Found statement. 您可以获取表中可用于给定查询的行数,如果该行数大于0,它将执行User Found语句。

PreparedStatement s1 = con.prepareStatement("SELECT * FROM dbo.Logins WHERE Username=? AND Password=?");
s1.setString(1,loginUsername);
s1.setString(2, loginPassword.toString());
ResultSet rs = s1.executeQuery();

int rowCount = rs.last() ? rs.getRow() : 0; //Number of rows available in table for query

if(rowCount>0)
    JOptionPane.showMessageDialog(null, "User Found");
}
else{
    JOptionPane.showMessageDialog(null, "Error");
}
public void Login(String user, String pass) throws ClassNotFoundException, SQLException{ try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); MainWindow mw = new MainWindow(); String userName = "<USERNAME>"; String password = "<PASSWORD>"; String url = "<URL>"; Connection con = DriverManager.getConnection(url, userName, password); PreparedStatement s1 = con.prepareStatement("SELECT * FROM dbo.Logins WHERE Username=? AND Password=?"); s1.setString(1,user); s1.setString(2, pass); ResultSet rs = s1.executeQuery(); //int rowCount = rs.last() ? rs.getRow() : 0; //Number of rows System.out.println("Name: " + user + "\nPass: " + pass); if(rs.next()) JOptionPane.showMessageDialog(null, "User Found"); else{ JOptionPane.showMessageDialog(null, "Error"); } con.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM