【问题标题】:retrieving data from sqlite in blackberry java从黑莓java中的sqlite检索数据
【发布时间】:2015-02-10 10:57:58
【问题描述】:

我需要从 sqlite 检索数据。检索到的数据应显示在 gridfieldmanager 布局中。我已完成以下代码,请帮助我如何在 myscreen 上显示数据库中的数据。

SQLManager 屏幕

 public class SQLManager {
static String snapsdata;
private static String DB_NAME = "employee_details.db3";

   private Database _db;

  public SQLManager() 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.");
                System.exit(0);
            }
        });
    } else {
        String dbLocation = "/SDCard/databases/sample/";

        // 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();
        //Dialog.alert("db");
        // 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);
            //Dialog.alert("db1");
        }

        // Open the database
        db = DatabaseFactory.open(uri);
        _db = db;
    }
}

/**
 * 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);
    //Dialog.alert("db" + inputStream);
    // 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[1024 * 5];
    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();
    }
}

/**
 * Constructs a new SQLManager object
 * 
 * @param db
 *            Database to manage
 */
public SQLManager(Database db) {
    _db = db;
}

/**
 * Closes the database
 */
void closeDB() {
    try {
        _db.close();

    } catch (DatabaseException dbe) {

    }
}

public void SaveEmployeeInformation(int employeeid, String employee_name,   
String position, int salary){


    //return productinfo;
    Statement st;
    try {
        st = _db.createStatement("INSERT INTO 
 employee_details(employee_id,employee_name,position,salary) VALUES (?, ?, 
 ?, ?)");

     try
     {
         st.prepare();
         Object[] bindParams = {new Integer(employeeid), new 
 String(employee_name), new String(position), new Integer(salary)};
         long rowID = st.executeInsert(bindParams);
        // Dialog.alert("ro "+rowID);
     }
     finally
     {
         st.close();
         closeDB();
     }
    } catch (DatabaseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public Vector getEmployeeInformation(){
    Vector productinfo = new Vector();
    try {
        Statement statement = null;
        // Read in all records from the Category table

        statement = _db
                .createStatement("select MAX(employee_id) as 
  employeeReportId from employee_details");
        // ProjectImagesID Project_id,ImagePath,ImageDescription
        statement.prepare();

        // statement.setCursorBufferSize(10);

        Cursor cursor = statement.getCursor();
        Employeelist productdatas;

        Row row;

        // Iterate through the result set. For each row, create a new
        // Category object and add it to the hash table.
        while (cursor.next()) {
            row = cursor.getRow();

            productdatas = new Employeelist(row.getInteger(0));

            productinfo.addElement(productdatas);

        }
    //  Dialog.alert(""+productinfo.size());
        statement.close();
        cursor.close();

    } catch (DatabaseException dbe) {
        Dialog.alert("SD PRODUCTINFP  " + dbe.toString());
    } catch (DataTypeException e) {
        Dialog.alert("PRODUCTINFP  " + e.toString());
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        closeDB();
    }

    return productinfo;
}
}

我的屏幕屏幕

public final class MyScreen extends MainScreen
{
/**
 * Creates a new MyScreen object
 */
public  MyScreen()
{        
    // Set the displayed title of the screen       
    setTitle("MyTitle");
   // int reportid = 0;
    try {
        SQLManager emp = new SQLManager();
        emp.SaveEmployeeInformation(7,"farah","developer", 4000);
        emp.getEmployeeInformation();
    } catch (Exception e) 
    {// TODO Auto-generated catch block
        e.printStackTrace();
    }
    int row = 5;
      GridFieldManager  dfm = new GridFieldManager(row,3, 0);

                  add(dfm);        

    }
 }

【问题讨论】:

    标签: java sqlite blackberry


    【解决方案1】:

    首先尝试从 SQlite Db 成功设置和检索数据。 .然后用检索到的数据,尝试在网格字段管理器中使用它。

    请在代码中写出确切的问题,以帮助我们解决问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多