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.