`
anysky131
  • 浏览: 171737 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Vector使用

    博客分类:
  • Java
阅读更多
Vector 是个处理结果集的类。使用的方法是:
在想要从一个方法中往外传递多个参数时,就需要使用Vector类了。
public Vector select() throws Exception
{
Vector vector=new Vector();
conn=new DatabaseConnection();
sql="select * from note";
try {
pstmt=conn.getConnection().prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
{
Note note=new Note();
note.setId(rs.getInt(1));
note.setTitle(rs.getString(2));
note.setAuthor(rs.getString(3));
note.setContent(rs.getString(4));
vector.add(note);

}
} catch (RuntimeException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return vector;

}
本例中,我想要把id,title,author,content这几个参数从数据库中找出来,然后把它们都传出去。就得用Vector对象了。使用一个Note bean把结果集放进去(set方法)。然后使用一句话:vector.add(note);就可以把note中的所有参数都放进Vector中,一下子都传出去。
然后就是一个接收的问题了。
<jsp:useBean id="notedao" class="Notedao" scope="page" />
<jsp:useBean id="note" class="Note" scope="page" />
Vector vector=notedao.select();
int size=vector.size();
for(int i=0;i<size;i++)
{
note=(Note)vector.elementAt(i);
%>
<TR>
<TD><%=note.getId()%></TD>
<TD><%=note.getTitle()%></TD>
<TD><%=note.getAuthor()%></TD>
<TD><%=note.getContent()%></TD>
<TD><a href="look.jsp">查看</a></TD>
<TD><a href="update.jsp">修改</a></TD>
<TD><a href="delet.jsp">删除</a></TD>
</TR>
在这里是使用jsp页面显示出来结果集。用Notedao对象的select()方法,把vector接收过来。现在参数都在vector中了。然后调用size()方法,算出有多少个结果集,以便在下面for()中一个一个显示出来。使用一个elementAt(int i)方法把每个记录都再放回note中。使用get()方法一个一个传出来就可以了。
很简单的。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics