在 SQL 查询中使用 Java 中声明的变量

发布于 2024-10-04 04:59:52 字数 568 浏览 2 评论 0原文

k所以我使用Java中的Apache Derby创建了一个名为dictionaryDatabase的数据库。该数据库有一个名为dictionaryTable 的表,包含两列:wrongWord 和rightWord。 这是我到目前为止所得到的:

String keyWord = "test";

getWords = conn.prepareStatement(
        "SELECT wrongWord FROM dictionaryTable WHERE wrongWord = (?)");

现在,对于每个与关键字匹配的错误单词,我希望从错误单词值与关键字匹配的同一行中的正确单词列中检索值。

我已尝试以下操作,但这不起作用:

ResultSet existingKeywords = getWords.executeQuery();

while (existingKeywords.next()) {
 //Get rightWord column values here
}

我应该在这里做什么,我是否走在正确的轨道上?

谢谢!

k so I have created a database called dictionaryDatabase using Apache Derby in Java. The database has a table called dictionaryTable and contains two columns, wrongWord and rightWord.
Here's what I have so far:

String keyWord = "test";

getWords = conn.prepareStatement(
        "SELECT wrongWord FROM dictionaryTable WHERE wrongWord = (?)");

Now, for every wrongWord that does match keyWord, I wish to retrieve the value from the rightWord column in the same row in which the wrongWord value matched keyWord.

I have tried the following, but that doesn't work:

ResultSet existingKeywords = getWords.executeQuery();

while (existingKeywords.next()) {
 //Get rightWord column values here
}

What should I be doing here, and am I on the right track?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

心病无药医 2024-10-11 04:59:53

你基本上从这个SQL返回wrongWord

SELECT wrongWord FROM dictionaryTable WHERE wrongWord = (?)

为什么不创建一个类似的SQL

SELECT rightWord FROM dictionaryTable WHERE wrongWord = (?)

并且在while循环上,执行

while (existingKeywords.next()) {
 //Get rightWord column values here
    String rightWord = existsingKeywords.getString("rightWord");
}

显然,我正在吮吸拇指,因为我不知道什么dictionaryTable 包含列,因此这是一个假设。

另外,我还没有展示如何管理 JDBC 连接。

You are basically returning wrongWord from this SQL:

SELECT wrongWord FROM dictionaryTable WHERE wrongWord = (?)

Why not create a SQL like

SELECT rightWord FROM dictionaryTable WHERE wrongWord = (?)

And on the while loop, do

while (existingKeywords.next()) {
 //Get rightWord column values here
    String rightWord = existsingKeywords.getString("rightWord");
}

Clearly, I'm sucking thumb since I don't know what columns dictionaryTable contains so it's an assumption.

Also, I've not shown how manage JDBC connections.

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