EJB中得到的Collection,如何得到它的元素.....在線等待.......... list all = new ArrayList(得到的collection) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Iterator it = home.findAll().iterator();while(it.hasNext()) { remote = (EJBRemote)it.next(); System.out.println(remote.getId());}这是我写的测试程序,希望能给你点启发 得到的結果為什麼是:user.ejb.userEJB_25585_EOImp1_WLStub@5c414db1user.ejb.userEJB_25585_EOImp1_WLStub@9c8417cfuser.ejb.userEJB_25585_EOImp1_WLStub@8a1623bcuser.ejb.userEJB_25585_EOImp1_WLStub@7c41776buser.ejb.userEJB_25585_EOImp1_WLStub@5c5b8651ejb class:public Collection ejbFindAll() throws RemoteException{ Collection result; try{ result=finduser(); }catch(Exception ex){ throw new EJBException("fail"); }}private Collection finduser() throws SQLException{ String str="select name from table1"; PreparedStatement ps=con.prepareStatement(str); try{ ResultSet rs=ps.executeQuery(); ArrayList al=new ArrayList(); while(rs.next()){ String name=rs.getString(1); al.add(name); } return al; }finally{ ps.close(); }} 忘记告诉你要用remote.getId().toString(),否则都是句柄 String name=(String)rs.getString(1); 不对,是因为Collection里的元素是Object diaopeng(遥远) 我用你是測試得到的都是null, 用ArrayList得到以上句柄 假设你的EJB的远程接口是Userjava.util.Collection c=home.findAll();User[] users=new User[c.size()];c.toArray(users);users就是你找到的所有远程对象 偶也才学,不知对否?<% page language = "java" %><% import = "users.*,database.*,java.util.*" %><% UserWeb userWeb = new UserWeb(); AllayList arrayList = new AllayList(); try{ arrayList = userWeb.getAllUser(); } catch(Exception e){ out.println(e.getMessage()); }%><html> <head><title>全部注册用户:</title></head> <body> <center> <table border = "1"> <tr> <td>账号</td> <td>名称</td> <td>Email</td> <td>删除</td> </tr> <tr> <% Iterator i = arrayList.itertor(); while(i.hasnext()){ UserModel userModel = (UserModel)i.next(); %> <td><% = userModel.getUserID() %></td> <td><% = userModel.getUserName() %></td> <td><% = userModel.getUserEmail() %></td> <td><a herf = "deleteuser.jsp?userid = <% =userModel.getUserID() %>">删除</td> </tr> <% } %> </table> </center> </body></html> 小弟新手 请教关于服务器端验证码 验证的问题。。。。 struts2 的标签老是报错The requested resource () is not available. JSP包含Servlet出错????? 生成收据的问题,请高手解答,能给出代码最好! 多IP下配置tomcat达到虚拟主机的效果 自己调用main方法和jvm调用有区别吗? 入门级问题,为什么要用hibernate? 大家对培训是怎么看的? 我需要继承HttpSession吗? 50分求助 关于用jdbcodbc连数据库的简单问题 如何实现点击-下载文件?
while(it.hasNext()) {
remote = (EJBRemote)it.next();
System.out.println(remote.getId());
}
这是我写的测试程序,希望能给你点启发
user.ejb.userEJB_25585_EOImp1_WLStub@5c414db1
user.ejb.userEJB_25585_EOImp1_WLStub@9c8417cf
user.ejb.userEJB_25585_EOImp1_WLStub@8a1623bc
user.ejb.userEJB_25585_EOImp1_WLStub@7c41776b
user.ejb.userEJB_25585_EOImp1_WLStub@5c5b8651
ejb class:
public Collection ejbFindAll() throws RemoteException
{
Collection result;
try{
result=finduser();
}catch(Exception ex){
throw new EJBException("fail");
}}private Collection finduser() throws SQLException
{
String str="select name from table1";
PreparedStatement ps=con.prepareStatement(str);
try{
ResultSet rs=ps.executeQuery();
ArrayList al=new ArrayList();
while(rs.next()){
String name=rs.getString(1);
al.add(name);
}
return al;
}finally{
ps.close();
}
}
java.util.Collection c=home.findAll();
User[] users=new User[c.size()];
c.toArray(users);users就是你找到的所有远程对象
<% import = "users.*,database.*,java.util.*" %>
<%
UserWeb userWeb = new UserWeb();
AllayList arrayList = new AllayList();
try{
arrayList = userWeb.getAllUser();
}
catch(Exception e){
out.println(e.getMessage());
}
%>
<html>
<head><title>全部注册用户:</title></head>
<body>
<center>
<table border = "1">
<tr>
<td>账号</td>
<td>名称</td>
<td>Email</td>
<td>删除</td>
</tr>
<tr>
<%
Iterator i = arrayList.itertor();
while(i.hasnext()){
UserModel userModel = (UserModel)i.next();
%>
<td><% = userModel.getUserID() %></td>
<td><% = userModel.getUserName() %></td>
<td><% = userModel.getUserEmail() %></td>
<td><a herf = "deleteuser.jsp?userid = <% =userModel.getUserID() %>">删除</td>
</tr>
<%
}
%>
</table>
</center>
</body>
</html>