简体   繁体   English

Java SQL查询问题,ResultSet不包含任何内容

[英]Java SQL query problems, ResultSet doesn't contain anything

I am trying to do SQL with Java, this is my first time. 我正在尝试用Java做SQL,这是我第一次。 I am trying to select all of a table and put it in a ResultSet, but the ResultSet seems empty. 我试图选择所有表并将其放在ResultSet中,但ResultSet似乎为空。 The same query on the MySQL client does show results, which leaves me wondering what is wrong with my code. MySQL客户端上的相同查询确实显示结果,这让我想知道我的代码有什么问题。 It could either be the way the query is being done, or the way I am trying to get the data from the result set. 它可以是查询的完成方式,也可以是我尝试从结果集中获取数据的方式。

The query in the command line MySQL client... 在MySQL客户端的命令行中查询...

mysql> SELECT * FROM accounts.sites;
+----------+-----------------------+--------------------------+--------------+----------------+
| username | domain                | directory                | fpm-template | nginx-template |
+----------+-----------------------+--------------------------+--------------+----------------+
| cyrus    | cyrustheg.org         | /var/www/sites/cyrustheg | standard     | drupal         |
| cyrus    | tornews.org           | /var/www/sites/tornews   | standard     | standard       |
| cyrus    | oletf.org             | /var/www/sites/oletf     | standard     | standard       |
| taf      | theabsolutefinest.org | /var/www/sites/taf       | standard     | wordpress      |
| taf      | bitsplit.org          | /var/www/sites/bitsplit  | bitsplit     | bitsplit       |
+----------+-----------------------+--------------------------+--------------+----------------+
5 rows in set (0.00 sec)

I am using this code to build a resultSet with that query... 我正在使用此代码来构建具有该查询的resultSet ...

private void read() throws Exception {
    try {  
        Class.forName("com.mysql.jdbc.Driver");

        String dburl = "jdbc:mysql://" + dbHost + "/" + dbName +
                        "?user=" + dbUser + "&password=" + dbPass;

        connect = DriverManager.getConnection(dburl);

        resultSet = statement.executeQuery("SELECT * from accounts.sites");
    } catch (Exception e) {
        throw e;
    }
}

This code iterates through the resultSet, looking for every domain for every user. 此代码遍历resultSet,为每个用户查找每个域。 I know the problem is either in the former code or this or the former code, because I've added a print statement to the while loop, and it is never called. 我知道问题出在前一个代码或者这个或前一个代码中,因为我在while循环中添加了一个print语句,而且从不调用它。 This is why I conclude the resultSet is empty. 这就是为什么我断定resultSet为空。

public HashSet<String> listSites(String user)
        throws Exception, ExcSiteNosites {
    HashSet<String> list = new HashSet<String>();
    boolean exists = false;

    try { 
        read();

        while (resultSet.next()) {
            if (resultSet.getString("username").equals(user)) {
                exists = true;
                list.add(resultSet.getString("domain"));
            }
        }

        if (!exists) {
            throw new ExcSiteNosites(user);
        }
    } catch (Exception e) {
        throw e;
    } finally {
        resultSet.close();
        statement.close();
        connect.close();
    }

    return list;
}

The entire class: http://pastebin.com/0DSbFey7 The unit test for that class: http://pastebin.com/9UYLEeMB 全班: http//pastebin.com/0DSbFey7该课程的单元测试: http//pastebin.com/9UYLEeMB

I found the bug running that unit test, which makes the following output... 我发现错误运行单元测试,这使得以下输出...

Listing cyrus's sites...
Listing taf's sites...
java.lang.NullPointerException
    at database.Sites.listSites(Sites.java:96)
    at test.dbSites.listall(dbSites.java:28)
    at test.dbSites.main(dbSites.java:51)
java.lang.NullPointerException
    at database.Sites.listSites(Sites.java:96)
    at test.dbSites.listall(dbSites.java:28)
    at test.dbSites.main(dbSites.java:53)

The NullPointerException I think might be an unrelated problem. 我认为NullPointerException可能是一个无关的问题。 I've not done any Java in a while, and I've not worked that out either. 我有一段时间没有做过任何Java,而且我也没有这样做过。 Though the problem I want help with (unless related) is just that the ResultSet seems empty. 虽然我想要帮助的问题(除非相关)只是ResultSet似乎是空的。

You never initialize statement in your code, it's a private variable and you have no setter on it, so I think that you are not injecting it either. 你永远不会在你的代码中初始化statement ,它是一个私有变量而你没有setter,所以我认为你也没有注入它。

So, when you call read() , you will have a NullPointerException on 因此,当您调用read() ,您将遇到NullPointerException

resultSet = statement.executeQuery("SELECT * from accounts.sites");

this will be catched and thrown again by your catch . 这将被你的catch并再次抛出。

So, in your listSites() , you will catch it, and throw it again, and in the finally , you will have another NullPointerException on 所以,在你的listSites() ,你将捕获它,并再次抛出它,在finally ,你将有另一个 NullPointerException

statement.close();

and it's the one in your stack trace. 它是堆栈跟踪中的一个。

在使用它来执行Query()之前,您需要从连接创建一个语句....

Statement statement = connection.createStatement();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM