如何获取ResultSet中的第一个元素
如何获取ResultSet中的第一个元素,代码如下:
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ResultSet 的第一个元素是您第一次执行
while (resultSet.next())
循环时使用的元素,因此它的内容用于构建 ResultSet 的第一个元素用户列表
。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 ofuserList
.请执行以下操作:
在此处检查 API: https://docs .oracle.com/javase/7/docs/api/java/sql/ResultSet.html
PS:
在我看来,最好替换:
用: (RETURNS OBJECT)
返回列表: (返回LIST)
要调用你的方法,请执行以下操作:
我建议这样做,因为我认为你不会有 2 个结果(列表是不必要的)。您期望得到 0 或 1 个结果!
Do:
Check the API here: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
PS:
In my opinion is better to replace:
With: (RETURNS OBJECT)
Return list: (RETURNS LIST)
To call your method do:
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!
ResultSet 不是 Java 集合,因此没有真正的第一个元素。您可以访问循环第一次迭代中第一个返回行的列
。
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
loop.
像这样:
事实是,您的数据库应该强制执行这样的想法:用户名/密码组合应该足以识别唯一的个人。查询应该返回唯一的个体或 null。如果您的数据库有此约束,您编写的代码将按此方式运行。
如果没有,您需要考虑您的架构设计。
Like this:
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.
使用
if(resultSet.first)...
而不是while(resultSet.next)...
但如果您想获取所有结果,然后从所有结果中选择第一个,这是一种方法:
并获取第一个结果
UsersTest firstUser = users.get(0);
use
if(resultSet.first)...
instead ofwhile(resultSet.next)...
but if you want to get all results and then choose the first from all this is a way to do so :
and to get the first result
UsersTest firstUser = users.get(0);