Lesson 5: Add data to the table

In this lesson, you add the following controls to the screen:

You then add code to insert the name in the text field and refresh the list.

  1. Add the controls to the screen.
    1. Add the following code before calling the getDataAccess method.
      // Add an edit field for entering new names
      _nameEditField = new EditField( "Name: ", "", 50, EditField.USE_ALL_WIDTH );
      add (_nameEditField );
              
      // Add an ObjectListField for displaying a list of names
      _nameListField = new ObjectListField();
      add( _nameListField );
      
      // Add a menu item
      addMenuItem(_addToListMenuItem);
                      
      // Create database and connect
      try{
          _da = DataAccess.getDataAccess();
    2. Add class-level declarations for _nameEditField and _nameListField. Also, define a _addToListMenuItem MenuItem with a run method (empty for now). These declarations belong next to the declarations of _statusLabel and _da.
      private EditField _nameEditField;
      private ObjectListField _nameListField;
      
      private MenuItem _addToListMenuItem = new MenuItem("Add", 10, 1){
          public void run() {
              // TODO
          }
      };
    3. Recompile the application and confirm that it runs.
  2. Add the following methods and objects to your application:
    • A DataAccess method to insert a row into a table
    • An object to hold a row of the Names table as an object
    • A DataAccess method to read the rows from a table into a Vector of objects
    • A method to refresh the contents of the list displayed on the HomeScreen
    • A method to add an item to the list on the HomeScreen.
    1. Add the DataAccess method to insert a row into a table:
      public void insertName( String name ){
          try{
              Value nameID = _conn.createUUIDValue();
              String sql = "INSERT INTO Names( ID, Name ) VALUES
                           ( ?, ? )";           
              PreparedStatement ps = _conn.prepareStatement(sql);
              ps.set(1, nameID );
              ps.set(2, name );
              ps.execute();
              _conn.commit();
          }
          catch( ULjException uex ){
              System.out.println( "ULjException: " + uex.toString() );
          }
          catch( Exception ex ){
              System.out.println( "Exception: " + ex.toString() );
          }  
      }
    2. Add the class that holds a row of the Names table. The toString method is used by the ObjectListField control.
      package myapp;
      
      class NameRow {
          
          public NameRow( String nameID, String name ) {  
              _nameID = nameID;
              _name = name;
          }
          
          public String getNameID(){
              return _nameID;
          }
          
          public String getName(){
              return _name;
          }
             
          public String toString(){
              return _name;
          }
          
          private String _nameID;
          private String _name;
          
      }
    3. Add the DataAccess method to read the rows from the table into a Vector of objects:
      public Vector getNameVector(){
          Vector nameVector = new Vector();
          try{
              String sql = "SELECT ID, Name FROM Names";
              PreparedStatement ps = _conn.prepareStatement(sql);
              ResultSet rs = ps.executeQuery();
              while ( rs.next() ){
                  String nameID = rs.getString(1);
                  String name = rs.getString(2);
                  NameRow nr = new NameRow( nameID, name);
                  nameVector.addElement(nr);
              }
          }
          catch( ULjException uex ){
              System.out.println( "ULjException: " + uex.toString() );
          }
          catch( Exception ex ){
              System.out.println( "Exception: " + ex.toString() );
          }
          finally{
              return nameVector;
          }
      }
    4. Add the user interface methods to the HomeScreen class. Following is the method to refresh the list of names:
      public void refreshNameList(){
          //Clear the list
          _nameListField.setSize(0);
          //Refill from the list of names
          Vector nameVector = _da.getNameVector();
          for( Enumeration e = nameVector.elements(); e.hasMoreElements(); ){
              NameRow nr = ( NameRow )e.nextElement();
              _nameListField.insert(0, nr);
          }
      }
    5. Call the refreshNameList method before the end of the HomeScreen constructor so that the list is filled when the application starts.
      // Fill the ObjectListField
      this.refreshNameList();
    6. Add a HomeScreen method that is used to add a row to the list:
      private void onAddToList(){
          _da.insertName(_nameEditField.getText());
          this.refreshNameList();
          _nameEditField.setText("");
      }
    7. Call this method from within the run method of the _addToListMenuItem MenuItem (which currently says //TODO):
      public void run() {
          onAddToList();
      }
  3. Compile and run the application.
Resetting the simulator

If you need to reset the simulator to a clean state, choose Erase Simulator File from the BlackBerry JDE File menu (not the Simulator menu), and erase the items in the submenu. If you reset the simulator this way, you must re-import the UltraLiteJ.cod file before running your application again.