清單 4. 一個讀取數據的完整示例 // First, get all rows meeting the criterion RowSet rs = table.getRows( /"id<103/" ); // Iterate through the set for (int i=0; i<rs.length(); ++i) { // Grab each row in turn Row row = rs.get( i ); // Get and print the value of the /"name/" field String name = row.get( /"name/" ); System.out.println( /"Name: /"+name ); }
修改數據 正如前面所提到的,使用我們的 API 讀寫數據是以整個行為單位的。為了向數據庫寫入數據,您必須創建(或修改)Row 對象,然后向數據庫寫入那個 Row 對象。
向數據庫寫入數據是通過使用 Table 中的 putRow 方法。這種方法有兩種變體:
public void putRow( Row row ) public void putRow( Row row, String conditions )
清單 5. 重新創建一個 Row // Create an empty row object Row row = new Row(); // Fill it up with data row.put( /"id/", /"200/" ); row.put( /"name/", /"Joey Capellino/" );
或者,您可以修改一個以前曾經從數據庫中讀取的一個現有的行,如清單 6 所示。
清單 6. 修改現有的 Row // Grab a row from the database Row row = table.getRow( someConditions ); // Change some or all of the fields row.put( /"name/", /"Joey Capellino/" );