分配结果集时未发现数据异常

No Data found exception while assigning the result set

我正在编写代码来获取列中值的计数,并使用此结果我需要在我的 swing 中创建一个 jtable。现在,当我直接使用 sysout 打印结果时,没有任何问题,数据正在直接打印。但是当我开始分配变量时,会抛出异常。下面是我的代码。

//to run a query and build user
public List<User> searchUser(String USERNAME, String action) throws Exception {
        List<User> list = new ArrayList<>();

        PreparedStatement pst = null;
        ResultSet rs = null;

        try {

                USERNAME = "%" + USERNAME + "%";
                String query = "select count(*) as cnt, USERNAME from [Sheet1$] GROUP BY USERNAME";
                pst = myConn.prepareStatement(query);
                // pst.setString(1, USERNAME);
                rs = pst.executeQuery();
                String sum = null;
                while (rs.next()) {
                    User tempUser = convertRowToUser(rs);
                    list.add(tempUser);
                }
                System.out.println(sum);

            return list;
        } finally {
            close(pst, rs);
        }
    }

//Convert row data to user data

private User convertRowToUser(ResultSet rs) throws SQLException {
        System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
        int resultInt = rs.getInt(1);
        String lastName = rs.getString(2);
        System.out.println(lastName + "\t" + resultInt);
        User tempUsers = new User(lastName, resultInt);

        return tempUsers;
    }

//My main method

public static void main(String[] args) throws Exception {
        UsersDAO dao = new UsersDAO();
        dao.searchUser("abc", "count");
    }

当我注释掉以下行时,打印 last nameresultInt 和 return null 它正在控制台中打印数据。

    int resultInt = rs.getInt(1);
    String lastName = rs.getString(2);
    System.out.println(lastName + "\t" + resultInt);
    User tempUsers = new User(lastName, resultInt);

我添加了 try-catch 块,如下所示,以查看堆栈跟踪,它显示了以下结果。

private User convertRowToUser(ResultSet rs) {
        try {
            System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String lastName = null;
        try {
            lastName = rs.getString(2);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int resultInt = 0;
        try {
            resultInt = rs.getInt(1);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(lastName + "\t" + resultInt);

        User tempUsers = new User(lastName, resultInt);

        return null;
    }

例外情况如下

16  abc
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
    at org.swing.classes.UsersDAO.convertRowToUser(UsersDAO.java:154)
    at org.swing.classes.UsersDAO.searchUser(UsersDAO.java:103)
    at org.swing.classes.UsersDAO.main(UsersDAO.java:193)
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
    at org.swing.classes.UsersDAO.convertRowToUser(UsersDAO.java:161)
    at org.swing.classes.UsersDAO.searchUser(UsersDAO.java:103)
    at org.swing.classes.UsersDAO.main(UsersDAO.java:193)
null    0
8   edf
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
    at org.swing.classes.UsersDAO.convertRowToUser(UsersDAO.java:154)
null    0
8   rgtd
    at org.swing.classes.UsersDAO.searchUser(UsersDAO.java:103)
    at org.swing.classes.UsersDAO.main(UsersDAO.java:193)
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
    at org.swing.classes.UsersDAO.convertRowToUser(UsersDAO.java:161)
    at org.swing.classes.UsersDAO.searchUser(UsersDAO.java:103)
    at org.swing.classes.UsersDAO.main(UsersDAO.java:193)
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
    at org.swing.classes.UsersDAO.convertRowToUser(UsersDAO.java:154)
    at org.swing.classes.UsersDAO.searchUser(UsersDAO.java:103)
    at org.swing.classes.UsersDAO.main(UsersDAO.java:193)
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
    at org.swing.classes.UsersDAO.convertRowToUser(UsersDAO.java:161)
    at org.swing.classes.UsersDAO.searchUser(UsersDAO.java:103)
    at org.swing.classes.UsersDAO.main(UsersDAO.java:193)
null    0
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
    at org.swing.classes.UsersDAO.convertRowToUser(UsersDAO.java:154)
    at org.swing.classes.UsersDAO.searchUser(UsersDAO.java:103)
    at org.swing.classes.UsersDAO.main(UsersDAO.java:193)
8   rtfgt
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
    at org.swing.classes.UsersDAO.convertRowToUser(UsersDAO.java:161)
    at org.swing.classes.UsersDAO.searchUser(UsersDAO.java:103)
    at org.swing.classes.UsersDAO.main(UsersDAO.java:193)
null    0
null

如果我只打印值(不分配),输出如下。

16  abc
8   edf
8   rgtd
8   rtfgt
null

请告诉我哪里出错了,我该如何解决。

谢谢

这是 JDBC-用于 MS Access 的 ODBC 桥驱动程序的常见警告(错误?)。您应该只为一个结果集行读取一次数据并将其存储在局部变量中:

private User convertRowToUser(ResultSet rs) throws SQLException {
    int resultInt = rs.getInt(1);
    String lastName = rs.getString(2);
    System.out.println(resultInt + "\t" + lastName);
    User tempUsers = new User(lastName, resultInt);
    return tempUsers;
}