📄 jdk1.5学习之rowset(一).htm
字号:
<P><FONT face="Times New Roman" size=3></FONT> </P></SPAN>
<P></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><FONT size=3><SPAN lang=EN-US><FONT
face="Times New Roman">JdbcRowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">接口:</SPAN></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><FONT size=3><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我的理解是这个接口基本上和</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">ResultSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有类似的功能,只不过它的结果集默认是</SPAN><SPAN
lang=EN-US><FONT
face="Times New Roman">ResultSet.TYPE_SCROLL_INSENSITIVE</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN
lang=EN-US><FONT
face="Times New Roman">ResultSet.CONCUR_UPDATABLE</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的,也就是说默认的结果集就是可以上下滚动和可更新的。</SPAN></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><FONT size=3><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">因为本身</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">RowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">接口就是</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">ResultSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的子接口,所以</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">1.5</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">里面的所有</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">RowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">都有</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">ResultSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的方法,而</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">JdbcRowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">又只是在默认的属性和</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">ResultSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有区别,所以它对结果集的操作方法和</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">ResultSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">都是一样的,我就不具体介绍了,大家可以参考</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">API</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><FONT size=3><SPAN lang=EN-US><SPAN style="mso-tab-count: 1"><FONT
face="Times New Roman">
</FONT></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面我介绍一下</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">JdbcRowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的创建方法,这有两种方法,一种是基于传统的</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">JDBC</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">连接数据库的方法:</SPAN></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman"
size=3>Class.forName(“org.gjt.mm.mysql.Driver”);</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman" size=3>Connection
conn=DriverManager.getConnection(“jdbc:mydql://localhost:3306/test”,”root’,””);</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman" size=3>Statement
stmt=conn.createStatement();</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman" size=3>ResultSet
rs=stamt.executeQuery(“select * from table<CHMETCNV w:st="on" tcsc="0"
numbertype="1" negative="False" hasspace="False" sourcevalue="1"
unitname="”">1”</CHMETCNV>);</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman" size=3>JdbcRowSet
jrs=new JdbcRowSetImpl(rs);</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><FONT size=3><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这样就创建了一个对象(</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">JdbcRowSetImpl</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">com.sun.rowset</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">包里面的实现类,<FONT
id=ieqestarmiwopp>LrW{%N|垠中育&%#r</FONT>文中的五个接口在包中都对应有一个实现类),这个对象里面的数据就是和</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">rs</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">里面的数据是一样的。还有一种创建的方法是使用默认的构造方法,然后</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">set</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性得到数据,个人推荐使用第二种方法:</SPAN></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman" size=3>JdbcRowSet
jrs=new JdbcRowSetImpl();</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman"
size=3>jrs.setUrl(“jdbc:mydql://localhost:3306/test”);</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman"
size=3>jrs.setUsername(“root”);</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman"
size=3>jrs.setPassword(“”);</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman"
size=3>jrs.setCommand(“select * from table<CHMETCNV w:st="on" tcsc="0"
numbertype="1" negative="False" hasspace="False" sourcevalue="1"
unitname="”">1”</CHMETCNV>);</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman"
size=3>jrs.execute();</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><FONT size=3><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这样创建的对象是和第一种方法是一样的结果。当然这种方法可以连接一个数据源,如果我们在上下文环境种绑定了一个数据源,</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">JNDI</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">名字是</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">dataSource1</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,那么下面的代码就可以获得对象:</SPAN></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman" size=3>JdbcRowSet
jrs=new JdbcRowSetImpl();</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman"><FONT size=3><SPAN
style="mso-tab-count: 1">
</SPAN>jrs.setDataSourceName(“dataSource<CHMETCNV w:st="on" tcsc="0"
numbertype="1" negative="False" hasspace="False" sourcevalue="1"
unitname="”">1”</CHMETCNV>)</FONT></FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US><FONT face="Times New Roman"><FONT size=3><SPAN
style="mso-tab-count: 1">
</SPAN>jrs.execute();</FONT></FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT
size=3>得到对象之后我们就可以使用相应的方法来对数据进行遍历,
<DIV id=udqguoqcsgtvfhbh>络q(C@QJ国h6R中6!业,</DIV>更新,插入或者删除。</FONT></SPAN>
<P></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><FONT size=3><SPAN lang=EN-US><SPAN style="mso-tab-count: 1"><FONT
face="Times New Roman">
</FONT></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我有</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">2</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">点要说明:第一,其它四个接口的对象中除了</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">JoinRowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">创建方式基本都是一样的,只是接口名字和接口实现类的名字不同而已,后面我就不再说明创建对象的方法了。第二,虽然</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">JdbcRowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">默认是可滚动和可更新的,但是这也是需要数据库驱动支持的,我使用的</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">MySql</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">驱动就不支持更新结果集,所以大家使用之前需要阅读驱动的说明文档。</SPAN></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt; TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><SPAN lang=EN-US
style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: TimesNewRomanPSMT; mso-font-kerning: 0pt; mso-bidi-font-family: TimesNewRomanPSMT">
<P><FONT face="Times New Roman"></FONT> </P></SPAN>
<P></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN
lang=EN-US><FONT face="Times New Roman">CachedRowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">接口:</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN
lang=EN-US><SPAN style="mso-tab-count: 1"><FONT
face="Times New Roman">
</FONT></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">它继承于</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">RowSet</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">接口,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -