本文整理匯總了Java中com.j256.ormlite.dao.DaoManager.createDao方法的典型用法代碼示例。如果您正苦於以下問題:Java DaoManager.createDao方法的具體用法?Java DaoManager.createDao怎麽用?Java DaoManager.createDao使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.j256.ormlite.dao.DaoManager
的用法示例。
在下文中一共展示了DaoManager.createDao方法的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: 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;
}
}
示例6: 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);
}
示例7: 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;
}
示例8: markPostType
import com.j256.ormlite.dao.DaoManager; //導入方法依賴的package包/類
public void markPostType(String datasetName) {
ConnectionSource source = super.getConnectionSource();
try {
Dao<DataEntity, Long> DataDao = DaoManager.createDao(source, getEntityConfigOfDataSet(source, DataEntity.class, datasetName) );
List<DataEntity> dataEntities = DataDao.queryForAll();
UpdateBuilder<DataEntity, Long> updateBuilder = DataDao.updateBuilder();
for (DataEntity entity : dataEntities) {
String text = entity.getTitle();
updateBuilder.where().eq("post_id", entity.getPostId());
if (text != null && text.startsWith("Re:")) {
updateBuilder.updateColumnValue("post_type_id", 2);
} else {
updateBuilder.updateColumnValue("post_type_id", 1);
}
updateBuilder.update();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
示例9: 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;
}
示例10: saveToDb
import com.j256.ormlite.dao.DaoManager; //導入方法依賴的package包/類
public void saveToDb(String datasetName, long queryId, ConnectionSource connSrc) {
try {
DatabaseHandler dbHandler = new DatabaseHandler();
Dao<GraphEntity, Long> graphDao = DaoManager.createDao(connSrc, dbHandler.getEntityConfigOfDataSet(connSrc, GraphEntity.class, datasetName) );
graphEntity = new GraphEntity();
graphEntity.setQueryId(queryId);
graphEntity.setCreateDate(new Date());
graphEntity.setGraph(getGraphAsString("graph_jung.graphml"));
// graphDao.createIfNotExists(graphEntity);
} catch (SQLException e) {
e.printStackTrace();
}
}
示例11: 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;
}
示例12: addUser
import com.j256.ormlite.dao.DaoManager; //導入方法依賴的package包/類
public void addUser(String username) {
UserAccEntity entity = null;
try {
Dao<UserAccEntity, Long> AccDao = DaoManager.createDao(super.getConnectionSource(), UserAccEntity.class);
entity = new UserAccEntity();
entity.setUserName(username);
entity.setDate(new Date());
// AccDao.createIfNotExists(entity);
AccDao.create(entity);
} catch (SQLException e) {
System.out.println("Error in getting post..." + e);
e.printStackTrace();
}
}
示例13: getDatabaseName
import com.j256.ormlite.dao.DaoManager; //導入方法依賴的package包/類
/**
*
* @param datasetId
* An id identifying the dataset. Ids are stored in a database
* called ersdb.
*
* @return Returns the database name associated with a particular dataset.
*/
private String getDatabaseName(String datasetId) {
DatabaseHandler handler = new DatabaseHandler("ersdb", "root", "");
String databaseName = null;
try {
Dao<DataInfoEntity, Long> DatasetInfoDao = DaoManager.createDao(handler.getConnectionSource(), DataInfoEntity.class);
DataInfoEntity datasetEntity = DatasetInfoDao.queryForId(Long.parseLong(datasetId));
databaseName = datasetEntity.getDatabaseName();
} catch (SQLException e) {
e.printStackTrace();
}
handler.close();
return databaseName;
}
示例14: testConnectionRollback
import com.j256.ormlite.dao.DaoManager; //導入方法依賴的package包/類
@Test
public void testConnectionRollback() throws Exception {
JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL);
Dao<Foo, Integer> dao = null;
DatabaseConnection conn = null;
try {
TableUtils.createTable(pooled, Foo.class);
dao = DaoManager.createDao(pooled, Foo.class);
conn = dao.startThreadConnection();
dao.setAutoCommit(conn, false);
Foo foo = new Foo();
assertEquals(1, dao.create(foo));
assertNotNull(dao.queryForId(foo.id));
dao.endThreadConnection(conn);
assertNull(dao.queryForId(foo.id));
} finally {
TableUtils.dropTable(pooled, Foo.class, true);
if (dao != null) {
dao.endThreadConnection(conn);
}
pooled.close();
}
}
示例15: getDao
import com.j256.ormlite.dao.DaoManager; //導入方法依賴的package包/類
public <D extends Dao<T, ?>, T> D getDao(Class<T> type) {
synchronized (lock) {
Dao dao = managers.get(type);
if (dao == null) {
try {
dao = DaoManager.createDao(connectionSource, type);
managers.put(type, dao);
} catch (SQLException e) {
throw new Panic(e);
}
}
return (D) dao;
}
}