和普通Container修改其中Item项类似,SQLContainer 可以使用类似的方法来编辑其中的Item。RowItem的ColumnProperties 会自动通知SQLContainer关于数据的变化并应用到数据库。
添加Item
向SQLContainer中添加一项是通过方法addItem()来完成的。这个方法将创建一个新的Item,新创建的Item可以在内存中缓存或直接添加到数据库中。这取决于SQLContainer的auto commit 模式。
实际也只能通过addItem() 方法向SQLContainer中添加记录,其它由Container定义的方法SQLContainer并不支持:
- public boolean addContainerProperty(Object propertyId,
- Class<?> type,
- Object defaultValue)
- public boolean removeContainerProperty(Object propertyId)
- public Item addItem(Object itemId)
- public Object addItemAt(int index)
- public Item addItemAt(int index, Object newItemId)
- public Object addItemAfter(Object previousItemId)
- public Item addItemAfter(Object previousItemId, Object newItemId)
此外RowItem 不支持的Item方法有:
- public boolean addItemProperty(Object id, Property property)
- public boolean removeItemProperty(Object id)
下面的例子向Customer 表中添加一个记录 James ,Shen, Tribute Street, Perth
- void addCustomer(SQLContainer sqlContainer){
- sqlContainer.setAutoCommit(false);
- TemporaryRowId rowId=(TemporaryRowId)sqlContainer.addItem();
- if(rowId!=null){
- RowItem rowItem=(RowItem)sqlContainer.getItem(rowId);
- ColumnProperty firstName=(ColumnProperty)rowItem.getItemProperty("FIRSTNAME");
- firstName.setValue("James");
- ColumnProperty lastName=(ColumnProperty)rowItem.getItemProperty("LASTNAME");
- lastName.setValue("Shen");
- ColumnProperty street=(ColumnProperty)rowItem.getItemProperty("STREET");
- street.setValue("Tribute Steet");
- ColumnProperty city=(ColumnProperty)rowItem.getItemProperty("CITY");
- city.setValue("Perth");
- ColumnProperty Id=(ColumnProperty)rowItem.getItemProperty("ID");
- Id.setValue(50);
- rowItem.commit();
- }
- }
整体感觉使用SQLContainer编辑数据并不十分方便,还不如直接使用SQL语句或是使用hibernate.来的方便。