如何获取ResultSet中的第一个元素

发布于 2024-11-26 11:30:05 字数 1293 浏览 0 评论 0原文

如何获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

他是夢罘是命 2024-12-03 11:30:05

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 of userList.

回首观望 2024-12-03 11:30:05

请执行以下操作:

resultSet.first()

在此处检查 API: https://docs .oracle.com/javase/7/docs/api/java/sql/ResultSet.html

PS:

在我看来,最好替换:

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);
    }

用: (RETURNS OBJECT)

if (!rs.next())
  //No user found.
else {
  UsersTest user = new UsersTest();
  resultSet.first();
  user.setId(resultSet.getString("id"));
  user.setUsername(resultSet.getString("username"));
  user.setPassword(resultSet.getString("password"));
}

return user;

返回列表: (返回LIST)

List userList = new ArrayList();

if (!rs.next())
  //No user found.     
else {
  UsersTest user = new UsersTest();
  resultSet.first();
  user.setId(resultSet.getString("id"));
  user.setUsername(resultSet.getString("username"));
  user.setPassword(resultSet.getString("password"));
  userList.add(user) //it will only add one element
}

return userList;

要调用你的方法,请执行以下操作:

UsersTest u = new UserTest();
List l = getUserTest(username, password)

if(l.size > 0)
  u = l.get(0);
else
  System.out.println("Cannot find user"); 

我建议这样做,因为我认为你不会有 2 个结果(列表是不必要的)。您期望得到 0 或 1 个结果!

Do:

resultSet.first()

Check the API here: https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

PS:

In my opinion is better to replace:

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);
    }

With: (RETURNS OBJECT)

if (!rs.next())
  //No user found.
else {
  UsersTest user = new UsersTest();
  resultSet.first();
  user.setId(resultSet.getString("id"));
  user.setUsername(resultSet.getString("username"));
  user.setPassword(resultSet.getString("password"));
}

return user;

Return list: (RETURNS LIST)

List userList = new ArrayList();

if (!rs.next())
  //No user found.     
else {
  UsersTest user = new UsersTest();
  resultSet.first();
  user.setId(resultSet.getString("id"));
  user.setUsername(resultSet.getString("username"));
  user.setPassword(resultSet.getString("password"));
  userList.add(user) //it will only add one element
}

return userList;

To call your method do:

UsersTest u = new UserTest();
List l = getUserTest(username, password)

if(l.size > 0)
  u = l.get(0);
else
  System.out.println("Cannot find user"); 

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!

孤云独去闲 2024-12-03 11:30:05

ResultSet 不是 Java 集合,因此没有真正的第一个元素。您可以访问循环第一次迭代中第一个返回行的列

<!-- language: lang-java -->
while(resultSet.next())
{
 ...
}

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

<!-- language: lang-java -->
while(resultSet.next())
{
 ...
}

loop.

罗罗贝儿 2024-12-03 11:30:05

像这样:

List users = List getUserTest(username, password);
User user = ((users.size() > 0 ? users.get(0) : null);

事实是,您的数据库应该强制执行这样的想法:用户名/密码组合应该足以识别唯一的个人。查询应该返回唯一的个体或 null。如果您的数据库有此约束,您编写的代码将按此方式运行。

如果没有,您需要考虑您的架构设计。

Like this:

List users = List getUserTest(username, password);
User user = ((users.size() > 0 ? users.get(0) : null);

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.

我要还你自由 2024-12-03 11:30:05

使用 if(resultSet.first)... 而不是 while(resultSet.next)...
但如果您想获取所有结果,然后从所有结果中选择第一个,这是一种方法:

List<UsersTest> users = new ArrayList<UsersTest>();
while(resultSet.next){
   UsersTest user = new UsersTest();
   /*
      * All your Setters but you should create a constructor with fields
   */
   users.add(user);
}

并获取第一个结果 UsersTest firstUser = users.get(0);

use if(resultSet.first)... instead of while(resultSet.next)...
but if you want to get all results and then choose the first from all this is a way to do so :

List<UsersTest> users = new ArrayList<UsersTest>();
while(resultSet.next){
   UsersTest user = new UsersTest();
   /*
      * All your Setters but you should create a constructor with fields
   */
   users.add(user);
}

and to get the first result UsersTest firstUser = users.get(0);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文