如何用where子句指定hibernate select查询?占位符

时间:2013-05-20 11:45:01

标签: java hibernate web-applications prepared-statement

这是我的代码示例:

String hqlquery="select stName,stCode from StGroup where stName=?";
PreparedStatement ps=con.prepareStatement(hqlquery);
ps.setString(1,stname);//here stname am passing through one function
ps.executeQuery();

此代码为我提供了所选的记录数。 但我想要与“stName”匹配的记录列表(包含stName,stCode)(在where子句中)。 如何获取列表。我可以使用带有hibernate的ResultSet。

1 个答案:

答案 0 :(得分:0)

PreparedStatement.executeQuery()返回一个ResultSet,其中包含与WHERE子句匹配的行,每行包含您在SELECT子句中放置的字段。使用ResultSet的first()last()next()方法,您可以访问所有这些信息:结果集位于当前行,您可以通过类似方法访问字段值getString(columnName)getInt(columnName),具体取决于列的类型。