在jsp中显示迭代器
我有以下代码:
@Override
public Iterator retrieve() throws SQLException {
List<PasalBean> pasalObject = new ArrayList<PasalBean>();
try {
Class.forName(dbDriver);
con = DriverManager.getConnection(url);
ps = con.createStatement();
rs = ps.executeQuery("select * from T_PASAL WHERE ID_PASAL = '" + id_pasal + "' ORDER BY ID_PASAL");
while (rs.next()) {
pasalObject.add(new PasalBean(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4)));
}
}catch (Exception e) {
System.out.println("Error Data : " + e.getMessage());
}
return (Iterator) pasalObject;
}
如何在JSP中显示它?
I have the following code:
@Override
public Iterator retrieve() throws SQLException {
List<PasalBean> pasalObject = new ArrayList<PasalBean>();
try {
Class.forName(dbDriver);
con = DriverManager.getConnection(url);
ps = con.createStatement();
rs = ps.executeQuery("select * from T_PASAL WHERE ID_PASAL = '" + id_pasal + "' ORDER BY ID_PASAL");
while (rs.next()) {
pasalObject.add(new PasalBean(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4)));
}
}catch (Exception e) {
System.out.println("Error Data : " + e.getMessage());
}
return (Iterator) pasalObject;
}
How can I display it in JSP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,这段代码将会失败。您无法将
List
转换为Iterator
。让它返回一个List
。然后,为了在 servlet 中显示它,您需要的是:
然后在 JSP 中:
最好将
retrieve()
方法放在单独的数据访问类 (DAO) 中,而不是直接在 servlet 中。First, this code will fail. You cannot cast a
List
toIterator
. Make it return aList
.Then, what you need in order to display it in a servlet is:
And then in the JSP:
It would be good to place the
retrieve()
method in a separate, data-access class (DAO), and not directly in the servlet.