Hello Amit,

Thanks for your observations and improvements of the DatabaseDemo to
use an AUTOINCREMENT key.  Could you please review the attached patch
that revises the one you sent me earlier?

This patch updates the DatabaseDemo with the following modifications:

- Uses better SQL coding style
- The Id field now uses an AUTOINCREMENT primary key
- The Id & Timestamp is visible on the UI
- Query results are put in a FlexTable with headings
- Minor formatting changes.

M      
samples/database/src/com/google/gwt/gears/sample/database/client/DatabaseDemo.java
-- 
Eric Z. Ayers - GWT Team - Atlanta, GA USA
http://code.google.com/webtoolkit/

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Index: samples/database/src/com/google/gwt/gears/sample/database/client/DatabaseDemo.java
===================================================================
--- samples/database/src/com/google/gwt/gears/sample/database/client/DatabaseDemo.java	(revision 877)
+++ samples/database/src/com/google/gwt/gears/sample/database/client/DatabaseDemo.java	(working copy)
@@ -23,6 +23,7 @@
 import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.ClickListener;
+import com.google.gwt.user.client.ui.FlexTable;
 import com.google.gwt.user.client.ui.HTML;
 import com.google.gwt.user.client.ui.HorizontalPanel;
 import com.google.gwt.user.client.ui.KeyboardListener;
@@ -33,38 +34,49 @@
 import com.google.gwt.user.client.ui.VerticalPanel;
 import com.google.gwt.user.client.ui.Widget;
 
+import java.util.Date;
+
 /**
  * Sample application demonstrating how to use the [EMAIL PROTECTED] Database} class.
  */
 public class DatabaseDemo implements EntryPoint {
-
+  private static final int NUM_SAVED_ROWS = 3;
+  
   private final Button addButton = new Button("Add");
   private final Button clearButton = new Button("Clear Database");
   private Database db;
   private final TextBox input = new TextBox();
+  private final FlexTable dataTable = new FlexTable();
 
-  private final Label[] labels = new Label[] {
-      new Label(), new Label(), new Label()};
-
   public void onModuleLoad() {
     VerticalPanel outerPanel = new VerticalPanel();
-    outerPanel.getElement().getStyle().setPropertyPx("margin", 15);    
-    
+    outerPanel.setSpacing(10);
+    outerPanel.getElement().getStyle().setPropertyPx("margin", 15);
+
     HorizontalPanel textAndButtonsPanel = new HorizontalPanel();
+    textAndButtonsPanel.add(new Label("Enter a Phrase: "));
     textAndButtonsPanel.add(input);
     textAndButtonsPanel.add(addButton);
     textAndButtonsPanel.add(clearButton);
     outerPanel.add(textAndButtonsPanel);
+    outerPanel.add(new Label("Last 3 Entries:"));
+    outerPanel.add(dataTable);
     
-    for (int i = 0; i < labels.length; ++i) {
-      outerPanel.add(labels[i]);
+    for (int i = 0; i <= NUM_SAVED_ROWS; ++i) {
+      dataTable.insertRow(i);
+      dataTable.addCell(i);
+      dataTable.addCell(i);
+      dataTable.addCell(i);
     }
+    dataTable.setWidget(0, 0, new HTML("<b>Id</b>"));
+    dataTable.setWidget(0, 1, new HTML("<b>Phrase</b>"));
+    dataTable.setWidget(0, 2, new HTML("<b>Timestamp</b>"));
 
     // Create the database if it doesn't exist.
     try {
       db = Factory.getInstance().createDatabase();
       db.open("database-demo");
-      db.execute("create table if not exists Demo (Phrase varchar(255), Timestamp int)");
+      db.execute("CREATE TABLE IF NOT EXISTS Phrases (Id INTEGER PRIMARY KEY AUTOINCREMENT, Phrase VARCHAR(255), Timestamp INTEGER)");
     } catch (DatabaseException e) {
       RootPanel.get("demo").add(new HTML("Error opening or creating database: <font color=\"red\">" + e.toString() + "</font>"));
       // Fatal error.  Do not build the interface.
@@ -92,41 +104,44 @@
         displayRecentPhrases();
       }
     });
-    
+
     RootPanel.get("demo").add(outerPanel);
     displayRecentPhrases();
   }
-  
+
   /**
    * Remove all phrases from the database.
    */
   private void clearPhrases() {
     try {
-      db.execute("delete from Demo");
+      db.execute("DELETE FROM Phrases");
     } catch (DatabaseException e) {
       Window.alert(e.toString());
     }
   }
-  
+
   /**
    * Fill the labels with the phrases from the database.
    */
   private void displayRecentPhrases() {
     try {
-      ResultSet rs = db.execute("select * from Demo order by Timestamp desc");
+      ResultSet rs = db.execute("SELECT * FROM Phrases ORDER BY Id DESC");
       int i;
-     
-      for (i = 0; rs.isValidRow(); ++i, rs.next()) {
-        if (i < labels.length) {
-          labels[i].setText(rs.getFieldAsString(0));
+
+      for (i = 1; rs.isValidRow(); ++i, rs.next()) {
+        if (i <= NUM_SAVED_ROWS) {
+          dataTable.setText(i, 0, rs.getFieldAsString(0));
+          dataTable.setText(i, 1, rs.getFieldAsString(1));
+          dataTable.setText(i, 2, new Date(rs.getFieldAsLong(2)).toString());
         } else {
-          db.execute("delete from Demo where Timestamp=?",
-              new String[] {rs.getFieldAsString(1)});
+          db.execute("DELETE FROM Phrases WHERE Id = ?",
+              new String[] {rs.getFieldAsString(0)});
         }
       }
       // If a phrase has been removed, clear the label.
-      for (; i < labels.length; i++) {
-        labels[i].setText("");
+      for (; i <= NUM_SAVED_ROWS; i++) {
+        dataTable.clearCell(i, 0);
+        dataTable.clearCell(i, 1);
       }
       rs.close();
     } catch (DatabaseException e) {
@@ -141,8 +156,8 @@
     try {
       String phrase = input.getText();
       if (phrase.length() > 0) {
-        db.execute("insert into Demo values (?, ?)", new String[] {
-            phrase, Long.toString(System.currentTimeMillis())});
+        db.execute("INSERT INTO Phrases (Phrase, Timestamp) VALUES (?, ?)",
+            new String[] {phrase, Long.toString(System.currentTimeMillis())});
         displayRecentPhrases();
         input.setText("");
       }

Reply via email to