Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » UltraLite - Java Programming » Tutorial: Building a BlackBerry application » Part 1: Creating a new BlackBerry application

 

Lesson 5: Adding data to the table

In this lesson, you populate the UltraLite Java edition database by adding several controls to your application, implementing code to insert data into the Names table, running the application in a simulator, and then using the controls to input data.

Prerequisites

This lesson assumes you have completed all preceding lessons. See Lesson 1: Setting up a new BlackBerry project.

 Task
  1. Update the HomeScreen class to add the controls.

    Double-click HomeScreen.java in the Package Explorer window, and then insert the following code above the try-catch statement that calls 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);
  2. Add class-level declarations for _nameEditField and _nameListField, and then define a MenuItem with a run method (empty for now). These declarations belong next to the declarations of _statusLabel and _da.

    Insert the following code below the private DataAccess _da; statement:

        private EditField _nameEditField;
        private ObjectListField _nameListField;
    
        private MenuItem _addToListMenuItem = new MenuItem("Add", 1, 1){
            public void run() {
                // TODO
            }
        };
  3. Add a new method to the DataAccess class that inserts a row into a table.

    Double-click DataAccess.java in the Package Explorer window, and then insert the following code after the createDatabaseSchema method:



        public void insertName(String name){
            try {
                UUIDValue 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();
                ps.close();
            }
            catch(ULjException uex) {
                Dialog.alert("ULjException: " + uex.toString());
            }
            catch( Exception ex ){
                Dialog.alert("Exception: " + ex.toString());
            }  
        }
  4. Add a NameRow class to your project.

    1. In the Package Explorer window, expand HelloBlackBerry and click src.

    2. Click File » New » Class.

      The New Java Class window appears.

    3. In the Name field, type NameRow.

    4. Click Finish.

      The NameRow.java file appears under your project in the Package Explorer window.

  5. Update the NameRow class so that it can store a row in the Names table as an object.

    Double-click NameRow.java in the Package Explorer window, and then replace the code with the following snippet:



    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;
        
    }

    The toString method is used by the ObjectListField control.

  6. Add a new method to the DataAccess class that reads a row into a Vector of objects.

    Double-click DataAccess.java in the Package Explorer window, and then insert the following code after the insertName method:



        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) {
                Dialog.alert("ULjException: " + uex.toString());
            }
            catch(Exception ex) {
                Dialog.alert("Exception: " + ex.toString());
            }
            return nameVector;
        }
  7. Add a new method to the HomeScreen class that refreshes the contents of the list displayed on the screen.

    Double-click HomeScreen.java in the Package Explorer window, and then insert the following code after the _addToListMenuItem method:



        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);
            }
        }
  8. Update the HomeScreen class so that it calls the refreshNameList method, ensuring that the list is filled when the application starts.

    Insert the following code before the end of the HomeScreen constructor:

            // Fill the ObjectListField
            refreshNameList();
  9. Add a new method to the HomeScreen class that adds a row to the list on the screen.

    Insert the following code after the refreshNameList method:

        private void onAddToList(){
            String name = _nameEditField.getText();
            _da.insertName(name);
            this.refreshNameList();
            _nameEditField.setText("");
            _statusLabel.setText(name + " added to list");
        }
  10. Update the run method in the HomeScreen class so that it calls the onAddToList method.

    Replace the line of code that states \\ TODO with the following code snippet:

                onAddToList();
  11. Click File » Save All.

  12. Run the simulator to verify that the application compiles and runs.

    In the Package Explorer window, click Application.java, and then click Run » Run As » BlackBerry Simulator.

    Note

    If multiple projects are open in your workspace, click Run » Run Configurations, select HelloBlackBerry, and then click Run.

    The HelloBlackBerry project compiles and then the simulator window appears.

    Ensure that the project compiles without errors by selecting the Problems tab in Eclipse.

  13. From the simulator menu, click File » Load Java Program.

  14. Browse to the \UltraLite\UltraLiteJ\BlackBerry4.2\ directory of your SQL Anywhere installation and open the UltraLiteJ16.cod file.

    Note

    You may need to copy UltraLiteJ16.cod and the DBG files to the working simulator directory (for example, C:\Eclipse\plugins\net.rim.ejde.componentpack6.0.0_6.0.0.0.26\components\simulator\) to run the application. When copied, you do not need to load the Java program from the simulator menu.

  15. From the simulator menu, click Simulate » Set IT Policy.

    The Set IT Policy window appears.

  16. In the Policy field, click Allow Third Party Apps To Use Persistent Store » >>.

  17. Click Set and then click Close.

  18. Launch your application.

    In the simulator window, navigate to Downloads and then run the HelloBlackBerry application.

    A screen appears that displays the Hello BlackBerry title bar, the Status: Connected text, and a Name field.

  19. At the name field, type John Smith.

  20. Click *EMPTY* and then choose Add.

    John Smith appears in the list, which indicates that the name entry was added to the Names table in the database.

    Names are stored in the database as you add them. They are retrieved from the database and added to the list when you close and re-open the application.

  21. Stop the simulation.

    In the simulator window, click File » Exit.

Results

The inputted data is inserted into the Names table in the UltraLite Java edition database.