ORMLite provides the following features:

Setup your classes by simply adding Java annotations.
Powerful abstract Database Access Object (DAO) classes.
Flexible QueryBuilder to easily construct simple and complex queries.
Supports MySQL, Postgres, Microsoft SQL Server, H2, Derby, HSQLDB, and Sqlite and can be extended to additional databases relatively easily.
Provisional support for DB2, Oracle, ODBC, and Netezza. Contact the author if your database type is not supported.
Handles "compiled" SQL statements for repetitive query tasks.
Supports "foreign" objects with the class field being the object but an id stored in the database table.
Basic support for database transactions.
Auto generates SQL to create and drop database tables.
Spring configuration support for DOAs and class configurations.
Support for configuring of tables and fields without annotations.
Use Annotations to Mark Classes to be Persisted

To use the package you add the @DatabaseTable annotation to the top of each class and a @DatabaseField annotation to each of the fields in the class that are to be persisted to the database. For example:

@DatabaseTable(tableName = "accounts")
public class Account {
@DatabaseField(id = true)
private String name;

@DatabaseField(canBeNull = false)
private String password;
Account() {
// all persisted classes must define a no-arg constructor with at least package visibility

Classes can also be configured with javax.persistence annotations, Java calls, or Spring wiring. For more details, see the online documentation.

Sample Code Example

The ORMLite Android code includes base activity, service, and tab classes to help create and manage your database connections and DAOs. The following is a quick code example to give you a taste on how to use the package.

// you get the SQLiteOpenHelper from your Android Activity
ConnectionSource connectionSource =
new AndroidConnectionSource(sqliteOpenHelper);

// instantiate the DAO to handle Account with String id
Dao<Account,String> accountDao =
BaseDaoImpl.createDao(connectionSource, Account.class);

// if you need to create the 'accounts' table make this call
TableUtils.createTable(connectionSource, Account.class);

// create an instance of Account
String name = "Jim Smith";
Account account = new Account(name, "_secret");

// persist the account object to the database
// it should return 1 for the 1 row inserted
if (accountDao.create(account) != 1) {
throw new Exception("Failure adding account");

// retrieve the account
Account account2 = accountDao.queryForId(name);
// show its password
System.out.println("Account: " + account2.getPassword());

// close the connection source

Comments and questions

Publish comment or question

Copyright 2019 © ELTASK.COM
All rights reserved.