少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
package com.abin.mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
public class DBClass {
private static final String host="127.0.0.1";
private static final int port=27017;
private static final String username="abin";
private static final String passwd="abin";
private static final String dbase="abin";
private static boolean auth=false;
private static Mongo mg=null;
public static DBCollection Connection(String collection){
DB db=null;
DBCollection col=null;
try {
mg=new Mongo(host,port);
db=mg.getDB(dbase);
auth=db.authenticate(username, passwd.toCharArray());
if(auth==true){
System.out.println("用户授权通过");
col=db.getCollection(collection);
}
} catch (Exception e) {
e.printStackTrace();
}
return col;
}
public static void MongoClose(Mongo mg){
if(mg!=null){
mg.close();
}
}
}




package com.abin.mongo;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
public class Connection{
DBCollection col=null;
@Before
public void setUp(){
String coll="abing55";
col=new DBClass().Connection(coll);
}
@Test
public void add(){
System.out.println("循环前:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()));
for(int i=0;i<100000;i++){
DBObject col1=new BasicDBObject();
col1.put("name", "steven"+i);
col1.put("age", 24+i);
col1.put("sex", "男"+i);
col.insert(col1);
}
System.out.println("循环后:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()));
}
@Test
public void query(){
System.out.println("循环前:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()));
DBCursor cur=col.find();
while(cur.hasNext()){
System.out.println(cur.next());
}
System.out.println("循环后:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()));
}
}






package com.abin.mongodb;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
public class ConnectionMongoDB {
private Mongo mg;
private DB db;
private DBCollection users;
@Before
public void init()throws Exception{
try {
mg=new Mongo("localhost",27017);
} catch (Exception e) {
e.printStackTrace();
}
db=mg.getDB("temp");
users=db.getCollection("users");
}
@After
public void destory(){
if(mg!=null){
mg.close();
}
db=null;
users=null;
}
public void print(Object o){
System.out.println(o);
}
public void queryAll(){
print("查询users表所有的数据");
DBCursor cur=users.find();
while(cur.hasNext()){
print(cur.next());
}
}
@Test
public void add(){
DBObject user=new BasicDBObject();
user.put("name", "abin");
user.put("age", 24);
user.put("sex", "男");
print(users.save(user).getN());
queryAll();
}
}
posted on 2011-12-19 17:09 abin 阅读(2610) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: