版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中連接MongoDB數(shù)據(jù)庫
怎么在Android中連接MongoDB數(shù)據(jù)庫?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。MongoDB簡介Mongodb,分布式文檔存儲數(shù)據(jù)庫,由C++語言編寫,旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲解決方案。MongoDB是一個高性能,開源,無模式的文檔型數(shù)據(jù)庫,是當(dāng)前NoSql數(shù)據(jù)庫中比較熱門的一種。它在許多場景下可用于替代傳統(tǒng)的關(guān)系型數(shù)據(jù)庫或鍵/值存儲方式。Mongo使用C++開發(fā)。Mongo安裝參考1)下載安裝包文件,解壓到某一文件夾下。官方下載地址:/downloads2)配置環(huán)境變量:在path后添加安裝路徑。3)啟動Mongo數(shù)據(jù)庫:進(jìn)入“cmd”->鍵入“mongod--dbpathD:\amp\MongoDBDATA”D:\amp\MongoDBDATA表示數(shù)據(jù)庫文件存儲路徑4)啟動Mongo客戶端:mongo:27017/adminAndroid連接MongoDB步驟1:下載并導(dǎo)入jar包到工程步驟2:安裝MongoDB到PC端(參見MongoDB安裝)步驟3:編寫代碼連接MongoDB實(shí)現(xiàn)簡單操作(增刪改查)代碼參考(Android端,也適用于java等工程)1.
MongoDBUtil.javapackage
com.hills.happytest.utils;
import
java.util.List;
import
org.bson.types.ObjectId;
import
com.mongodb.BasicDBObject;
import
com.mongodb.DB;
import
com.mongodb.DBCollection;
import
com.mongodb.DBCursor;
import
com.mongodb.DBObject;
import
com.mongodb.Mongo;
/**
*
Class
Name:
MongoDBUtil.java
*
Function:
*
The
Util
that
MongoDB
Operate.
*
Modifications:
*
*
@author
Gym
Yung.
*
@DateTime
2014-10-29
下午1:56:49
*
@version
1.0
*/
public
class
MongoDBUtil
{
static
Mongo
connection
=
null;
static
DB
db
=
null;
public
MongoDBUtil(String
dbName)
throws
Exception
{
connection
=
new
Mongo(":27017");
db
=
connection.getDB(dbName);
}
public
static
Mongo
getConnection()
{
return
connection;
}
/**
*
Create
a
blanket
collection.
*
@param
collName
:collection
name.
*/
public
void
createCollection(String
collName)
{
DBObject
dbs
=
new
BasicDBObject();
dbs.put("test",
"test");
db.createCollection(collName,
dbs);
}
/**
*
Insert
dbObject
into
collection.
*
@param
dbObject
*
@param
collName
*/
public
void
insert(DBObject
dbObject,String
collName)
{
DBCollection
collection
=
db.getCollection(collName);
collection.insert(dbObject);
}
/**
*
Insert
dbObject
list
into
collection.
*
@param
dbObjects
*
@param
collName
*/
public
void
insertBatch(List<DBObject>
dbObjects,String
collName)
{
DBCollection
collection
=
db.getCollection(collName);
collection.insert(dbObjects);
}
/**
*
Delete
data
By
Id.
*
@param
id
*
@param
collName
*
@return
*/
public
int
deleteById(String
id,String
collName)
{
DBCollection
collection
=
db.getCollection(collName);
DBObject
dbs
=
new
BasicDBObject("_id",
new
ObjectId(id));
int
counts
=
collection.remove(dbs).getN();
return
counts;
}
/**
*
Delete
data
By
Condition.
*
@param
dbObject
*
@param
collName
*
@return
*/
public
int
deleteByDbs(DBObject
dbObject,String
collName)
{
DBCollection
collection
=
db.getCollection(collName);
int
count
=
collection.remove(dbObject).getN();
return
count;
}
/**
*
Update
Data.
*
@param
find
*
@param
update
*
@param
upsert
*
@param
multi
*
@param
collName
*
@return
*/
public
int
update(DBObject
find,DBObject
update,boolean
upsert,boolean
multi,String
collName)
{
DBCollection
collection
=
db.getCollection(collName);
int
count
=
collection.update(find,
update,
upsert,
multi).getN();
return
count;
}
/**
*
Find
Data
With
Page.
*
@param
ref
*
@param
keys
*
@param
start
*
@param
limit
*
@param
collName
*
@return
*/
public
DBCursor
findWithPage(DBObject
where,DBObject
selection,int
start,int
limit,String
collName)
{
DBCursor
cursor
=
findNoPage(where,
selection,
collName);
return
cursor.limit(limit).skip(start);
}
/**
*
Find
Data
No
Page.
*
@param
ref
*
@param
keys
*
@param
collName
*
@return
*/
public
DBCursor
findNoPage(DBObject
where,DBObject
selection,String
collName)
{
DBCollection
collection
=
db.getCollection(collName);
DBCursor
cursor
=
collection.find(where,
selection);
return
cursor;
}
}2.MongoDBDao.javapackage
com.hills.happytest.utils;
import
java.util.ArrayList;
import
java.util.List;
import
com.mongodb.BasicDBObject;
import
com.mongodb.DBCursor;
import
com.mongodb.DBObject;
/**
*
Class
Name:
MongoDBDao.java
*
Function:
*
The
Data
Dao
that
MongoDB
Operate.
*
Modifications:
*
*
@author
Administrator
*
@DateTime
2014-10-29
下午1:57:58
*
@version
1.0
*/
public
class
MongoDBDao
{
private
static
MongoDBUtil
mongoDb;
//
Init.
MongoDBUtil.
static{
try
{
mongoDb
=
new
MongoDBUtil("test");
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
/**
*
Test
Create
a
blanket
collection.
*
@param
collName
:collection
name.
*/
public
void
createCollectionTest(String
collName)
{
mongoDb.createCollection(collName);
}
/**
*
Test
Insert
dbObject
into
collection.
*
@param
collName
Collection
Name.
*/
public
void
insertTest(String
collName)
{
DBObject
dbs
=
new
BasicDBObject();
dbs.put("name",
"gymyung");
dbs.put("age",
20);
List<String>
books
=
new
ArrayList<String>();
books.add("EXTJS");
books.add("MONGDB");
books.add("JAVA");
dbs.put("books",
books);
mongoDb.insert(dbs,
collName);
}
/**
*
Test
Insert
dbObject
list
into
collection.
*
@param
collName
Collection
Name.
*/
public
void
insertBatchTest(String
collName)
{
List<DBObject>
dbObjects
=
new
ArrayList<DBObject>();
DBObject
jim2
=
new
BasicDBObject("name",
"jim2");
DBObject
liuting
=
new
BasicDBObject();
liuting.put("name",
"liuting");
liuting.put("age",
"22");
dbObjects.add(jim2);
dbObjects.add(liuting);
mongoDb.insertBatch(dbObjects,
collName);
}
/**
*
Test
Delete
data
By
Id.
*
@param
collName
Collection
Name.
*
@return
Operate
Result
Code.
*/
public
int
deleteByIdTest(String
collName)
{
int
counts
=
mongoDb.deleteById("54507d19cbbd7a385c129ef5",
collName);
return
counts;
}
/**
*
Test
Delete
data
By
Condition.
*
@param
collName
Collection
Name.
*
@return
Operate
Result
Code.
*/
public
int
deleteByDbsTest(String
collName)
{
DBObject
jim2
=
new
BasicDBObject("name",
"jim2");
int
count
=
mongoDb.deleteByDbs(jim2,
collName);
return
count;
}
/**
*
Test
Update
Data.
*
@param
collName
Collection
Name.
*
@return
Operate
Result
Code.
*/
public
int
updateTest(String
collName)
{
DBObject
liuting
=
new
BasicDBObject();
DBObject
liuting2
=
new
BasicDBObject();
liuting2.put("$set",
new
BasicDBObject("gender",
"female"));
int
count
=
mongoDb.update(liuting,
liuting2,
false,
true,
collName);
return
count;
}
/**
*
Test
Find
Data
With
Page.
*
@param
collName
Collection
Name.
*
@return
String
List
Result.
*/
public
List<String>
findWithPageTest(String
collName)
{
DBCursor
cursor
=
mongoDb.findWithPage(null,
null,
0,
3,
collName);
return
convertCursorToList(cursor);
}
/**
*
Test
Find
Data
With
Condition.
*
@param
collName
Collection
Name.
*
@return
String
List
Result.
*/
public
List<String>
findWithConditionTest(String
collName)
{
DBObject
where
=
new
BasicDBObject();
where.put("age",
new
BasicDBObject("$lte",
26));
where.put("gender",
"female");
DBCursor
cursor
=
mongoDb.findNoPage(where,
null,collName);
return
convertCursorToList(cursor);
}
/**
*
Test
Find
Data
No
Page.
*
@param
collName
Collection
Name.
*
@return
String
List
Result.
*/
public
List<String>
findNoPageTest(String
collName)
{
DBObject
keys
=
new
BasicDBObject();
keys.put("_id",
false);
keys.put("name",
true);
keys.put("age",
true);
DBCursor
cursor
=
mongoDb.findNoPage(null,
keys,
collName);
return
convertCursorToList(cursor);
}
/**
*
Convert
Cursor
To
List.
*
@param
cursor
Required
DBCursor.
*
@return
String
List
Result.
*/
private
List<String>
convertCursorToList(DBCursor
cursor)
{
List<String>
results
=
new
ArrayList<String>();
while(cursor.hasNext())
{
DBObject
dbObject
=
cursor.next();
for(String
key
:
dbObject.keySet())
{
results.add("{"+key+":"+dbObject.get(key)+"}");
}
}
return
results;
}
}3.TestActivity.javapackage
com.hills.happytest;
import
java.util.List;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import
android.os.AsyncTask;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
com.hills.happytest.utils.MongoDBDao;
import
com.hills.happytest.utils.MongoDBUtil;
/**
*
Class
Name:
TestActivity.java
*
Function:
*
Test
MongoDB
Operate
on
Android
device.
*
Modifications:
*
*
@author
Gym
Yung.
*
@DateTime
2014-10-29
下午1:53:40
*
@version
1.0
*/
public
class
TestActivity
extends
Activity
implements
OnClickListener{
/**************
Component
in
Layout.
***************/
private
Button
mongoTestBtn1;
private
Button
mongoTestBtn2;
private
Button
mongoTestBtn3;
private
Button
mongoTestBtn4;
private
Button
mongoTestBtn5;
private
Button
mongoTestBtn6;
private
Button
mongoTestBtn7;
private
Button
mongoTestBtn8;
//
The
Object
use
to
MongoDB
Operate.
private
MongoDBDao
mongoDbDao;
//
The
Collection
Name
in
MongoDB.
private
String
collName
=
"androidDB";
/**************
Operate
Code
********************/
private
final
int
CREATE_COLLECTION_TEST
=
100;
private
final
int
INSERT_TEST
=
101;
private
final
int
INSERT_BATCH_TEST
=
102;
private
final
int
DELETE_BY_ID_TEST
=
103;
private
final
int
DELETE_BY_DBS_TEST
=
104;
private
final
int
UPDATE_TEST
=
105;
private
final
int
FIND_WITH_PAGE_TEST
=
106;
private
final
int
FIND_NOPAGE_TEST
=
107;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mongodb_test);
findViewAndSetListener();
mongoDbDao
=
new
MongoDBDao();
}
/**
*
Function:
*
Find
View
And
Set
Listener.
*
@author
Administrator
*
@DateTime
2014-10-29
下午1:08:50
*/
private
void
findViewAndSetListener()
{
mongoTestBtn1
=
(Button)
findViewById(R.id.mongodb_btn1);
mongoTestBtn2
=
(Button)
findViewById(R.id.mongodb_btn2);
mongoTestBtn3
=
(Button)
findViewById(R.id.mongodb_btn3);
mongoTestBtn4
=
(Button)
findViewById(R.id.mongodb_btn4);
mongoTestBtn5
=
(Button)
findViewById(R.id.mongodb_btn5);
mongoTestBtn6
=
(Button)
findViewById(R.id.mongodb_btn6);
mongoTestBtn7
=
(Button)
findViewById(R.id.mongodb_btn7);
mongoTestBtn8
=
(Button)
findViewById(R.id.mongodb_btn8);
mongoTestBtn1.setOnClickListener(this);
mongoTestBtn2.setOnClickListener(this);
mongoTestBtn3.setOnClickListener(this);
mongoTestBtn4.setOnClickListener(this);
mongoTestBtn5.setOnClickListener(this);
mongoTestBtn6.setOnClickListener(this);
mongoTestBtn7.setOnClickListener(this);
mongoTestBtn8.setOnClickListener(this);
}
@Override
public
void
onClick(View
v)
{
MyAsyncTast
myAsyncTast
=
new
MyAsyncTast();
switch(v.getId()){
case
R.id.mongodb_btn1:
myAsyncTast.execute(CREATE_COLLECTION_TEST);
break;
case
R.id.mongodb_btn2:
myAsyncTast.execute(INSERT_TEST);
break;
case
R.id.mongodb_btn3:
myAsyncTast.execute(INSERT_BATCH_TEST);
break;
case
R.id.mongodb_btn4:
myAsyncTast.execute(DELETE_BY_ID_TEST);
break;
case
R.id.mongodb_btn5:
myAsyncTast.execute(DELETE_BY_DBS_TEST);
break;
case
R.id.mongodb_btn6:
myAsyncTast.execute(UPDATE_TEST);
break;
case
R.id.mongodb_btn7:
myAsyncTast.execute(FIND_WITH_PAGE_TEST);
break;
case
R.id.mongodb_btn8:
myAsyncTast.execute(FIND_NOPAGE_TEST);
break;
}
}
/**
*
Class
Name:
TestActivity.java
*
Function:
*
Execute
Internet
Task
by
Async...
*
Modifications:
*
*
@author
Administrator
*
@DateTime
2014-10-29
下午1:54:34
*
@version
1.0
*/
class
MyAsyncTast
extends
AsyncTask<Object,
Object,
Object>{
@Override
protected
Object
doInBackground(Object...
params)
{
Object
result
=
null;
switch(Integer.parseInt(params[0].toString()))
{
case
CREATE_COLLECTION_TEST:
mongoDbDao.createCollectionTest(collName);
break;
case
INSERT_TEST:
mongoDbDao.insertTest(collName);
break;
case
INSERT_BATCH_TEST:
mongoDbDao.insertBatchTest(collName);
break;
case
DELETE_BY_ID_TEST:
result
=
mongoDbDao.deleteByIdTest(collName);
break;
case
DELETE_BY_DBS_TEST:
result
=
mongoDbDao.deleteByDbsTest(collName);
break;
case
UPDATE_TEST:
result
=
mongoDbDao.updateTest(collName);
break;
case
FIND_WITH_PAGE_TEST:
result
=
mongoDbDao.findWithPageTest(collName);
break;
case
FIND_NOPAGE_TEST:
result
=
mongoDbDao.findNoPageTest(collName)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 四川省宜賓市筠連縣第二中學(xué)2025屆高一物理第一學(xué)期期末調(diào)研試題含解析
- 2025屆青海省大通回族土族自治縣第一中學(xué)高二物理第一學(xué)期期中調(diào)研模擬試題含解析
- 2025屆天津市靜海區(qū)大邱莊中學(xué)高三上物理期中調(diào)研模擬試題含解析
- 2025屆江蘇省江陰四校物理高三第一學(xué)期期中復(fù)習(xí)檢測試題含解析
- 2025屆河北省石家莊市精英中學(xué)物理高二第一學(xué)期期末綜合測試模擬試題含解析
- 2025屆山西省大同市鐵路一中高三物理第一學(xué)期期中調(diào)研試題含解析
- 2025屆天津市河西區(qū)實(shí)驗(yàn)中學(xué)物理高一上期末質(zhì)量檢測試題含解析
- 2025屆河北省鹿泉一中等名校物理高二上期中統(tǒng)考模擬試題含解析
- 定西市重點(diǎn)中學(xué)2025屆高一物理第一學(xué)期期末學(xué)業(yè)質(zhì)量監(jiān)測試題含解析
- 四川省樹德中學(xué)2025屆物理高一第一學(xué)期期末綜合測試模擬試題含解析
- 2025年廣東省高中學(xué)業(yè)水平考試春季高考數(shù)學(xué)試題(含答案解析)
- 形勢與政策(吉林大學(xué))智慧樹知到答案2024年吉林大學(xué)
- 24秋國家開放大學(xué)《會計信息系統(tǒng)(本)》測試題參考答案
- 盛世華誕慶祝祖國成立75周年共筑中國夢同慶國慶節(jié)課件
- 2024年二級建造師繼續(xù)教育題庫及答案(500題)
- 2024年“正大杯”市場調(diào)查與分析競賽考試題庫及答案
- 二年級100以內(nèi)加減乘除混合口算題(直接打印)
- MOOC 制造技術(shù)基礎(chǔ)訓(xùn)練-北京理工大學(xué) 中國大學(xué)慕課答案
- 人教版九年級英語上冊閱讀理解10篇(含答案)
- 冬季作業(yè)安全技術(shù)交底
- 降低會陰側(cè)切率的PDCA
評論
0/150
提交評論