详解数据库基础操作及实例 废话不多说,直接上代码,注释写的比较清楚,大家参考下, 示
详解数据库基础操作及实例
废话不多说,直接上代码,注释写的比较清楚,大家参考下,
示例代码:
public static void aDDDBObject(DBCollection collection,BasicDBObject object){
collection.insert(object);
}
public static DBObject getDBObjectById(String value) throws UnknownHostException, MonGoException{
dbc = getDBCollection("company", "users").find(new BasicDBObject("_id",new ObjectId(value)));
DBObject ob = null;
int i = 0;
while(dbc.hasNext()){
ob = dbc.next();
i++;
}
if(i == 1){
return ob;
}else{
return null;
}
}
public static DBObject getDBObject(String key,String value) throws UnknownHostException, MongoException{
dbc = getDBCollection("company", "users").find(new BasicDBObject(key,value));
DBObject ob = null;
int i = 0;
while(dbc.hasNext()){
ob = dbc.next();
i++;
}
if(i == 1){
return ob;
}else{
return null;
}
}
public static Set<String> getCollectionsNames(String DBName) throws MongoException, UnknownHostException{
return getDB(DBName).getCollectionNames();
}
public static Set<DBObject> getDBObjects(DBCollection collection){
Set<DBObject> dbObjects = new HashSet<DBObject>();
DBCursor cursor = collection.find();
while(cursor.hasNext()){
DBObject object = cursor.next();
dbObjects.add(object);
}
return dbObjects;
}
public static DBCollection getDBCollection(String DBName,String collectionName) throws UnknownHostException, MongoException{
return getDB(DBName).getCollection(collectionName);
}
public static DB getDB(String DBName) throws UnknownHostException, MongoException{
return getMongo().getDB(DBName);
}
public static Mongo getMongo() throws UnknownHostException, MongoException{
Mongo mg = null;
if(mg == null){
mg = new Mongo();
}
return mg;
}
public static void destory(Mongo mg) {
if (mg != null){
mg.close();
mg = null;
}
System.GC();
}
public static List<String> getDBNames() throws MongoException, UnknownHostException{
return getMongo().getDatabaseNames();
}
public static void deleteDB(String DBName) throws MongoException, UnknownHostException{
getMongo().dropDatabase(DBName);
}
--结束END--
本文标题: 详解MongoDB数据库基础操作及实例
本文链接: https://lsjlt.com/news/52657.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0