0
PreparedStatement preparedStatement = Connectionstring().prepareStatement(
            "Select Username from dbo.LoginDetails where Username = ? and Password =?");
  String User = tf_Fname.getText();
  String _Pass = new String(tf_Lname.getPassword());
        preparedStatement.setString(1, User);
        preparedStatement.setString(2, _Pass);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) 
        {
            System.out.println("Username is "+ resultSet.getString(1)+"Password is "+resultSet.getString(2));  
        }

没有

+"Password is "+resultSet.getString(2)

它工作正常,它正在从数据库中打印用户名,但它也会引发错误。

com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
4

3 回答 3

8
Select Username from .....................

select您在子句中只有一列。

将其更改为类似

  Select Username, yourpasswordcolumnname from......
于 2013-07-25T14:45:40.957 回答
7

您只从表中检索一列:

"Select Username from dbo.LoginDetails where Username = ? and Password =?");

试试这个:

 "Select Username, Password from dbo.LoginDetails where Username = ? and Password =?");

然后您的代码应该可以工作,在另一个相关说明中,永远不要将密码存储为“明文”,始终使用单向加密方法,并使用盐!

相关:您可能错误地存储了密码

于 2013-07-25T14:45:56.520 回答
2
select * from dbo.LoginDetails where Username = ? and Password =?

将为您提供所需的信息,以便您从中选择所有列,dbo.LoginDetails而不仅仅是一列Username,就像您现在一样

于 2013-07-25T14:46:33.587 回答