gpt4 book ai didi

java - JDBC 的 rs.getString() 不会返回查询结果的值

转载 作者:行者123 更新时间:2023-12-01 00:14:37 24 4
gpt4 key购买 nike

我对 JDBC 的 rs.getString("column_name") 有一些问题,基本上它不会分配从查询结果中收到的值,我有一个字符串 ris 应该获取行名来自 rs.getString,为了简化问题,我正在使用 ris 并且我的查询只返回一行。这是代码:

         //It returns null, or any other values I use to initialize the variable 
String ris=null;

q = "SELECT DISTINCT nome FROM malattia WHERE eta='" + age + "' AND sesso='" + sexstr + "' AND etnia='" + etniastr + "' AND sintomi IN(" + tes + ")";

ResultSet rs = st.executeQuery(q);
if (!rs.last()) {
ris = "no";
}
else {

//This is the place where I'm having problems
while(rs.next()){
//ris is supposed to get the name of the query result having column "nome"
ris=rs.getString("nome");

}
}


conn.close();

} catch (Exception e) {
ris = e.toString();
}

return ris;

我简化了代码,因此很容易将注意力集中在问题所在。提前致谢!

最佳答案

if (rs.last())
while (rs.next())

那是行不通的,因为在调用 last 之后,您位于最后一行,而 next 将始终返回 false (它会返回 true 并带你到下一行(如果还有的话)。

请使用带有绑定(bind)变量的准备好的语句!

最后关闭 ResultSet 和 Connection(或使用 Jakarta Commons DbUtils)。

关于java - JDBC 的 rs.getString() 不会返回查询结果的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7268942/

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