mardi 10 février 2015

retrieving data from sqlite in blackberry java

i need to retrieve data from sqlite .and retrieved data should be displayed in a gridfieldmanager layout.i have done the below code please help me how to display data from database over the myscreen.


SQLManager screen



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


myscreen screen



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

}
}

Aucun commentaire:

Enregistrer un commentaire