In this lesson, you update the application code to create a table named Names in the UltraLite Java edition database.
Prerequisites
This lesson assumes you have completed all preceding lessons. See Lesson 1: Setting up a new BlackBerry project.
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.
The table 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 |
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() |
Compare your DataAccess code to the complete code listing of the DataAccess class to ensure that they are identical.
Click File » Save.
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.
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.
From the simulator menu, click File » Load Java Program.
Browse to the \UltraLite\UltraLiteJ\BlackBerry4.2\ directory of your SQL Anywhere installation and open the UltraLiteJ16.cod file.
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.
From the simulator menu, click Simulate » Set IT Policy.
The Set IT Policy window appears.
Click Policy » Allow Third Party Apps to Use Persistent Store and then click >>.
Click Set and then click Close.
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.
Stop the simulation.
In the simulator window, click File » Exit.
![]() |
Discuss this page in DocCommentXchange.
|
Copyright © 2014, SAP AG or an SAP affiliate company. - SAP Sybase SQL Anywhere 16.0 |