tut*_*tak 0 java mysql sql web-services jdbc
我对JDBC的rs.getString("column_name")有一些问题,基本上它不会分配从查询结果中收到的值,我有一个String 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;
Run Code Online (Sandbox Code Playgroud)
我简化了代码,因此很容易关注问题所在.提前致谢!
if (rs.last())
while (rs.next())
Run Code Online (Sandbox Code Playgroud)
这是行不通的,因为在你调用之后last,你就在最后一行并且next总是会返回false(true如果剩下一行, 它会返回并带你到下一行).
请使用带有绑定变量的预准备语句!
最后关闭ResultSet和Connection(或使用Jakarta Commons DbUtils).
| 归档时间: |
|
| 查看次数: |
3636 次 |
| 最近记录: |