Search a ResultSet for a specific value method?

10,495

Solution 1

Don't do the search in Java side. That's unnecessarily slow and memory hogging. You're basically taking over the job the DB is designed for. Just let the DB do the job it is designed for: selecting and returning exactly the data you want with help of the SQL language powers.

Start learning the SQL WHERE clause. For example, to check if an username/password mathes, do:

connection = database.getConnection();
preparedStatement = connection.prepareStatement("SELECT * FROM user WHERE username=? AND password=md5(?)");
preparedStatement.setString(1, username); 
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
    // Match found!
} else {
    // No match!
}

Solution 2

Assuming you mean a SQL ResultSet, the answer is no, you have to write one. The JDBC driver usually won't retrieve all the rows at once (what if the query returned 1 million rows). You will have to read the rows and filter them yourself.

Share:
10,495
anon
Author by

anon

Updated on June 04, 2022

Comments

  • anon
    anon almost 2 years

    Is there a ResultSet method that I can use that would search through a ResultSet and check whether it has the specific value/element?

    Similar to ArrayList.contains() method.

    If there isn't, you don't need to type up a search method, I'll make one :)

    Thanks in advance.