Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search a ResultSet for a specific value method?

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.

like image 992
anon Avatar asked Feb 26 '23 08:02

anon


2 Answers

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!
}
like image 186
BalusC Avatar answered Mar 05 '23 18:03

BalusC


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.

like image 32
Jim Garrison Avatar answered Mar 05 '23 17:03

Jim Garrison