📄 关于记录分页中的问题.htm
字号:
<BR>
CachedRowSet提供的最令人兴奋的功能就是分页功能。以前程序员很头疼的问题就是怎么处理数据分页而不影响性能,现在有了CachedRowSet一切都变得那么简单,请看下面的代码:
<BR> CachedRowSet crs=new CaehedRowSetImpl();
<BR> crs.setUrl(“jdbc:mydql://localhost:3306/test”); <BR>
crs.setUsername(“root”); <BR> crs.setPassword(“”);
<BR> crs.setCommand(“select * from table1”);
<BR> crs.setPageSize(5); <BR> crs.execute();
<BR> while(crs.nextPage()) <BR> while(crs.next())
<BR> System.out.println(crs.getInt(“id”+”\t\t”+crs.getString(“name”));
<BR>
我们在crs.execute()之前设置每页的数据行数,那么Reader读取数据的时候就只读指定的行数的数据,这样我们就避免了一次读取所有数据再进行分页操作。是不是很简单呢?
<BR> <BR> JoinRowSet接口: <BR>
这个接口可以提供我们在无连接的状态下直接对结果集进行Join。下面的代码提供了JoinRowSet的实现: <BR>
CachedRowSet crs1=new CaehedRowSetImpl(); <BR>
crs1.setUrl(“jdbc:mydql://localhost:3306/test”); <BR>
crs1.setUsername(“root”); <BR> crs1.setPassword(“”);
<BR> crs1.setCommand(“select * from table1”);
<BR> crs1.execute(); <BR> CachedRowSet crs2=new
CaehedRowSetImpl(); <BR>
crs2.setUrl(“jdbc:mydql://localhost:3306/test”); <BR>
crs2.setUsername(“root”); <BR> crs2.setPassword(“”);
<BR> crs2.setCommand(“select * from table2”);
<BR> crs2.execute(); <BR> JoinRowSet jrs=new
JoinRowSetImpl(); <BR> jrs.addRowSet(crs1,”id”); <BR>
jrs.addRowSet(crs2,”id”); <BR> while(jrs.next()) <BR>
System.out.println(jrs.getInt(“id”)+”\t\t”+jrs.getString(“name”)+”\t\t”+jrs.getString(“info”);
<BR> 这段代码的作用和执行select * from table1
inner join table2 on
table1.id=table2.id语句得到的结果集是一样的。但是我个人认为与其这样复杂地使用JoinRowSet,不如直接使用这条Join语句来得到CachedRowSet。
<BR> 默认的Join是inner join的,接口还支持cross join,full
join,left outer join和right outer
join,我们通过setJoinType()方法来修改连接类型,当然这还是需要数据库的支持。还有一个值得注意的地方就是,在这个例子里我连接的列在两个表里面都叫id,那么我们取数据的时候就使用id这个名字,那如果两列的名字不一样呢?系统就会为这个连接列取一个默认的名字叫做”
MergedCol”。 <BR> FilteredRowSet接口: <BR> .NET
的ADO.NET支持获取结果集使用一定的条件过滤从而得到不同的结果,那现在jdk1.5也能做到了,FilterRowSet接口让我们可以灵活地定义过滤条件达到不同的效果。Javax.sql.rowset包里面的Predicate接口就是这个过滤器,我们通过实现这个接口定义过滤条件,下面是示意代码:
<BR> public class Filter implements
Predicate { <BR> private int min;
<BR> private int max; <BR> private
String colName; <BR> public Filter (int
min ,int max ,String colName) {
<BR> this.min=min; this.max=max; this.colName=colName;
<BR> } <BR> public boolean evaluate
(RowSet rs) { <BR> CachedRowSet
crs=(CachedRowSet)rs; <BR>
if((crs.getInt(colName)>min)&& (crs.getInt(colName)<max))
<BR> return true; <BR> else return
false; <BR> } <BR> } <BR>
下面我们就使用这个过滤器来过滤掉id不在min和max之间的数据: <BR> FilteredRowSet
frs=new FilteredRowSet(); <BR> …… <BR>
frs.setCommand(“select * from table1”); <BR>
frs.execute();//先获取所有数据; <BR> frs.setFilter(new
Filter(1,20,”id”);//过滤掉id值不在1和20之间的数据; <BR>
因为实现Prdicate接口里面的方法很灵活,所以我们就能很灵活地设置过滤条件,我们就可以只通过一条语句得到不同的结果。 <BR>
WebRowSet接口: <BR>
XML因为其平台无关性越来越受到开发者的青睐,它也是数据持久化的一个不错的选择,WebRowSet封装了读写XML的方法,我们就可以轻松地把数据库的数据持久化到XML或者从XML读取数据写入数据库。
<BR> 写入到XML文件的方法是wrs.writeXML(new
FileOutputStream(“data.xml”));它执行的结果是把内存中的数据写入当前目录里面的data.xml文件中。在这个xml文件里面记录了三类数据:
<BR> properties:包括setXXX()方法所有的属性,没有设置的就是默认属性 <BR>
metadata:包括数据库表的相关元数据,对应ResultSetMetaData里的信息 <BR> data:结果集的全部数据
<BR> 从xml文件读取数据装载到RowSet的方法是readXML(…);只要是按照规范的格式写的xml都可以装载进来。
<BR> <BR> 结语: <BR>
五个RowSet接口里面分别都还有一些方法,由于篇幅有限,我只列出了其中一些典型的方法,希望本文对大家进一步学习jdk1.5有所帮助!
<BR> <BR> 以后我可能陆陆续续再发一些我自己的学习的收获,而且希望大家光临我的blog
<BR> blog.csdn.net/jFresH_MaN <BR> <A
href="http://topic.csdn.net/t/20050423/20/3961523.html#">Top</A></P>
<H4><STRONG>相关问题</STRONG></H4>
<DIV class=relation>
<UL>
<LI><A href="http://topic.csdn.net/t/20060220/11/4565553.html"
rel=external>GridView中如何在分页出显示记录总数?</A>
<LI><A href="http://topic.csdn.net/t/20020214/01/529126.html"
rel=external>记录集的分页</A>
<LI><A href="http://topic.csdn.net/t/20020430/10/690812.html"
rel=external>记录集怎么分页?</A>
<LI><A href="http://topic.csdn.net/t/20021128/20/1214277.html"
rel=external>记录分页问题?!</A>
<LI><A href="http://topic.csdn.net/t/20050311/11/3842592.html" rel=external>如何
将 DataTable 中的记录分页显示在 DataList 控件中呢?? 谢谢</A>
<LI><A href="http://topic.csdn.net/t/20010903/00/268428.html"
rel=external>如何在数据窗口中对记录进行分页显示?`</A>
<LI><A href="http://topic.csdn.net/t/20060310/21/4606937.html"
rel=external>水晶报表分页问题——如何按记录分页</A>
<LI><A href="http://topic.csdn.net/t/20020816/21/947401.html"
rel=external>不要一次把所有记录取的记录集中,实现分页。怎样实现?</A>
<LI><A href="http://topic.csdn.net/t/20050612/17/4077025.html"
rel=external>关于分页问题!错误:表中只有一条记录时,不能显示该条记录!</A>
<LI><A href="http://topic.csdn.net/t/20050615/13/4083914.html"
rel=external>datagrid中的分页</A> </LI></UL></DIV></DIV></DIV>
<DIV id=sidebar>
<H3>关键词</H3>
<DIV>
<UL>
<LI><A href="http://tag.csdn.net/tag/接口/" rel=external>接口</A>
<LI><A href="http://tag.csdn.net/tag/连接/" rel=external>连接</A>
<LI><A href="http://tag.csdn.net/tag/数据/" rel=external>数据</A>
<LI><A href="http://tag.csdn.net/tag/数据库/" rel=external>数据库</A>
<LI><A href="http://tag.csdn.net/tag/代码/" rel=external>代码</A>
<LI><A href="http://tag.csdn.net/tag/crs/" rel=external>crs</A>
<LI><A href="http://tag.csdn.net/tag/rowset/" rel=external>rowset</A>
<LI><A href="http://tag.csdn.net/tag/分页/" rel=external>分页</A>
<LI><A href="http://tag.csdn.net/tag/cachedrowset/"
rel=external>cachedrowset</A>
<LI><A href="http://tag.csdn.net/tag/jdbcrowset/" rel=external>jdbcrowset</A>
</LI></UL></DIV>
<H3>得分解答快速导航</H3>
<DIV>
<UL>
<LI>帖主:<A href="http://topic.csdn.net/t/20050423/20/3961523.html#Top">v38</A>
<LI><A
href="http://topic.csdn.net/t/20050423/20/3961523.html#r_29183068">jihanzhong</A>
<LI><A
href="http://topic.csdn.net/t/20050423/20/3961523.html#r_29185947">wafeng</A>
</LI></UL></DIV>
<H3>相关链接</H3>
<DIV>
<UL>
<LI><A href="http://java.csdn.net/" target=_blank>CSDN Java频道</A>
<LI><A
href="http://www.dearbook.com.cn/Book/SearchBook.aspx?sortid=4&sorttype=smallsort"
target=_blank>Java类图书</A>
<LI><A href="http://www.codechina.net/resource/sort.php/21"
target=_blank>Java类源码下载</A> </LI></UL></DIV>
<H3>广告也精彩</H3>
<DIV>
<SCRIPT src="关于记录分页中的问题.files/show_ads.js" type=text/javascript></SCRIPT>
</DIV>
<H3>反馈</H3>
<DIV>请通过下述方式给我们反馈<BR><IMG alt=反馈
src="关于记录分页中的问题.files/feedback.gif"></DIV></DIV>
<DIV class=clear></DIV></DIV>
<DIV class=CSDN-PHF id=CSDNPF>
<HR>
<A class=biaoshi
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"
rel=external> </A>
<DIV><A href="http://www.csdn.net/intro/intro.asp?id=2" rel=external>网站简介</A>-<A
href="http://www.csdn.net/intro/intro.asp?id=5" rel=external>广告服务</A>-<A
href="http://www.csdn.net/map/map.shtm" rel=external>网站地图</A>-<A
href="http://www.csdn.net/help/help.asp" rel=external>帮助</A>-<A
href="http://www.csdn.net/intro/intro.asp?id=9" rel=external>联系方式</A>-<A
href="http://job.csdn.net/Jobs/f9c75c9f2ad14404a604669b757b9ed0/viewcompany.aspx"
rel=external>诚聘英才</A>-<A href="http://www.csdn.net/english/"
rel=external>English</A>-<A
href="javascript:navigate('mai'%20+%20'lto:'%20+%20'webm'%20+%20'aster@c'%20+%20'sdn.n'+'et?subject=向CSDN报告问题')"
rel=external>问题报告</A></DIV>
<DIV>北京百联美达美数码科技有限公司 版权所有 京 ICP 证 020026 号</DIV>
<DIV>Copyright © 2000-2006, CSDN.NET, All Rights Reserved</DIV>
<SCRIPT src="关于记录分页中的问题.files/counter.js" type=text/javascript></SCRIPT>
<HR>
</DIV></DIV></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -