[官方Demo]BlackBerry SQL与 TreeField 官方实例
SQLiteDemo.java
?
SQLiteDemo extends UiApplication{ private static String DB_NAME = "SQLiteDemoDirectory"; /** * Entry point for this application * @param args Command line arguments (not used) * @throws Exception */ public static void main(String[] args) throws Exception { // Create a new instance of the application and make the currently // running thread the application's event dispatch thread. SQLiteDemo app = new SQLiteDemo(); app.enterEventDispatcher(); } /** * Creates a new SQLiteDemo object * @throws Exception */ public SQLiteDemo() throws Exception { // Determine if an SDCard is present boolean sdCardPresent = false; String root = null; Enumeration e = FileSystemRegistry.listRoots(); while (e.hasMoreElements()) { root = (String)e.nextElement(); if(root.equalsIgnoreCase("sdcard/")) { sdCardPresent = true; } } if(!sdCardPresent) { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { Dialog.alert("This application requires an SD card to be present. Exiting application..."); System.exit(0); } }); } else { String dbLocation = "/SDCard/databases/SQLite Demo/"; // Create URI URI uri = URI.create(dbLocation + DB_NAME); // Open or create a plain text database. This will create the // directory and file defined by the URI (if they do not already exist). Database db = DatabaseFactory.openOrCreate(uri, new DatabaseSecurityOptions(false)); // Close the database in case it is blank and we need to write to the file db.close(); // Open a connection to the database file FileConnection fileConnection = (FileConnection)Connector.open("file://" + dbLocation + DB_NAME); // If the file is blank, copy the pre-defined database from this // module to the SDCard. if(fileConnection.exists() && fileConnection.fileSize() == 0) { readAndWriteDatabaseFile(fileConnection); } // Retrieve the code signing key for the XYZ key file CodeSigningKey codeSigningKey = CodeSigningKey.get(CodeModuleManager.getModuleHandle( "SQLiteDemo" ), "XYZ"); try { // Encrypt and protect the database. If the database is already // encrypted, the method will exit gracefully. DatabaseFactory.encrypt(uri, new DatabaseSecurityOptions(codeSigningKey)); } catch(DatabaseException dbe) { errorDialog("Encryption failed - " + dbe.toString()); } // Open the database db = DatabaseFactory.open(uri); // Create a new main screen and push it onto the display stack SQLiteDemoScreen screen = new SQLiteDemoScreen(new SQLManager(db)); pushScreen(screen); } } /** * Copies the pre-defined database from this module to the * location specified by the fileConnection argument. * @param fileConnection File connection to the database location */ public void readAndWriteDatabaseFile(FileConnection fileConnection) throws IOException { OutputStream outputStream = null; InputStream inputStream = null; // Open an input stream to the pre-defined encrypted database bundled // within this module. inputStream = getClass().getResourceAsStream("/" + DB_NAME); // Open an output stream to the newly created file outputStream = (OutputStream)fileConnection.openOutputStream(); // Read data from the input stream and write the data to the // output stream. byte[] data = new byte[256]; int length = 0; while (-1 != (length = inputStream.read(data))) { outputStream.write(data, 0, length); } // Close the connections if(fileConnection != null) { fileConnection.close(); } if(outputStream != null) { outputStream.close(); } if(inputStream != null) { inputStream.close(); } } /** * Presents a dialog to the user with a given message * @param message The text to display */ public static void errorDialog(final String message) { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { Dialog.alert(message); } }); }}??
?
ItemScreen.java
?
??
DirectoryItem.java
?