Contacts.acceptChanges();
|
如果您是采用清单3中的方法初始化和创建CachedRowSet的,您必须指定连接对象:
Contacts.acceptChanges(connection);
|
在JSP页面中,这个操作可以与一个请求参数进行关联。它允许用户决定什么时候提交更新:
if ( req.getParameter("save") != null ) {
Contacts.acceptChanges();
}
|
CachedRowSet同样可以执行插入和删除操作。虽然向CachedRowSet插入记录非常简单,但显得有些不太直观。在插入一行以前,您必须将游标放置在一个称为“插入行”的特殊位置。
下面的代码说明如何将一行数据插入到CachedRowSet:
rowSet.moveToInsertRow(); // move to construct an insert row
rowSet.updateString(1, "New Contact"); // initialize name
rowSet.updateString(2, "(111) 111-1111"); // initialize phone
rowSet.insertRow(); // insert the row
rowSet.moveToCurrentRow(); // move back to previous cursor position
|
moveToInsertRow()将游标移动到可能包含初始列值的空白行。调用insertRow()将新行插入到游标最近位置的后面。然后,在调用其它导航命令之前调用moveToCurrentRow()重置游标位置。最后,必须为RowSet调用acceptChanges()以更新数据库。将它应用到JSP中可能需要一些修改,大多数记录的entry/edit表单按照下面的步骤插入行:
在当前游标的位置后面插入一个新的行,将所有字段初始化为空,在表单中显示新插入的行,以便用户进行编辑或保存,要加入记录插入功能,您的JSP需要监听一个新的请求参数"insert"。下面就是JSP页面的插入操作代码:
} else if ( req.getParameter("insert") != null) {
Contacts.moveToInsertRow();
Contacts.updateString(1, "");
Contacts.updateString(2, "");
Contacts.insertRow();
Contacts.moveToCurrentRow();
Contacts.next();
}
|
CachedRowSet 的其它用途
对缓冲ResultSet扩展的需求远远不只限制在JSP的开发中,CachedRowSet适合于需要将表格数据进行串行化并发送到客户端的任何一种情况,如移动计算设备。虽然用户可能周期地断开网络数据源,但它们仍然有填加和维护数据的需求。可以使用CachedRowSet作为从EJB会话bean中存取/更新数据的一种方法。
记住,CachedRowSet完全在存储器中存储,如果应用程序需要对非常大的数据集进行操作,CachedRowSet显然不是最佳选择。
清单4显示了以CachedRowSet为数据模型的一个完整的JSP entry/edit页面代码。为清晰起见,我在JSP页面中加入了数据模型初始化代码,这些代码在useBean标签中体现。 在更大型的工程应用中,我建议遵从MVC (Model-View-Controller)准则,将数据模型初始化代码放在一个servlet控制器中。
清单4:JSP示例update/entry表单
<%@ page import="sun.jdbc.rowset.CachedRowSet" %>
<HTML>
<HEAD>
<jsp:useBean id="Contacts" class="sun.jdbc.rowset.CachedRowSet"
scope="session">
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// initialize our CachedRowSet bean
Contacts.setUsername("user");
Contacts.setPassword("password");
Contacts.setUrl("jdbc:odbc:mydsn");
// some drivers require this
Contacts.setTableName("Contacts");
Contacts.setCommand("SELECT name, telephone from Contacts");
Contacts.execute();
Contacts.first();
%>
</jsp:useBean>
<%
// get the servlet request object
javax.servlet.ServletRequest req = pageContext.getRequest();
// process updates
boolean updateRow = false;
String contactName = Contacts.getString(1);
String newValue = req.getParameter("ContactName");
if (( newValue != null) && (!newValue.equals( contactName ))) {
Contacts.updateString( 1,req.getParameter("ContactName"));
updateRow = true;
}
String contactPhone = Contacts.getString(2);
newValue = req.getParameter("ContactPhone");
if (( newValue != null) && (!newValue.equals(contactPhone))) {
Contacts.updateString( 2,req.getParameter("ContactPhone"));
updateRow = true;
}
if (updateRow) Contacts.updateRow();
// process navigation commands
if ( req.getParameter("next") != null ) {
if (! Contacts.next() ) Contacts.last();
} else if ( req.getParameter("prev") != null) {
if (! Contacts.previous()) Contacts.first();
} else if ( req.getParameter("save") != null) {
Contacts.acceptChanges();
} else if ( req.getParameter("insert") != null) {
Contacts.moveToInsertRow();
Contacts.updateString(1, "");
Contacts.updateString(2, "");
Contacts.insertRow();
Contacts.moveToCurrentRow();
Contacts.next();
} else if ( req.getParameter("delete") != null) {
Contacts.deleteRow();
if (!Contacts.next()) Contacts.last();
}
%>
<STYLE>
BODY { font-style: verdana }
</STYLE>
<TITLE>
CachedRowSetExample
</TITLE>
</HEAD>
<BODY BGCOLOR='lightgrey'>
<H2>Contacts</H2>
<FORM>
<TABLE BORDER='0'>
<TR><TD>Name:</TD><TD>Telephone
number:</TD></TR>
<TR>
<TD><INPUT TYPE='text'
NAME="ContactName"
VALUE='<%=Contacts.getString(1)%>' /></TD>
<TD><INPUT TYPE="text"
NAME="ContactPhone"
VALUE='<%=Contacts.getString(2)%>' /></TD>
</TABLE>
<INPUT TYPE="submit" NAME="prev" VALUE=" < "/>
<INPUT TYPE="submit" NAME="next" VALUE=" > "/>
<INPUT TYPE="submit" NAME="insert" VALUE="Insert"/>
<INPUT TYPE="submit" NAME="delete" VALUE="Delete"/>
<INPUT TYPE="submit" NAME="save" VALUE="Save"/>
Record <%=Contacts.getRow()%> of <%=Contacts.size()%>
</FORM>
</BODY>
</HTML>
如果您对本文有任何疑问或者建议,请到讨论区发表您的意见:
>>
论坛入口 <<
上一篇:XML与Java技术
下一篇:建立独立于数据类型的JDBC应用
【文章评论】
【收藏本文】
【推荐好友】
【打印本文】
【我要投稿】 【论坛讨论】
更多相关文章
|
|