在ResultSet中搜索特定值方法? [英] Search a ResultSet for a specific value method?

查看:127
本文介绍了在ResultSet中搜索特定值方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以使用ResultSet方法搜索ResultSet并检查它是否具有特定值/元素?

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

类似于 ArrayList.contains() 方法。

Similar to ArrayList.contains() method.

如果没有,则无需键入搜索方法,我会做一个:)

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

提前致谢。

推荐答案

不要在Java端进行搜索。这是不必要的缓慢和内存占用。你基本上接管了数据库的设计目标。让DB完成它的设计工作:借助SQL语言功能选择并返回所需的数据。

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.

开始学习 SQL WHERE 子句。例如,要检查用户名/密码是否匹配,请执行以下操作:

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

这篇关于在ResultSet中搜索特定值方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆