英文:
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 =
"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");
}
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论