Reputation: 7
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));
}
Without
+"Password is "+resultSet.getString(2)
it works fine, it is printing username from the database but with that it also throws an error.
com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
Upvotes: 0
Views: 24701
Reputation: 8724
select * from dbo.LoginDetails where Username = ? and Password =?
will get you the information you need so that you are selecting all columns from dbo.LoginDetails
instead of just one column, Username
, like you are right now
Upvotes: 2
Reputation: 4216
You are only retrieving one column from the table:
"Select Username from dbo.LoginDetails where Username = ? and Password =?");
Try this:
"Select Username, Password from dbo.LoginDetails where Username = ? and Password =?");
Then your code should work, on another related note, NEVER store passwords as "clear text", always use a one way encryption method, and use a salt!
Related: You're Probably Storing Passwords Incorrectly
Upvotes: 7
Reputation: 66637
Select Username from .....................
You have only one column in select
clause.
Change it to something like
Select Username, yourpasswordcolumnname from......
Upvotes: 8