java 索引 2 超出范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17861289/
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
The index 2 is out of range
提问by anuj
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.
回答by kosa
Select Username from .....................
You have only one column in select
clause.
您在select
子句中只有一列。
Change it to something like
将其更改为类似
Select Username, yourpasswordcolumnname from......
回答by jsedano
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
相关:您可能未正确存储密码
回答by mistahenry
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
将为您提供所需的信息,以便您选择所有列,dbo.LoginDetails
而不仅仅是一列Username
,就像您现在一样