Reputation: 21
How to get the first element in ResultSet in the following code:
public List getUserTest(String username, String password) {
List userList = new ArrayList();
Connection connection = null;
String rstring = null;
Statement stmt = null;///*******
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
stmt = connection.createStatement();///*******
resultSet = stmt.executeQuery("SELECT * FROM users WHERE (username)='" + username + "'"
+ "AND (password)='" + password + "'");///*******
while (resultSet.next()) {
UsersTest user = new UsersTest();
user.setId(resultSet.getString("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
userList.add(user);
}
} catch (SQLException e) {
System.err.println(e);
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (stmt != null) {
stmt.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
}
}
return userList;
}
Upvotes: 2
Views: 44784
Reputation: 7101
Do:
resultSet.first()
Check the API here: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
PS:
In my opinion is better to replace:
while (resultSet.next()) {
UsersTest user = new UsersTest();
user.setId(resultSet.getString("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
userList.add(user);
}
With: (RETURNS OBJECT)
if (!rs.next())
//No user found.
else {
UsersTest user = new UsersTest();
resultSet.first();
user.setId(resultSet.getString("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
}
return user;
Return list: (RETURNS LIST)
List userList = new ArrayList();
if (!rs.next())
//No user found.
else {
UsersTest user = new UsersTest();
resultSet.first();
user.setId(resultSet.getString("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
userList.add(user) //it will only add one element
}
return userList;
To call your method do:
UsersTest u = new UserTest();
List l = getUserTest(username, password)
if(l.size > 0)
u = l.get(0);
else
System.out.println("Cannot find user");
I suggest that because I do not think that you will have 2 results(List is unnecessary). You expect to have 0 or 1 result!
Upvotes: 5
Reputation: 447
use if(resultSet.first)...
instead of while(resultSet.next)...
but if you want to get all results and then choose the first from all this is a way to do so :
List<UsersTest> users = new ArrayList<UsersTest>();
while(resultSet.next){
UsersTest user = new UsersTest();
/*
* All your Setters but you should create a constructor with fields
*/
users.add(user);
}
and to get the first result UsersTest firstUser = users.get(0);
Upvotes: 0
Reputation: 308743
Like this:
List users = List getUserTest(username, password);
User user = ((users.size() > 0 ? users.get(0) : null);
The truth is that your database ought to enforce the idea that a username/password combination ought to be enough to identify a unique individual. A query should either return a unique individual or null. Your code as written will behave this way if your database has this constraint.
If it doesn't, you need to think about your schema design.
Upvotes: 0
Reputation: 1283
A ResultSet is not a Java collection so there is no real first element. You have access to the columns of the first returned row in the first iteration of the
<!-- language: lang-java -->
while(resultSet.next())
{
...
}
loop.
Upvotes: 0
Reputation: 128779
The first element of the ResultSet is the one that you're working with the first time you execute the while (resultSet.next())
loop, and therefore its contents are used to build the first element of userList
.
Upvotes: 5