Search a ResultSet for a specific value method?

a 夏天 提交于 2019-12-22 08:37:30

问题


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.


回答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!
}



回答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.



来源:https://stackoverflow.com/questions/4720204/search-a-resultset-for-a-specific-value-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!