gpt4 book ai didi

java - Java 中的 SQL。无法接收结果集

转载 作者:行者123 更新时间:2023-12-04 05:40:37 25 4
gpt4 key购买 nike

如果我的语法有误,希望您能纠正我。只有当 SELECT...WHERE 子句正确时,我才能在 Google 上进行检查。

我想找回 student_id 用于在另一个查询中使用它,但似乎我从中得到的只是“0”。

在java文件中:

rst = stmt.executeQuery("SELECT student_id FROM students WHERE name= 'to_delete'");
sid = rst.getInt("student_id");

to_delete 是此 java 文件接收的字符串作为必须返回的方法的参数 student_id .它确实包含正确的字符串(我检查过)。

表“students”包含字段:student_id、姓名、年份。我需要为名称“to_delete”返回 student_id。

我没有错误/异常,只是当我显示结果时,无论我输入什么名称,我都会看到 id: 0 。也许 rst.getString("column_name") 仅对 executeUpdate(...) 正确?

提前谢谢你!

最佳答案

调用 next()在你试图从中得到任何东西之前,先在结果集上。每次要读取一行时,都通过调用 next 方法来遍历 ResultSet。当 next 返回 false 时,您就完成了。

这是下一个方法的 API 文档:

boolean next() throws SQLException

Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown. If the result set type is TYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation will return false or throw an SQLException on a subsequent call to next.

If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.

Returns: true if the new current row is valid; false if there are no more rows Throws: SQLException - if a database access error occurs or this method is called on a closed result set

关于java - Java 中的 SQL。无法接收结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com