本文整理汇总了Java中com.j256.ormlite.dao.DaoManager类的典型用法代码示例。如果您正苦于以下问题:Java DaoManager类的具体用法?Java DaoManager怎么用?Java DaoManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DaoManager类属于com.j256.ormlite.dao包,在下文中一共展示了DaoManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initDb
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
private void initDb() throws SQLException {
// create a connection source to our database
connectionSource = new JdbcConnectionSource(DATABASE_URL, "sa", "",
new HsqldbDatabaseType());
// instantiate the DAO
vaultDao = DaoManager.createDao(connectionSource, VaultEntry.class);
if (!vaultDao.isTableExists()) {
TableUtils.createTableIfNotExists(connectionSource, VaultEntry.class);
} else {
LOG.warning("Found existing DB for VaultEntries. Reusing it!!");
}
rawDao = DaoManager.createDao(connectionSource, RawEntry.class);
if (!rawDao.isTableExists()) {
TableUtils.createTableIfNotExists(connectionSource, RawEntry.class);
}
// TableUtils.createTableIfNotExists(connectionSource, SliceEntry.class);
}
示例2: AnkiDatabase
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
AnkiDatabase(File databaseFile) throws SQLException {
try {
//Loading the sqlite drivers
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
//Should never happen
throw new SQLException(e);
}
//Creating a connection to the database:
connectionSource = new JdbcConnectionSource("jdbc:sqlite:" + databaseFile.getAbsolutePath());
//Creating DAOs
notesDAO = DaoManager.createDao(connectionSource, DBNote.class);
cardsDAO = DaoManager.createDao(connectionSource, DBCard.class);
configDAO = DaoManager.createDao(connectionSource, DBConfig.class);
}
示例3: AbstractDaoService
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
public AbstractDaoService(Class clazz, SpringConfiguration configuration) {
try {
this.clazz = clazz;
// TODO: let user create it's own database
connection = DatabaseUtils.getH2OrmliteConnectionPool(configuration.getDatabasePath(),
// please do not laugh :)
SpringConfiguration.DB_LOGIN, SpringConfiguration.DB_PASSWORD);
TableUtils.createTableIfNotExists(connection, clazz);
// instantiate the dao
dao = DaoManager.createDao(connection, clazz);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
示例4: initDb
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
/**
* Initializes the actual database as a {@link JdbcConnectionSource}.
*
* @throws SQLException Thrown if the database can not be successfully initialized.
*/
private void initDb() throws SQLException {
// create a connection source to our database
connectionSource = new JdbcConnectionSource(DATABASE_URL, "sa", "",
new HsqldbDatabaseType());
// instantiate the DAO
vaultDao = DaoManager.createDao(connectionSource, VaultEntry.class);
if (!vaultDao.isTableExists()) {
TableUtils.createTableIfNotExists(connectionSource, VaultEntry.class);
} else {
LOG.warning("Found existing DB for VaultEntries. Reusing it!!");
}
rawDao = DaoManager.createDao(connectionSource, RawEntry.class);
if (!rawDao.isTableExists()) {
TableUtils.createTableIfNotExists(connectionSource, RawEntry.class);
}
// TableUtils.createTableIfNotExists(connectionSource, SliceEntry.class);
}
示例5: save
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
/**
* Saves the user data to the database.
*
* @param data The user data to save.
*/
private static void save(UserData data) {
data.setAvatarUrl(data.getAvatarUrl());
data.setUsername(data.getUsername());
ZLevels.async.submit(() -> {
try {
ConnectionSource source = Database.openConnection();
if (source == null) return;
Dao<UserData, String> db = DaoManager.createDao(source, UserData.class);
db.createOrUpdate(data);
Database.closeConnection();
} catch (Exception e) {
ZLogger.warn("Could not save UserData for " + data.getUserId() + "!");
e.printStackTrace();
}
});
}
示例6: delete
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
/**
* Deletes this user data from the database and cache.
*/
public void delete() {
UserData current = this;
if (cache.containsKey(current.getUserId())) cache.remove(current.getUserId());
ZLevels.async.submit(() -> {
try {
ConnectionSource source = Database.openConnection();
if (source == null) return;
Dao<UserData, String> db = DaoManager.createDao(source, UserData.class);
db.delete(current);
Database.closeConnection();
} catch (Exception e) {
ZLogger.warn("Colud not delete UserData for " + getUserId() + "!");
e.printStackTrace();
}
});
}
示例7: fromId
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
/**
* Gets the user data by userId from the cache and database.
*
* @param userId The user id.
* @return The user data.
*/
public static UserData fromId(String userId) {
if (cache.containsKey(userId)) {
return cache.get(userId);
}
try {
ConnectionSource source = Database.openConnection();
if (source == null) return null;
Dao<UserData, String> db = DaoManager.createDao(source, UserData.class);
UserData data = db.queryForEq("userId", userId).get(0);
Database.closeConnection();
return data;
} catch (Exception e) {
if (e instanceof IndexOutOfBoundsException) {
return null;
}
ZLogger.warn("Could not get UserData for " + userId + "!");
e.printStackTrace();
return null;
}
}
示例8: setUpTest
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
@BeforeClass
public static void setUpTest() throws Exception {
ServerEventService eventService = new ServerEventService();
daoProvider = new DataSourceProvider();
eventService.subscribe(DataSourceInitializedEvent.class, daoProvider);
DataSourceEventSubscriber daoInitializer = new DataSourceEventSubscriber();
eventService.subscribe(DeviceDaoCreatedEvent.class, daoInitializer);
eventService.subscribe(AgentDaoCreatedEvent.class, daoInitializer);
DataSourceManager dataSourceManager = new DataSourceManager(new DataSourceCallback());
dataSourceManager.initialize();
assertNotNull("Initialization of the database failed. ", agentDao);
// initialize a test agent entry in the data source to attach the devices to
agentDao.add(TEST_AGENT_ID);
initializeTestDevices();
// initialize an OrmLite device DAO to clear the device table after each test
JdbcConnectionSource connectionSource = new JdbcConnectionSource(Property.DATABASE_URL);
ormliteDeviceDao = DaoManager.createDao(connectionSource, Device.class);
}
示例9: getDao
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
/**
* This method obtains a DAO given its Class
* <p/>
* Source: https://goo.gl/6LIYy2
*
* @param clazz
* The DAO class
* @param <D>
* DAO super class
* @param <T>
* Requested DAO class
*
* @return The DAO instance
*
* @throws SQLException
*/
public <D extends Dao<T, ?>, T> D getDao(Class<T> clazz) throws SQLException {
// lookup the dao, possibly invoking the cached database config
Dao<T, ?> dao = DaoManager.lookupDao(connectionSource, clazz);
if (dao == null) {
// try to use our new reflection magic
DatabaseTableConfig<T> tableConfig = DatabaseTableConfigUtil
.fromClass(connectionSource, clazz);
if (tableConfig == null) {
/**
* Note: We have to do this to get to see if they are using the deprecated
* annotations like
* {@link DatabaseFieldSimple}.
*/
dao = (Dao<T, ?>) DaoManager.createDao(connectionSource, clazz);
} else {
dao = (Dao<T, ?>) DaoManager.createDao(connectionSource, tableConfig);
}
}
@SuppressWarnings("unchecked")
D castDao = (D) dao;
return castDao;
}
示例10: DatabaseManager
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
protected DatabaseManager() {
String databaseUrl = "jdbc:sqlite:halgnu.db";
try {
m_connectionSource = new JdbcConnectionSource(databaseUrl);
// Create daos
m_memberDao = DaoManager.createDao(m_connectionSource, MemberModel.class);
m_activityDao = DaoManager.createDao(m_connectionSource, ActivityModel.class);
TableUtils.createTableIfNotExists(m_connectionSource, MemberModel.class);
TableUtils.createTableIfNotExists(m_connectionSource, ActivityModel.class);
// Check if tables need to be created
CheckIfAdminExists();
} catch (SQLException e) {
e.printStackTrace();
}
}
示例11: getCachedWebResourcesDao
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
@NonNull
public Dao<CachedWebResource, String> getCachedWebResourcesDao() {
if (cachedWebResourcesDao == null) {
synchronized (this) {
if (cachedWebResourcesDao == null) {
try {
cachedWebResourcesDao = DaoManager.createDao(getConnectionSource(), CachedWebResource.class);
} catch (SQLException e) {
Log.e(TAG, "Cannot create DAO", e);
throw new RuntimeException(e);
}
}
}
}
return cachedWebResourcesDao;
}
示例12: getCachedFeedItemDao
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
@NonNull
public Dao<CachedFeedItem, String> getCachedFeedItemDao() {
if (cachedFeedItemDao == null) {
synchronized (this) {
if (cachedFeedItemDao == null) {
try {
cachedFeedItemDao = DaoManager.createDao(getConnectionSource(), CachedFeedItem.class);
} catch (SQLException e) {
Log.e(TAG, "Cannot create DAO", e);
throw new RuntimeException(e);
}
}
}
}
return cachedFeedItemDao;
}
示例13: getCachedUserDao
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
@NonNull
public Dao<CachedUser, Integer> getCachedUserDao() {
if (cachedUserDao == null) {
synchronized (this) {
if (cachedUserDao == null) {
try {
cachedUserDao = DaoManager.createDao(getConnectionSource(), CachedUser.class);
} catch (SQLException e) {
Log.e(TAG, "Cannot create DAO", e);
throw new RuntimeException(e);
}
}
}
}
return cachedUserDao;
}
示例14: getCachedGroupDao
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
@NonNull
public Dao<CachedGroup, Integer> getCachedGroupDao() {
if (cachedGroupDao == null) {
synchronized (this) {
if (cachedGroupDao == null) {
try {
cachedGroupDao = DaoManager.createDao(getConnectionSource(), CachedGroup.class);
} catch (SQLException e) {
Log.e(TAG, "Cannot create DAO", e);
throw new RuntimeException(e);
}
}
}
}
return cachedGroupDao;
}
示例15: getCachedWebPageDao
import com.j256.ormlite.dao.DaoManager; //导入依赖的package包/类
@NonNull
public Dao<CachedWebPage, String> getCachedWebPageDao() {
if (cachedWebPageDao == null) {
synchronized (this) {
if (cachedWebPageDao == null) {
try {
cachedWebPageDao = DaoManager.createDao(getConnectionSource(), CachedWebPage.class);
} catch (SQLException e) {
Log.e(TAG, "Cannot create DAO", e);
throw new RuntimeException(e);
}
}
}
}
return cachedWebPageDao;
}