结果集为空但SQL语句正确

huangapple go评论84阅读模式
英文:

ResultSet Empty but SQL Statement correct

问题

我正在尝试从我的表中选择一列,但结果集始终为空。
当我在IntelliJ数据库工具中尝试时,SQL语句按预期工作,甚至我使用System.out.print将其打印出来并放入SQL查询工具中时也可以正常工作。

由于结果集为空,我一直收到错误消息"ResultSet is not positioned correctly"(我尝试写rs.next但它不起作用)

以下是代码:

private final String HELP_STATEMENT_STR =
        "SELECT a1atnr " +
        "FROM market.a1 INNER JOIN market.asol ON a1atnr = asoatnr " +
        "WHERE asoatikid = ?;";

private PreparedStatement HELP_STATEMENT;

public List<Long> deleteArticle(String ext_article_nr) throws SQLException {
    if (HELP_STATEMENT == null){
        HELP_STATEMENT = database.getConnection().prepareStatement(HELP_STATEMENT_STR);
    }
    HELP_STATEMENT.setString(1, ext_article_nr);
    System.out.println(HELP_STATEMENT.toString());
    ResultSet rs = HELP_STATEMENT.executeQuery();
    if (!rs.next()){
        System.out.println("ResultSet is empty");
    } else {
        System.out.println("ResultSet is not empty");
    }
    String nr = rs.getString("a1atnr");
}

我还尝试使用普通的Statement而不是prepareStatement,但结果完全相同。

任何帮助将不胜感激。

英文:

I'm trying to SELECT a column from my table, but the ResultSet is always empty.
The SQL Statement works as expected when I try it in the IntelliJ database tool and I even printed it out using System.out.print and put it into the sql query tool and it works.

Because the ResultSet is empty I keep getting the error "ResultSet is not positioned correctly" (I tried writing rs.next but it didn't work)

Here is the code

private final String HELP_STATEMENT_STR =
                &quot;SELECT a1atnr &quot; +
                &quot;FROM market.a1 INNER JOIN market.asol ON a1atnr = asoatnr &quot; +
                &quot;WHERE asoatikid = ?;&quot;;
    
private PreparedStatement HELP_STATEMENT;

public List&lt;Long&gt; deleteArticle(String ext_article_nr) throws SQLException {
    if (HELP_STATEMENT == null){
        HELP_STATEMENT = database.getConnection().prepareStatement(HELP_STATEMENT_STR);
    }
    HELP_STATEMENT.setString(1, ext_article_nr);
    System.out.println(HELP_STATEMENT.toString());
    ResultSet rs = HELP_STATEMENT.executeQuery();
    if (!rs.next()){
        System.out.println(&quot;ResultSet is empty&quot;);
    } else {
        System.out.println(&quot;ResultSet is not empty&quot;);
    }
    String nr = rs.getString(&quot;a1atnr&quot;);
}

I also tried using a normal Statement instead of a prepareStatement but the results are exactly the same

Any help would be appreciated

答案1

得分: 2

你可以尝试在FROM子句中为这两个表添加别名,并在ON子句中使用它们,就像这样:

"SELECT a1atnr " +
"FROM market.a1 a1Table INNER JOIN market.asol asolTable " +
"ON a1Table.a1atnr = asolTable.asoatnr " +
"WHERE asoatikid = ?;";

```

<details>
<summary>英文:</summary>

could you try adding aliases for the two tables in your FROM clause and use them in the ON clause like this:

"SELECT a1atnr " +
"FROM market.a1 a1Table INNER JOIN market.asol asolTable " +
"ON a1Table.a1atnr = asolTable.asoatnr " +
"WHERE asoatikid = ?;";


</details>



huangapple
  • 本文由 发表于 2023年7月12日 21:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定