Lesson 4: Creating a table in the database

In this lesson, you create a table named Names, which contains two columns that have the following properties:

Column name Data type Allow null? Default Primary key?
ID UUID No None Yes
Name Varchar(254) No None No
 Update the sample application to create a table in the database
  1. Add a new method to the DataAccess class that creates the Names table.

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



        private void createDatabaseSchema() {
            try {
                String sql = "CREATE TABLE Names (ID UNIQUEIDENTIFIER DEFAULT NEWID(), Name VARCHAR(254), " +
                    "PRIMARY KEY (ID))";
                PreparedStatement ps = _conn.prepareStatement(sql);
                ps.execute();
                ps.close();
            }
            catch (ULjException uex1) {
                Dialog.alert("ULjException: " + uex1.toString());
            }
            catch (Exception ex1) {
                Dialog.alert("Exception: " + ex1.toString());
            }
        }

    This method throws an exception if the Names table already exists in the database.

  2. Call the createDatabaseSchema method from the getDataAccess method.

    Remove the code comments from the getDataAccess method so that the createDatabaseSchema calls look like the following code snippet:

    _da.createDatabaseSchema()
  3. Compare your DataAccess code to the complete code listing of the DataAccess class to ensure that they are identical.

  4. Click File » Save.

  5. 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.

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

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

    Note

    You may need to copy UltraLiteJ12.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.

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

    The Set IT Policy window appears.

  9. Click Policy » Allow Third Party Apps to Use Persistent Store and then click >>.

  10. Click Set and then click Close.

  11. 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 and the Status: Connected text, which indicates that the application has successfully connected to the UltraLite Java edition database.

  12. Stop the simulation.

    In the simulator window, click File » Exit.

 See also